Working with rows based on mathematical combinatio

2020-08-01 07:31发布

问题:

Is there a way to get a mathematical combination of all items in a row, and do math and such to generate things on another page based on said combination? e.g. {1, 2, 3} -> {1, 2, 3, 1 and 2, 1 and 3, 2 and 3, 1 and 2 and 3}

i need to sum the values of column b and c, separately, based on the rows corresponding to the items marked in the array of combinations.

screenshots for clarity. I need to use Sheet 1, which is all manual entry, to generate Sheet 2 in its entirety.

If need be it can be all in the same sheet instead of 2, but separate would be preferred for legibility

Edit: NB: I do realize this will probably take 3 or more formulas. Also, thanks in advance!

回答1:

Question #1. Get all possible combinations

This formula will produce all possible combinations from strings in Sheet1 column A:A:

=TRANSPOSE(SPLIT(TEXTJOIN("",1,ArrayFormula(IF(REGEXEXTRACT(DEC2BIN(ROW(INDIRECT("a1:a"&SUM(FACT(COUNTA(Sheet1!A:A))/(FACT(ROW(INDIRECT("a1:a"&COUNTA(Sheet1!A:A))))*FACT(COUNTA(Sheet1!A:A)-ROW(INDIRECT("a1:a"&COUNTA(Sheet1!A:A)))))))),COUNTA(Sheet1!A:A))&"1",REPT("(.)",COUNTA(Sheet1!A:A)+1))*1,TRANSPOSE({FILTER(Sheet1!A:A,Sheet1!A:A<>"")&",";"#"}),""))),",#",0))

will produce:

Item3
Item2
Item2,Item3
Item1
Item1,Item3
Item1,Item2
Item1,Item2,Item3

If you add another item in sheet1, formula would adjust.

See math background here:

http://mymathforum.com/advanced-statistics/2567-non-repeating-combinations.html

Question #2. Sum by joined strings

In Sheet2 A:A we have joined strings. Paste this formula in B1 Sheet2:

=QUERY(QUERY({TRANSPOSE(SPLIT(JOIN("",ArrayFormula(REPT(row(OFFSET(A1,,,COUNTA(A:A)))&",",LEN(OFFSET(A1,,,COUNTA(A:A)))-LEN(SUBSTITUTE(OFFSET(A1,,,COUNTA(A:A)),",",""))+1))),",")),ArrayFormula(SUMIF(Sheet1!A:A,TRANSPOSE(SPLIT(TEXTJOIN(",",1,A:A),",")),Sheet1!B:B))},"select Col1, sum(Col2) group by Col1 label sum(Col2) ''"),"select Col2")

The result is:

Item3               1000
Item2               750
Item2,Item3         1750
Item1               500
Item1,Item3         1500
Item1,Item2         1250
Item1,Item2,Item3   2250