Using COUNTIF with the Array Formula - google-sheets

I'm trying to get a simple COUNTIF formula to combine with the Array Formula in order to count the number of zeros in each row. The original basic formula would look like this:
=COUNTIF($C6:$CD6,"0")
I would want the formula to go down so that it counts the number of zeros in each row.
Please could someone tell me which formula to use? I've tried countless versions with Arrayformula and none of them are working the way I want them to.
Thank you.
EDIT/ADDITION: I've used the formula below to count the zeros in a row.
=ArrayFormula(IF(ISBLANK($A6:$A),,INDEX(MMULT(1*(IF($CH6:$DA="", "×", $CH6:$DA)=0), SEQUENCE(COLUMNS($CH6:$DA), 1, 1, )))))
I have used another formula (below) to sum the amounts in certain columns (which are categorised).
=ArrayFormula(IF(ISBLANK($A5:$A),,SUMIF(IF(($C$2:$CD$2)="Alphabet",ROW($C$5:$C)),ROW($C5:$CD),$C5:$CD)))
In addition to this, I've realised it would be helpful to be able to count the zeros in those same columns. That means I would kind of combine the two formulas above.
Is there a way to do this, please?

try:
=INDEX(MMULT(1*(IF(A1:D4="", "×", A1:D4)=0), SEQUENCE(COLUMNS(A1:D1), 1, 1, )))

Related

Repeating a value on the basis of count provided in another cell - Google Sheets

I'm trying to write a formula where I can generate a number n number of times where n can be the input provided by the user.
=ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(QUERY(
REPT($D2&",", $E2), ), ","))))
Ideal output
Here D2 is the value to be repeated and E2 is the number of times.
So instead of manually using this formula after each last repeated value to generate the next set of repeated values, I want to print the values in one go. I'll be really grateful, if anyone could please provide a way around to do the same. Thanks in advance.
Try this
=ARRAY_CONSTRAIN(arrayformula(query(flatten(split(rept("|"&D2:D,E2:E),"|")),"select * where Col1 is not null")),SUM(E2:E),1)
explanation
the core of the formula is
=arrayformula(iferror(split(rept("|"&D2:D,E2:E),"|")))
then, apply flatten with a limitation of rows (ARRAY_CONSTRAIN) equal to the sum of column E, and query only the rows that are not null
Try the below formula:
=ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(QUERY(
REPT(D2:D&",", E2:E), ,999^99), ","))))

Creating a Automate Formula Which Repeats the Values according to Criteria (Dynamic Formula)

I have been finding a way where i can make this thing automate/Dynamic.
Below Sheet have two columns where Col"B" has such values like
KPM11
KPM22
KPM33
KPM44
KPM55
KPM66
KPM77
KPM88
KPM99
KPM100
KPM110
I want to repeat these values until next is start as i have settled it manually in Col"A".
Looking for a way/formula which makes it automate so i can drag it till the range or could be array.
Any help will be highly appreciated.
I have tried to create a formula but its out of my experience.
https://docs.google.com/spreadsheets/d/1IsCl9fcBMcLYiGLCmnJrk5a06bRCDigm7w2gGjqkCrE/edit#gid=0
try:
=ARRAYFORMULA(IF(B2:B="",, VLOOKUP(ROW(A2:A),
IF(REGEXMATCH(B2:B, TEXTJOIN("|", 1, H:H)), {ROW(A2:A), B2:B}), 2, 1)))

Array formula for a rolling AVERAGEIF formula?

I have a working formula that I need to drag to autofill down a column and want to make it into an array formula:
=AVERAGEIF(INDIRECT("A2:A"&ROW()), ">=0",INDIRECT("A2:A"&ROW()))
So if you put this formula in column B it will take the values in column A and continually average them going down, skipping any values that are less than 0. Here is an example screenshot: https://i.imgur.com/nRq8hAH.png
How can I make an array formula for this?
This formula comes close but I couldn't figure out how to add the ">=0" conditional:
=ArrayFormula(IF(LEN(A2:A),SUMIF(ROW(A2:A),"<="&ROW(A2:A),A2:A)/COUNTIF(ROW(A2:A),"<="&ROW(A2:A)),))
Lambda Update
There is no longer any need to use ArrayFormula for this.
=MAP(SEQUENCE(COUNTA(A2:A)),
LAMBDA(rowOff,
AVERAGEIF(OFFSET(A2,0,0,rowOff),">=0"))
)
How?
For each element rowOff in 1..# items in column:
Use AverageIf to get the average of everything starting at the top taking rowOff rows, excluding everything >=0
Old solution
Here's a single formula that can go into B2 (no need to drag), but it's fairly complicated:
=ArrayFormula(IFERROR(IF(LEN(A2:A),MMULT(TRANSPOSE((SEQUENCE(COUNTA(A2:A),1,2)<=TRANSPOSE(SEQUENCE(COUNTA(A2:A),1,2)))*FILTER(A2:A,LEN(A2:A))),--(FILTER(A2:A,LEN(A2:A))>0))/COUNTIFS(SEQUENCE(COUNTA(A2:A)),"<="&SEQUENCE(COUNTA(A2:A)),FILTER(A2:A,LEN(A2:A)),">=0"),"")))
Readable:
=ArrayFormula(IFERROR(
IF(
LEN(A2:A),
MMULT(
TRANSPOSE(
(SEQUENCE(COUNTA(A2:A),1,2)<=
TRANSPOSE(SEQUENCE(COUNTA(A2:A),1,2))
)*FILTER(A2:A,LEN(A2:A))
),
--(FILTER(A2:A,LEN(A2:A))>0)
)/
COUNTIFS(
SEQUENCE(COUNTA(A2:A)),
"<="&SEQUENCE(COUNTA(A2:A)),
FILTER(A2:A,LEN(A2:A)),
">=0"
),
""
)
))
How?
We can achieve a running sum using MMULT on a Lower Triangular Matrix of size COUNTA(A2:A) of all 1's and all non blanks of A2:A, which we filter out if the number is negative. In this case, it produces {2;2;6;6;6;6}.
The COUNTIFS() produces an array of the number of elements we want to divide by. Here, it's {1;1;2;2;3;4}
Then ignore any blanks at the with IF.
Blank out any errors with IFERROR. (#DIV/0! errors can happen if the leading numbers are negative.)
Perhaps, this formula can help:
=ARRAYFORMULA(AVERAGE(IF($A$2:A2>=0,$A$2:A2,"")))

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 Sheets Auto Repeat Using arrayformula()

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

Resources