How can I generate a three column list of unique "combos"? - google-sheets

I have three columns of information. For example: color, model, year.
Can I use the "unique" instruction to generate in three new columns each unique combination for color, model, year, each in one column?
ex.
color model year
red sedan 2016
red sedan 2020
black truck 2018
Thanks!

Suppose your three headers are in A1, B1 and C1 with your data running A2:C. And suppose you want the unique combinations in E:G. First, be sure that the entire range E:G is empty. Then place the following formula in E1:
=ArrayFormula({A1:C1;SPLIT(FLATTEN(UNIQUE(FILTER(A2:A,A2:A<>""))&"|"&TRANSPOSE(FLATTEN(UNIQUE(FILTER(B2:B,B2:B<>""))&"|"&TRANSPOSE(UNIQUE(FILTER(C2:C,C2:C<>"")))))),"|")})
The formula first reproduces the headers from A1:C1.
The combinations are formed by first concatenating each UNIQUE model (from a list that is FILTERed to remove blanks) with each UNIQUE year (from a list that is also FILTERed to remove blanks), with a pipe symbol between each as a separator that SPLIT will later use.
That grid of combinations is FLATTENed into a single column and then concatenated once more with a UNIQUE and FILTERed list of the colors leading off, and again with a pipe symbol as a separator. Once more, the entire grid of results is FLATTENed into a single column.
Finally, SPLIT acts on the pipe symbols to separate the three pieces into their own columns under the headers.

try:
=INDEX({A1:C1; UNIQUE(QUERY(SPLIT(FLATTEN(FLATTEN(A2:A&"×"&
TRANSPOSE(B2:B))&"×"&TRANSPOSE(C2:C)), "×"),
"where Col3 is not null"))})
the task is simple: take column A and combine it with transposed column B. flatten the output in one single column and combine it with transposed column C and again flatten it into one single column. then split it and query out all combinations that have less than 3 columns. next, run it through unique to remove duplicates.

Related

Unnest two columns in google sheet

I have a table like this one here (basically it's data from a google form with multiple choice answers in column A and B and non-muliple choice data in column C) I need a separate row for each multiple choice answer.
Column A
Column B
Email
A,B
XX,YY
1#gmail.com
A,C
FF,DD
2#gmail.com
I tried to un-nest the first column and keep the remaining columns like this
enter image description here
I tried several approaches I found with flatten and split with array formulas but I don't know where to start really.
Any help or hint would be much appreciated!
You can use the split function on the column A and after that, use the index function. Considering the table, you can use:
=index(split(A2,","),1,1)
The split function separate the text using the delimiter indicated, returning an array with 1 line and 2 columns; the index function will return the first line and the first column from this array. To return the second element from the column A, just change to
=index(split(A2,","),1,2)
I think there's no easy solution for this. You're asking for as many combinations of elements as multiple-choice elections have been made. Any function in Google Sheets has its potentials and limitations about how many elements it can express. One very useful formula here is REDUCE. With REDUCE and sequences of elements separated by commas counted with COUNTA, you can stablish this formula:
=QUERY(REDUCE({"Col A","Col B","Email"},SEQUENCE(COUNTA(A2:A)),LAMBDA(z,c,{z;LAMBDA(ax,bx,
REDUCE({"","",""},SEQUENCE(ax),LAMBDA(w,a,
{w;
REDUCE({"","",""},SEQUENCE(bx),LAMBDA(y,b,
{y;INDEX(SPLIT(INDEX(A2:A,c),","),,a),INDEX(SPLIT(INDEX(B2:B,c),","),,b),INDEX(C2:C,c)}
))})))
(COUNTA(SPLIT(INDEX(A2:A,c),",")),COUNTA(SPLIT(INDEX(B2:B,c),",")))})),
"Where Col1 is not null",1)
Since I had to use a "initial value" in every REDUCE, I then used QUERY to filter the empty values:

Display results from filter formula in one row?

I am using a filter formula to output multiple results based on a condition. Each result contains 4 columns of data, each result is in a new row. However, I would like all results to be output one after the other (i.e. in one row). The data of each result takes up 4 columns and in the fifth column, the new result starts with 4 columns.
This is how it looks now:
This is how it should look:
To get this dynamically, follow these steps.
1 - in the sheet where you want the results on cell A2 past this formula.
=TRANSPOSE(FLATTEN(QUERY(Data!A2:D," Select * where A is not null ")))
TRANSPOSE because FLATTEN formula outputs a column.
QUERY to remove blank rows.
FLATTEN to convert all values from one or more ranges into a single column.
2 - to make the header dynamic you need to paste this formula in resault sheet in cell A1.
=SPLIT(REPT(JOIN(";",Data!A1:D1)&";",COUNTA(Data!A2:A)),";")
JOIN the header with ; so you can split it with the SPLIT formula after repeating the header rang n time with the REPT formula, to handel how many times the header repeated just use COUNTA to count how many rows are there.
Input example:
Output example:
You first need to use Flatten() to make it a one dimensional array since the spreadsheet data is in a nested array no matter how many rows/columns you have.
Then from here you can just transpose the data so instead of the data going down the rows it will be transposed to go horizontally to the columns.
Try:
=TRANSPOSE(FLATTEN(A2:D))
To combine it with your formula just replace the Range "A2:D" with your current formula.
Result:
References:
Transpose
Flatten

Combine / merge contents of columns in Google Sheet into a single column based on a condition

I have a spreadsheet with multiple columns. Each column represents a recipe. Each column has a week number (e.g. 2, 3, 4)... Below the week number, we have a variable number of ingredients. I want to be able to merge all the ingredients from a given week into a single column.
Sample Data
I know how to statically merge ranges e.g. {B4:B20;C4:C20} and I can also think of using an if statement to check the week value e.g. =if(B1=2,{B4:B20;C4:C20}) but that's not close to what I want. I need to check the range B1:Z1 for the value of the week and if that value is X (let's say 2) then take the ingredients in B2:B and tack them on to C2:C and so on...
If I'm understanding you correctly, this should do it:
=QUERY(FLATTEN(FILTER(B3:Z,B1:Z1=2)),"WHERE Col1 Is Not Null")
FILTER keeps (i.e., "filters in") only the data in B3:Z where the header is 2.
FLATTEN forms one column (blank cells and all) from the FILTER results.
QUERY keeps only non-blank (i.e., Is Not Null) entries from that single-column list.

Split 1st column values to search two other columns and return single value

I'm attempting to take a CONCATENATEd value in a column (A), SPLIT that value in two and VLOOKUP two columns in another sheet (same file). The issue I'm running into is if there are not two values to SPLIT in the CONCATENATEd cells, I get an error. I can flip my formula to search for two values or one (two values JOINed with : or a single value), but not both.
This is the latest iteration of my formula that's only showing half of the desired results.
=ArrayFormula(VLOOKUP(IF(FIND(":",A3:A),SPLIT(A3:A,":"),A3:A),{materialsData!C2:C,materialsData!D2:D,materialsData!E2:H},4,))
Here is an example sheet of what I'm trying to do.
try:
=ARRAYFORMULA(IFNA(VLOOKUP(A3:A, {IF(materialsData!D2:D<>"",
materialsData!C2:C&":"&materialsData!D2:D, materialsData!C2:C), materialsData!F2:F}, 2, 0)))

VLOOKUP remove spaces when cell is empty

This a simple customer sheet:
A B C D
ID First Middle Last
1 John Doe
2 Jane Maia Doe
And in F1 I put this vlookup code:
=VLOOKUP($G$1;$A$1:$D$3;2;FALSE)&" "&VLOOKUP($G$1;$A$1:$D$3;3;FALSE)&" "&VLOOKUP($G$1;$A$1:$D$3;4;FALSE)
When I lookup ID 2, it's perfect nicely spaced between the vlookups
But when I lookup ID 1 you see 2 spaces between the first and last name, because there is no middle name here.
How can I manage that I always see 1 space between the vlookups?
One way you could achieve the result you're looking for is to simply replace multiple spaces with a single space.
=REGEXREPLACE(JOIN(" ",ARRAYFORMULA(VLOOKUP(G1,A:D,{2,3,4},FALSE))),"\s{2,}"," ")
This formula looks up G1 in your table (A:D). VLOOKUP can be used in an ARRAYFORMULA to efficiently retrieve all of the columns you want in one shot. Your JOIN joins all of the retrieved columns, inserting a space between each value. Finally, your REGEXREPLACE function looks for multiple consecutive spaces and replaces them with a single space.
Alternatively, you could filter the resulting array (i.e. the result of what your VLOOKUP returns). The following formula looks up the array of first, middle, and last name, and then filters out any empty cells before joining the remaining elements with a space.
=JOIN(" ",FILTER(VLOOKUP(I1,A:D,{2,3,4},FALSE),INDIRECT("B"&MATCH(I1,A:A,0)&":D"&MATCH(I1,A:A,0))<>""))
all you need is TRIM fx and:
=ARRAYFORMULA(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IFERROR(
VLOOKUP(G1:G2, A1:D3, {2,3,4}, 0))),,999^99))))

Resources