Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate all possible combinations for Columns in Google SpreadSheets

Tags:

I have a Google SpreadSheets doc with three columns A, B and C.

I need to populate the Column C with all the possible combinations of the values in Columns A and B. Please take a look a the capture to see what I mean.

I found this to be done in Excel, here, but it doesn't work in google spreadsheets.

The formula should be useful even for more columns (e.g.: four instead of two)

Can I do this?

enter image description here

like image 705
JPashs Avatar asked Mar 15 '17 09:03

JPashs


People also ask

How do I get all possible combinations in Google Sheets?

To do this, type the list of numbers into a cell in Google Sheets, and then use the permutation function to create a list of all the possible permutations. The function is =permutation(x,y) where "x" is the original list of numbers and "y" is the number of possible permutations.

How do you generate all possible combinations of one list?

Enter the formula =List1. Expand out the new List1 column and then Close & Load the query to a table. The table will have all the combinations of items from both lists and we saved on making a custom column in List1 and avoided using a merge query altogether!

Is there a lambda function in Google Sheets?

You can't use it. The Google Sheets source code does seem to have a (perhaps partial?) implementation of LAMBDA , but there is no way to use it.


1 Answers

Update 201810

Original formula crashes for a big dataset. I described a way to make cross-join with any size of data here.


Try formula:

=ArrayFormula(transpose(split(rept(concatenate(A2:A&char(9)),counta(B2:B)),char(9)))  &" "&transpose(split(concatenate(rept(B2:B&char(9),counta(A2:A))),char(9)))) 

The result:

car red train red car yellow train yellow car blue train blue 

You may use it again to add another list:

enter image description here

The formula is in cells C2 and E2,

C2 is:

=ArrayFormula(transpose(split(rept(concatenate(A2:A&char(9)),counta(B2:B)),char(9)))&" "&transpose(split(concatenate(rept(B2:B&char(9),counta(A2:A))),char(9))) ) 

and E2 is:

=ArrayFormula(transpose(split(rept(concatenate(C2:C&char(9)),counta(D2:D)),char(9)))&" "&transpose(split(concatenate(rept(D2:D&char(9),counta(C2:C))),char(9))) ) 
like image 66
Max Makhrov Avatar answered Sep 21 '22 23:09

Max Makhrov