Google Sheets Auto Repeat Using arrayformula() - google-sheets

Based on this sample data, I have this formula which gives me the accurate results with one major problem... It does not auto-populate to new rows
=arrayformula(if(countif(filter(G$2:G,A$2:A=A2,B$2:B=B2),">"&G2)>0,countif(filter(G$2:G,A$2:A=A2,B$2:B=B2),">"&G2),COUNTIF(filter(E$2:E,A$2:A=A2,B$2:B=B2),"Finished")))
I have tried this formula to see if it will auto-populate to the new rows...
=arrayformula(IF(ISNA(A2:A),,if(countif(filter(G$2:G,A$2:A=A2,B$2:B=B2),">"&G2)>0,countif(filter(G$2:G,A$2:A=A2,B$2:B=B2),">"&G2),COUNTIF(filter(E$2:E,A$2:A=A2,B$2:B=B2),"Finished"))))
...the above formula does auto-populate; however, every value is 1 instead of the correct value.
I tried a simple formula which does not do everything the above does but might help troubleshoot. I was under the suspicion that the above formula was only displaying results of the first row over and over. To test I tried this formula...
=arrayformula(IF(ISNA(A2:A),,indirect("g"&ROW(indirect("g2:g"&counta(G2:G))))))
...it turns out that the above formula does display the results from G2 into each row. If I could figure out the reason why, I am sure I could take the concept from the solution to this simple formula and add it to the above more complex one.

Please, try this formula:
=ArrayFormula(if(
mmult(
--(A2:A=TRANSPOSE(A2:A))*
--(B2:B=TRANSPOSE(B2:B))*
--(G2:G<TRANSPOSE(G2:G)),
row(A2:A)^0)>0,
mmult(
--(A2:A=TRANSPOSE(A2:A))*
--(B2:B=TRANSPOSE(B2:B))*
--(G2:G<TRANSPOSE(G2:G)),
row(A2:A)^0),
mmult(
--(A2:A=TRANSPOSE(A2:A))*
--(B2:B=TRANSPOSE(B2:B)),
--(E2:E="Finished"))
)
)
Caution! It works slow, so it's better to delete blank rows in the worksheet. Even better to use this formula in 2 steps. Step 1 formula:
=ArrayFormula(mmult(--(A2:A=TRANSPOSE(A2:A))*
--(B2:B=TRANSPOSE(B2:B))*
--(G2:G<TRANSPOSE(G2:G)),
row(A2:A)^0))
And step 2 formula:
=ArrayFormula(mmult(--(A2:A=TRANSPOSE(A2:A))
*--(B2:B=TRANSPOSE(B2:B)),
--(E2:E="Finished")))
Open ranges overload this formula. It also could work faster if you use:
offset(E2,,,counta(E2:E)) instead of E2:E

Related

Inconsistent results when using arrayformula within Googlesheets

I have a formula that will calculate the working hours between two dates.
It works perfectly well when referring a few lines and ranges. As the data in this sheet is going to be automatically added to over time I wanted to use arrayformula to automatically calculate the duration for these new rows.
However when used within arrayforumla the values are unpredictably different.
I can't make any sense of why it's different.
Below is an example;
https://docs.google.com/spreadsheets/d/1cWZwpnkp2MPyM-EppjOYHRcMEcblscU70R62uf691Xw/edit#gid=0
Any suggestions as to why the formula is behaving differently would be greatly appreciated!
Edit
For posterity;
Formula that worked when inline (COL C);
=TEXT((NETWORKDAYS.INTL(A2,B2,1,F$7:F$9)-1)*($G$2-$F$2)+IF(NETWORKDAYS.INTL(B2,B2,1,F$7:F$9),MEDIAN(MOD(B2,1),$F$2,$G$2),$G$2)-MEDIAN(NETWORKDAYS.INTL(A2,A2,1,F$7:F$9)*MOD(A2,1),$F$2,$G$2),"[hh]:mm:ss")
Forumla that didn't work in arrayformula (COL D);
=ARRAYFORMULA(
IF(ROW(B:B)=1, "Array Formula Duration",
IF(ISBLANK(B:B),"",
(NETWORKDAYS.INTL(A2:A,B2:B,1,F$7:F$9)-1)*($G$2-$F$2)+IF(NETWORKDAYS.INTL(B2:B,B2:B,1,F$7:F$9),MEDIAN(MOD(B2:B,1),$F$2,$G$2),$G$2)-MEDIAN(NETWORKDAYS.INTL(A2:A,A2:A,1,F$7:F$9)*MOD(A2:A,1),$F$2,$G$2)
)
)
)
Forumla that sorted it (COL E);
={"Array Formula Duration";map(A2:A,B2:B,lambda(a,b,if(b="",,TEXT((NETWORKDAYS.INTL(a,b,1,F7:F9)-1)*(G2-F2)+IF(NETWORKDAYS.INTL(b,b,1,F7:F9),MEDIAN(MOD(b,1),F2,G2),G2)-MEDIAN(NETWORKDAYS.INTL(a,a,1,F7:F9)*MOD(a,1),F2,G2),"[hh]:mm:ss"))))}
You may try:
={"Array Formula Duration";map(A2:A,B2:B,lambda(a,b,if(b="",,TEXT((NETWORKDAYS.INTL(a,b,1,F7:F9)-1)*(G2-F2)+IF(NETWORKDAYS.INTL(b,b,1,F7:F9),MEDIAN(MOD(b,1),F2,G2),G2)-MEDIAN(NETWORKDAYS.INTL(a,a,1,F7:F9)*MOD(a,1),F2,G2),"[hh]:mm:ss"))))}

Why is it faster in Google Sheets to split this formula into two steps?

Edit: I initially diagnosed this problem totally wrong, so the question is entirely rewritten to reflect new understanding.
The problem can be reproduced using a Google spreadsheet with one sheet that contains one header row and a significant number of additional rows (let’s say 5,000).
I wanted column A to increment by 1, starting with A2, as long as the adjacent cell in B was not blank. I used this formula in A1:
={"SKU"; arrayformula(if($B2:$B="","",text(row($A2:$A),"000000")))}
This formula worked but caused extremely significant lag.
In one of my attempts to resolve the issue, I added a helper column before column A and split my formula into two formulas to see which function was causing the lag:
Cell A1: ={"SKU (helper)"; arrayformula(if($C2:$C="","",row($A2:$A)))}
Cell B1: ={"SKU"; arrayformula(if($C2:$C="","",text($A2:$A,"000000")))}
To my surprise, the answer was neither. The lag was completely eliminated. What is the reason? And is it possible to eliminate the lag without the need for a helper column?
use:
={"SKU"; SEQUENCE(ROWS(A:A)-5344; 1; 5344)}
update:
={"SKU"; INDEX(TEXT(SEQUENCE(COUNTA(B2:B)), "000000"))}
if you have empty cells in between use:
=LAMBDA(x, {"SKU"; INDEX(IF(x="",,
TEXT(COUNTIFS(x, "<>", ROW(x), "<="&ROW(x)), "000000")))})
(B2:INDEX(B:B, MAX((B:B<>"")*ROW(B:B))))

Arrayformula calculation with row()

=OFFSET(INDIRECT($A$4),5+row()-2,4,1,1)
I'm looking to repeat the above formula in all row within the same column, so the calculation will change based on the row(). I tried to put it like below, column H is where the result will be going. But the row() is not following, all result are just using the formula row instead. The $A$4 is a where I'm using to input the range name.
=ARRAYFORMULA(D3:D&OFFSET(INDIRECT($A$4),5+row()-3,4,1,1))
I know I can drag it down, but automatic is always better. Before I'm over complicating the formula, is there a simple solution to make this work?
Updates:
Sample sheet link
https://docs.google.com/spreadsheets/d/1di8a9I0Fv-vuQFDNfV3sPsnWbDuBWTNDYlvUAI5pOCk/edit?usp=sharing
I've added a new sheet ("Erik Help") with two formulas, each of which will return the same results:
=IFERROR(OFFSET(INDIRECT("CLASS_"&$A$4),5+row()-3,4,COUNT(INDEX(INDIRECT("CLASS_"&$A$4))),1),"NO DATA")
-or-
=IFERROR(QUERY({INDEX(INDIRECT("CLASS_"&$A$4),,5)},"Select * WHERE Col1 Is Not Null",0),"NO DATA")
The D3 formula is better, because it is not dependent on the row where the formula is found (i.e., it can be placed anywhere and get the same result). It is "cleaner" (i.e., it has no redundancy of the INDIRECT clause). It's also easier to read and edit if necessary.

Using ArrayFormula to countif multiple columns and output the sum on each row

I've created a dense formula using COUNTIF and VLOOKUP across multiple columns, and I've currently set it up so that I have to copy/paste every formula. But I'd really like to be able to use arrayformula to write the formula once for each of the columns below, and have it render across at least 1000 rows. Is there a trick to writing this?
Column 1
=COUNTIF(VLOOKUP(F3,WarLinesValuesTypes,2,FALSE),"*Defense*")
+COUNTIF(VLOOKUP(G3,WarLinesValuesTypes,2,FALSE),"*Defense*")
+COUNTIF(VLOOKUP(H3,WarLinesValuesTypes,2,FALSE),"*Defense*")
+COUNTIF(VLOOKUP(I3,WarLinesValuesTypes,2,FALSE),"*Defense*")
+COUNTIF(VLOOKUP(J3,WarLinesValuesTypes,2,FALSE),"*Defense*")
Column 2
=COUNTIF(VLOOKUP(F3,WarLinesValuesTypes,2,FALSE),"*Offense*")
+COUNTIF(VLOOKUP(G3,WarLinesValuesTypes,2,FALSE),"*Offense*")
+COUNTIF(VLOOKUP(H3,WarLinesValuesTypes,2,FALSE),"*Offense*")
+COUNTIF(VLOOKUP(I3,WarLinesValuesTypes,2,FALSE),"*Offense*")
+COUNTIF(VLOOKUP(J3,WarLinesValuesTypes,2,FALSE),"*Offense*")
I also saw the StackOverflow post here (iterate row by row COUNTIF using ArrayFormula on Google Sheets), and tried to emulate it, but I am not getting it right. My sheet (https://docs.google.com/spreadsheets/d/1JR_eYvmf6YgW0CfzHrSR6dxF8RmyWhSKhqSpRnnbipk/edit?usp=sharing) shows the complex formula working correctly for both columns, as well as my failed attempt for 1 of those columns as a trial using arrayformula, mmult, and sign.
try:
=ARRAYFORMULA(MMULT(IF(REGEXMATCH(IFNA(VLOOKUP(
INDIRECT("F2:J"&COUNTA(B2:B)+1), 'Flattened Standard War Lines'!A:B, 2, 0)),
".*Offense.*"), 1, 0), TRANSPOSE(COLUMN(F:J))^0))

Google Sheet - Transform two columns into one column using arrayformula (more than 50,000 characters)

I'm using Google Sheets and looking for an arrayformula that able to take a list in two columns and arrange it alternately in one column. The sheet contains about 5,000 rows, each row has more than 35 characters.
I tried this:
=transpose(split(join(" ", query(transpose(B5:C),,50000)), " "))
But then I got this message:
Please take a look at the sheet here:
https://docs.google.com/spreadsheets/d/11T1Roj1trviOSiiTZS292-4l3oODid7KLi9oGz3Z66o/edit#gid=0
Assuming your 2 columns are A and B, this formula will "interlace" them:
=query(
sort(
{arrayformula({row(A1:A3)*2, A1:A3});
arrayformula({row(B1:B3)*2+1, B1:B3})}
),
"select Col2")
Explanation, unwrapping the formula from the inside:
Each value gets a unique number, based on its row number times 2 (+1 for the 2nd column)
Everything is sorted based on this number
Only the 2nd column is extracted for the result.
There is a function for this called FLATTEN().
This works perfectly as a general solution since it takes an array of any size and outputs the items in the order they appear left-right-top-down (See here).
It can be combined with TRANSPOSE() to accomplish the same thing but in the horizontal case, and if needed blank cells can be omitted with FILTER().
EDIT:
My sincere apologies, I did not read the question carefully enough. My response is incorrect.
This should work:
={B5:B12;C5:C12}
just be careful to NOT change it to
={B5:B;C5:C}
This will start an infinite loop where the spreadsheet will increase the amount of rows in the spreadsheet to allow this output column to expand, but in doing so increases the length of the 2 input columns, meaning the length of the output column increases even more, so the spreadsheet tries adding more rows, etc, etc. It'll make your sheet crash your browser or something each time you try to open it.
In Row5:
=ArrayFormula(offset(B$5,INT((row()-5)/2),iseven(row())))
Would need to be copied down however.

Resources