Generate all possible combinations for 3 Columns in Google Sheets - google-sheets

I have a Google Spreadsheets with four columns.
I need to populate the Column D with all the possible combinations of the values in Columns A and C. Please take a look a the capture to see what I mean.
I found this following formula, but it use for 2 column.but i have 3 column.
i am using this code:
=ArrayFormula(transpose(split(concatenate(rept(A1:A&char(9),counta(B1:B))),char(9)))&" "&transpose(split(rept(concatenate(B1:B&char(9)),counta(A1:A)),char(9))))

use:
=INDEX(FLATTEN(FLATTEN(
FILTER(A2:A; A2:A<>"")&" "&TRANSPOSE(
FILTER(B2:B; B2:B<>"")))&" "&TRANSPOSE(
FILTER(C2:C; C2:C<>""))))

Answer:
You can pass this formula back into itself to get the same result for three columns.
Formula:
=ArrayFormula(transpose(split(concatenate(rept(A1:A&char(9),counta(ArrayFormula(transpose(split(concatenate(rept(B1:B&char(9),counta(C1:C))),char(9)))&" "&transpose(split(rept(concatenate(C1:C&char(9)),counta(B1:B)),char(9))))))),char(9)))&" "&transpose(split(rept(concatenate(ArrayFormula(transpose(split(concatenate(rept(B1:B&char(9),counta(C1:C))),char(9)))&" "&transpose(split(rept(concatenate(C1:C&char(9)),counta(B1:B)),char(9))))&char(9)),counta(A1:A)),char(9))))
This formula replaces the references to B1:B to a formula which transposes all combinations of column B and column C, and transposes all those combinations with those in column A.

Related

ARRAYFORMULA is only populating the first row

I have financial data that I am trying to summarize in a format that can be used by a line chart.
The example spreadsheet is here.
In my source data on the left, I have an entry per Date, Symbol, Account. I need to transform this data so there is a row per Date and a column for each Symbol. I will SUM Total Value regardless of account.
I found a way to pull a unique Date (see H2), and then transpose unique Symbols into columns (see I1).
I also found a way to use SUMIFS to get the aggregation I want (take a look at cell I2), but I can't figure out how to use ARRAYFORMULA to apply this value to all rows in column I.
I know I can drag my formula from I2 down to I3,I4,I.. etc, but this sheet is part of a larger project so I'd like it to auto-populate as dates are added to H.
From what I've read ARRAYFORMULA should apply the formula to multiple rows. What am I missing?
Thanks
use:
=QUERY(A1:F, "select A,sum(F) where A is not null group by A pivot B", 1)
Use formulas like this
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2, $B$2:$B, I$1)))
Add IF(H2:H="",,
Explanation
if the range is empty "" do nothing ,, else Your formula
Your Example
Cells
Formulas
I2
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2:H, $B$2:$B, I$1)))
J2
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2:H, $B$2:$B, J$1)))
K2
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2:H, $B$2:$B, K$1)))

Sum every n columns starting at the 1st column in the range

In Excel/Google Sheets I have found how to sum every N columns on websites such as https://exceljet.net/formula/fixed-value-every-n-columns, but the problem is, from what I can see is that it starts at N column each time. I need something that starts from column 1 and then counts every N columns. like the following:
I need to do this with a formula and not a script.
With Google-Sheets, try:
Formula in M2:
=SUM(QUERY(TRANSPOSE(A2:J2),"Skipping "&L2))
Or, a single dynamic array formula (without dragging):
=INDEX(MMULT(A2:J4*(MOD(COLUMN(A2:J4),L2:L4)=1),SEQUENCE(10,1,1,0)))
Or, more dynamic:
=INDEX(MMULT(A2:J4*(MOD(COLUMN(A2:J4),L2:L4)=1),SEQUENCE(COLUMNS(A2:J4),1,1,0)))
Note: The latter would also work in Excel with slight modifications.
Google sheets formula:
=SUM(FILTER(A2:J2, MOD(A2:J2, L2)=1))
then drag to other cells
or use this array version:
Array version:
=INDEX(TRANSPOSE(MMULT(A2:J4,TRANSPOSE(COLUMN(A2:J4)^0 *
N(MOD(COLUMN(A2:J4), L2:L4)=1)))), ,1)
If you want the cells that were added to be automatically highlighted.
Conditional formatting used on A2:J:
=MOD(COLUMN(), $L2)=1
In M2:
=SUMPRODUCT(A2:J2,N(MOD(SEQUENCE(,COLUMNS(A2:J2),0),L2)=0))
and copied down.
Try this formula on column M:
=SUM((sumif(ArrayFormula(mod((COLUMN(B2:J2)-COLUMN(B2)+1),L2)),0,B2:J2))+A2)
Here's the result on Column M.
Just to break down the code sumif(ArrayFormula(mod((COLUMN(B2:J2)-COLUMN(B2)+1),L2)),0,B2:J2) does the actual calculation with the number of intervals set on Column L but take note that I started at the 2nd column so the range here does not include the first column. The result from this is at the Column O highlighted red as you can see in the screenshot.
At the Column M is the actual solution where I only added the first column by using SUM on top of the previous formula.
I hope my explanation is clear.
Just copy/drag the formula down to each row and it should work.
Reference: How to Sum Every Nth Row or Column in Google Sheets Using SUMIF

Using COUNTIF excluding duplicates in the same column

I'd like a formula to be able to calculate a COUNTIF function, but ignoring duplicate values in the same column. My sheet looks something like this:
For example, if I were to count the number of occurrences of "Grass" it would return a result of 4, ignoring the duplicates in columns C and D.
Link to this spreadsheet - https://docs.google.com/spreadsheets/d/110De2u6mKLT7SOTfaXN6EZVqgD35pEeeog9o5WWn5bs/edit?usp=sharing
There is a new sheet on your sample spreadsheet called MK.Idea. There you will find this formula:
=ARRAYFORMULA(COUNTIF(UNIQUE(SPLIT(FLATTEN(Sheet1!B1:G1&"|"&Sheet1!B2:G),"|")),"Grass"))
That uses UNIQUE and a SPLIT(Flatten technique to isolate the "grass" values that are duplicates in a given column

Is it possible to use two cells of criteria to return a value from a third column?

I have an Google sheets file with two sheets. I'm trying to reference one sheet's text based on two cells of criteria using a formula in the other sheet. I have one sheet that looks similar to this -
And another that looks like this -
I would like to put a formula on the second sheet that basically says - look on the first sheet for the values in columns A and B and return me the value in column C. The tricky part is - the values in the second sheet may be inverted or there may be an instance where only one value is present, like in row 1 in the first sheet. Also the formula should only fill in a value if both columns match. All text combinations in both sheets are unique.
Is it possible to do this with text? Thank you for your help!
Try the following
=ArrayFormula((IFERROR(VLOOKUP(R2:R&S2:S,{O2:O&P2:P,Q2:Q},2,0))&
IFERROR(IF((R2:R<>"")*(S2:S<>""),VLOOKUP(R2:R&S2:S,{P2:P&O2:O,Q2:Q},2,0),""))))
(Do adjust locale and ranges according to your needs)
Functions used:
ArrayFormula
IFERROR
IF
VLOOKUP
If you can use two keys concated in the lookup table as a virtual key (i.e. make a key like "CAT|DOG"), then you can use that to look in the secondary table.
If you can't guarantee the sort order of the two keys in the secondary table, you can use the following technique to "sort" the two keys so you can make a single lookup key that's always in one stable order.
Sample Table
A
B
C
D
E
SortedKey
Cat
Dog
TRUE
Cat
Dog
CatDog
Dog
Cat
FALSE
Cat
Dog
CatDog
Formulas
Sample formulas for row #1.
For column C, use formula: =A1<B1
For column D, use formula: =IF(C1=TRUE, A1, B1)
For column E, use formula: =IF(C1=TRUE, B1, A1)
For SortedKey, use formula: =concat(D1, E1)

Preserving formula of a column when adding new rows in Google Sheets

I have a sheet which looks like this:
The C column is simply the product of column A and B. This formula is replicated in the entirely of column C, from row 2 to 1000.
However, if I add a new row between two existing rows, the formula is no longer there for that specific row. Looking around for solutions, I came across ArrayFormula. I changed my formula to the following:
=ARRAYFORMULA(IF(OR(A2="",B2=""),"",PRODUCT(A2:A, B2:B)))
However, this messed up the result. How can I solve this problem?
ARRAYFORMULA does not understand OR so you need to convert it into 0/1 logic:
=ARRAYFORMULA(IF(((A2:A="")+(B2:B="")), , A2:A*B2:B))

Resources