Stack Google Sheets columns in a cleaner way? - google-sheets

I am making a spreadsheet to compare various lists in various different ways. One of those ways is to take a bunch of separate lists (each in different columns) and combine them into one large column (without losing anything). This allows me to keep things separate while still having the option to VLOOKUP and FILTER and stuff. Maybe this isn't the way to go about it, but it's the way I've gone with so far.
The solution I came up with for now is to QUERY it all as an array (I think that's the right term) and then just select everything that isn't "".
It looks like this though:
=QUERY({Decklists!B8:B;Decklists!C8:C;Decklists!D8:D;Decklists!E8:E;Decklists!F8:F;Decklists!G8:G;Decklists!H8:H;Decklists!I8:I;Decklists!J8:J;Decklists!K8:K;Decklists!L8:L;Decklists!M8:M;Decklists!N8:N;Decklists!O8:O;Decklists!P8:P;Decklists!Q8:Q;Decklists!R8:R;Decklists!S8:S;Decklists!T8:T;Decklists!U8:U;Decklists!V8:V;Decklists!W8:W;Decklists!X8:X;Decklists!Y8:Y;Decklists!Z8:Z;Decklists!AA8:AA;Decklists!AB8:AB;Decklists!AC8:AC;Decklists!AD8:AD}, "select Col1 where Col1 <>''", 0)
Which is really ugly. It works and does what I want, but I'd much prefer it to be clean and work if I add more columns in the future, without having to go and add the additional "call".
If you'd like to look at the whole sheet, it's here: https://docs.google.com/spreadsheets/d/17Nwek5ZCgu7Jvk922hl_gv9UJAeORjeH9brXqu3zL_Y/edit#gid=940775206

On the "Best Buys" sheet cell C2 Paste this simple formula.
=QUERY(FLATTEN(Decklists!B8:AF), "select Col1 where Col1 <>''")
Explanation
1 - FLATTEN the range needed in this case the Decklists range Decklists!B8:AF.
2 - QUERY the resulted column from the FLATTEN function with the query "select Col1 where Col1 <>''", to get only non empty cells

Try
=query( flatten(offset(Decklists!A:A,0,1,,columns(Decklists!1:1)-1)) ,"where Col1 is not null",0)

So we have 3 things to do, first one is transpose so the columns are below the other, then flatten so that it is ordered as necessary and finally a filter to remove extra blank columns we selected This will work wonders considering the columns are different lengths, are already next to each other as in a table. If columns are at multiple places use {A:A;D:D} to select columns and remove flatten and transpose.
=filter(flatten(transpose(A1:D10)), len(flatten(transpose(A1:D10))))

Related

Google sheets formula transposing column title to row

Attached is the link of my question
I would like to transpose the data like that.
My original data could be thousand of lines.
My thought is to make the same number of tables of my column title, then combine the four tables into one. My thought is on the google sheet as well. It might do the work but I would like a nicer solution.
A picture of my question
After having answered this question hundreds of times in the last couple years after the discovery of the FLATTEN() function, i decided to write a custom function for Google AppsScript to do it. While in some sense, a custom function is more opaque than the SPLIT(FLATTEN( formulaic solution you will find all over the forums in the last couple years, it is at least a little easier to understand in it's operation by the user.
In your shared sheet there is a new script file called MKHelp.gs. In it, you will find the code that I wrote to construct the "UNPIVOT()" function.
On a new tab in your sheet, you will find this formula in a tab called MK.Help.
=QUERY(UNPIVOT(A2:B,"V",C2:E,"B",C1:E1,"H"),"where Col3 is not null")
for unpivot to work well it is best surrounded by a query() to weed out unnecessary rows. In your case, it is that the "amount" not be "empty" or "null".
The letters "V","H" or "B" that follow each range describe the "shape" of the input data. Whether it is "vertical", "horizontal" or "Both".
Try
=query(arrayformula(split(flatten({(A3:A4&"♦"&B3:B4&"♦"&C3:E4)}),"♦")),"select * where Col3 is not null")
Explanation
step1: =arrayformula(A3:A4&"♦"&B3:B4&"♦"&C3:E4)
step2: =arrayformula(flatten(A3:A4&"♦"&B3:B4&"♦"&C3:E4)) , flatten will put all data inside one single column flatten()
step3: =query(arrayformula(split(flatten({(A3:A4&"♦"&B3:B4&"♦"&C3:E4)}),"♦")),"select * where Col3 is not null")

Splitting a cell containing a list of text values into other cells, depending on the values in the list

I'm wondering whether it's possible to split a cell that contains a comma-separated list into various columns, based on a reference table that is editable, using only Google Sheets formulae and not resorting to Apps Script.
I have created a simplified version to better explain what I'm trying to achieve.
I am presented with a table like this:
In A2 there is a list of colour names, and I would like them to be sorted into the correct columns in new, smaller lists. The ideal solution would look like this:
Ideally I would do this with formulae that live in B2, C2, and D2. What goes in which cell though has to be dynamic, so that if new options came about they could be sorted into the correct cells.
I imagine the best way to achieve this whilst keeping what goes in which column dynamic would be to have the formulae compare against a a reference table living in a "master" document. For example:
The formula in B2 would reference table column A, C2 would reference table column B, etc.
This way the formulae would not require editing - which I want to avoid as this system would be used across many different Google Sheets documents.
Whilst this is certainly achievable using Apps Script, I would rather the formulae do the heavy lifting so that if someone inherits my role further down the line they aren't required to understand Apps Script/JS etc. to maintain and/or modify the system.
I would imagine that the process involves SPLIT and JOIN by ", ", and possibly a LOOKUP (or VLOOKUP), but I can't work out how (or even if) it's possible.
Any input would be greatly appreciated!
Thanks
paste under REDS and drag to the right:
=INDEX(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE(
IF(REGEXMATCH($A11:$A, "(?i)"&
TRANSPOSE(FILTER(master!A2:A8, master!A2:A8<>""))),
TRANSPOSE(FILTER(master!A2:A8, master!A2:A8<>""))&",", )),,9^9))), ",$", ))
update:
=ARRAYFORMULA(IFNA(VLOOKUP(""&ROW(A11:A), SUBSTITUTE(REGEXREPLACE(TRIM(
SPLIT(FLATTEN(QUERY(QUERY(SPLIT(FLATTEN(ROW(A11:A)&"♠♣"&IFERROR(TRIM(
SPLIT(LOWER($A11:$A), ","))&"♦")), "♣"),
"select max(Col2)
where Col2 matches '^"&TEXTJOIN("$|^", 1, IF(B2:B8="",,LOWER(B2:B8)&"♦"))&"$'
group by Col2 pivot Col1"),,9^9)), "♠")), "♦$", ), "♦", ","), 2, 0)))

Google Spreadsheets QUERY() to combine multiple sheets, by value of one field

I am having the following problem: There are two Sheets in my document which contain different data about the cities (which are listed in column A in both cases). I am trying to create a combination where the data about a given city is merged from the second sheet.
I was trying to explore QUERY function, as it's suggested in a similar topic:
=QUERY({Sheet1!A2:C5;Sheet2!A2:C5},"select Col1 where Col1 <>''") However it's not clear how to replace the where Col1 <>'' with an appropriate statement.
Since Query Docs does not support Join similar to SQL, one workaround is to use ARRAYFORMULA, VLOOKUP, and indirect reference to achieve a similar result:
=ARRAYFORMULA({Sheet1!A1:C5,VLOOKUP(Sheet1!A1:A5,Sheet2!A1:B5,COLUMN(INDIRECT("R1C2:R1C"&COLUMNS(Sheet2!A1:B5),0)),0)})
From your tables on the question, the result looks like this:
References:
ARRAYFORMULA()
VLOOKUP()
INDIRECT()
COLUMN()
COLUMNS()

How to find common words in two columns in Google Sheets

I am stuck with a task which am seeking help from you guys. the solution I want is to find common words in two columns. eg if a word comes common in two cells it must appear in another column. we don't need the words only in one column.
https://docs.google.com/spreadsheets/d/1u_AD1FAbq0ntushGSYZBsR59iPX1_p5ZSIq1mowQSQ4/edit#gid=0
pls take a look to the shared link.
A screenshot also is shown here:
In a array context,
MATCH Col1 against Col2 getting indexes of strings in Col1 that are present in Col2
FILTER Col1 against the resulting matched array
=ARRAYFORMULA(FILTER(A2:A10,MATCH(A2:A10,B2:B10,0)))

How to search and sort multiple columns into one column

This is like my input. if theres better way to import that site, you can suggest me it
I need to find all columns with prices and put them in one big column-table. My point is to use vlookup afterwards.
Also I need formula not normal references, because the table can vary. Similar thing is that there are always names+price(+$}.
It should maybe check every cell with text and check if theres number next to it, then add it to big table?
try:
=QUERY({
Original!D:E; Original!H:I; Original!L:M; Original!P:Q};
"where Col1 is not null and Col2 is not null"; 0)

Resources