COUNTA in current row in an array formula (Google Sheets) - arrays

I have a Google sheet with fixed number of columns and dynamic rows.
I like to use countA to count fields with a value (non-blank) in the current row.
I found a formula here but don't understand it, neither can get it to work.
ArrayFormula(MMULT( LEN(A1:E)>0 ; TRANSPOSE(SIGN(COLUMN(A1:E1)))))
Sheet gives me error: "Function MMULT parameter 1 expects number values. But 'TRUE' is a boolean and cannot be coerced to a number."

The formula should work if you convert the booleans (true or false) returned by LEN(A1:E)>0 to numbers (1 or 0), as Barry already mentioned. This can be done quite easily by wrapping the output of the LEN()-function in an N-function or by preceding it with '--'. So, assuming your data starts in row 2, see if this works:
=ArrayFormula(MMULT( --(LEN(A2:E)>0) , TRANSPOSE(COLUMN(A2:E2)^0)))
An alternative way would be to use COUNTIF()
=ArrayFormula(COUNTIF(IF(A2:E<>"", row(A2:A),),row(A2:A)))
and probably even a combination should work:
=ArrayFormula(MMULT( --(A2:E<>"") , TRANSPOSE(COLUMN(A2:E1)^0)))
If you also want to include a header row, try:
=ArrayFormula(if(row(A:A)=1, "Header", MMULT( --(LEN(A:E)>0) , TRANSPOSE(COLUMN(A1:E1)^0))))
or
=ArrayFormula(if(row(A:A)=1, "Header", MMULT( --(A:E<>"") , TRANSPOSE(COLUMN(A1:E1)^0))))
or
=ArrayFormula(if(row(A:A)=1, "Header", COUNTIF(IF(not(isblank(A:E)), row(A:A),),row(A:A))))
EDIT: (after new question in comments)
If you want to sum the values, you can do that with MMULT() too:
=ArrayFormula(if(row(A:A)=1, "Header", MMULT(if(A1:E<>"", A1:E,0), transpose(column(A1:E1)^0))))
or using sumif:
=ArrayFormula(if(row(A:A)=1, "Header", sumif(IF(COLUMN(A1:E1),ROW(A1:A)),ROW(A1:A),A1:E)))
NOTE: if you want to limit the output to let's say the last row that has values in col A, try:
=ArrayFormula(if(row(A:A)=1, "Header", IF(LEN(A1:A), MMULT(if(A1:E<>"", A1:E,0), transpose(column(A1:E1)^0)),)))
or, again with sumif()
=ArrayFormula(if(row(A:A)=1, "Header", if(len(A1:A), sumif(IF(COLUMN(A1:E1),ROW(A1:A)),ROW(A1:A),A1:E),)))

That formula seems a little complex for your explanation, can't you just use this formula copied down
=COUNTA(A1:E1)
...but specifically addressing your question, you need to change this part
LEN(A1:E)>0
...so that it returns numbers - try
IF(LEN(A1:E)>0;1;0)

Related

Spreadsheet Formula read the wrong Value

I run the Vlookup Formula with data like this in he Spreadsheet:
A65 B65 C65 D65 E65 F65 G65
AGR 1 1 Penjualan 12/12/2022 Makanan 15,500
I used formula :
=IFNA(Vlookup(A65,A:D,4,0),"")
should returns the value as "Penjualan" or "" if nothing.
but it returns other value.
your formula is correct unless there is one more AGR in range A1:A64. try:
=IFNA(VLOOKUP(A65, A65:D65, 4, 0))
to see that its working properly

google sheet : How to vlookup by matching value in between max and min?

I have 2 sheets like this :
In that 2nd sheet, i want to lookup the id (S/M/L/XL) by checking if value is in between the Min value and Max value. So the goal is to have something like that 2nd sheet where in 'level' column, i need to put a formula so it will check the value is between what range so then it will retrieve the correct 'id' from 1st sheet.
the rule is if the value is >= minvalue and < max value
How can i do this ?
Thanks
use:
=INDEX(IF(A9:A="",,VLOOKUP(A9:A, {C2:C5, A2:A5}, 2, 1)))
Your first table, has overlapping values, so I suggest you think better about the rules you want to apply.
For example, 1, according your table can match both "S" and "M" , same for 3, which can be "M" or "L".
Once you have resolved that, you can use the QUERY function.
Example:
=QUERY($A$2:$D$5,
"select A,D where C<="&A2&" AND D >="&A2&" ORDER BY D DESC LIMIT 1 ")
Working solution can be found here:
https://docs.google.com/spreadsheets/d/1oLVwQqihT_df2y_ZQnfx7By77HnKSFz0bcbOzMuWqOM/edit?usp=sharing
Rather than have min and max columns, you could just use one column to list incremental values that determine the next size, and use vlookup() with a sort option of true - this avoids overlapping values:
=arrayformula({"level";if(A2:A<>"",VLOOKUP(A2:A,{Source!C:C,Source!A:A},2,1),)})

How can I use an array as input for FILTER function in Google Spreadsheet?

So this might be trivial, but it's kinda hard to ask. I'd like to FILTER a range based other FILTER results.
I'll try to explain from inside out (related to image below):
I use filter to find all names for given id (the results are joined in column B). This works fine and returns an array of values. This is the inner FILTER.
I want to use this array of names to find all values for them using another outer FILTER.
In other words: Find maximum value for all names for given id.
Here is what I've figured:
=MAX(FILTER(J:J, CONTAINS???(FILTER(G:G, F:F = A2), I:I)))
^--- imaginary function returning TRUE for every value in I
that is contained in the array
=MAX(FILTER(J:J, I:I = FILTER(G:G, F:F = A2)))
^--- equal does not work here when filter returns more than 1 value
=MAX(FILTER(J:J, REGEXMATCH(JOIN(",", FILTER(G:G, F:F = A2)), I:I)))
^--- this approach WORKS but is ineffective and slow on 10k+ cells
https://docs.google.com/spreadsheets/d/1k5lOUYMLebkmU7X2SLmzWGiDAVR3u3CSAF3dYZ_VnKE
I hope to find better CONTAINS function then the REGEXMATCH/JOIN combo, or to do the task using other approach.
try this in A2 cell (after you delete everything in A2:C range):
=SORTN(SORT({INDIRECT("F2:F"&COUNTA(F2:F)+1),
TRANSPOSE(SUBSTITUTE(TRIM(QUERY(QUERY(QUERY({F2:G},
"select max(Col2) group by Col2 pivot Col1"), "offset 1"),,999^99)), " ", ",")),
VLOOKUP(INDIRECT("G2:G"&COUNTA(F2:F)+1), I2:J, 2, 0)}, 1, 1, 3, 0), 999^99, 2, 1, 1)

Array formula with MATCH gives no result when source table has only one row

The following array formula results in #N/A when the source table has one row, but will result in 1 (which is OK) when the table has more than 1 row (even if there are two rows of which one is empty).
As a workaround, I added (in the Power Query query) an empty row to the table concerned (tblRCGRouteMasterCoProduct).
How to solve this?
=MATCH(1;(tblRCGRouteMasterCoProduct[RoutingCropGroupCode]="BEA") * (tblRCGRouteMasterCoProduct[RouteName]= "Sample Cleaning (2)") * (tblRCGRouteMasterCoProduct[LineNumber]= 10);0)
NOTE: In my formulas below, don't forget to change the commas , to semicolons ; for your locale.
The problem seems to be that the second argument of the MATCH function needs to be an array. For example:
`=MATCH(1,1,0) --> #N/A
but
=MATCH(1,{1},0) --> 1
So one way around it would be to use a different formula for a one-row data body range.
=IF(ROWS(tblRCGRouteMasterCoProduct[#Data])=1,IFERROR(1/(tblRCGRouteMasterCoProduct[RoutingCropGroupCode]="BEA") * (tblRCGRouteMasterCoProduct[RouteName]= "Sample Cleaning (2)") * (tblRCGRouteMasterCoProduct[LineNumber]= 10),NA()),MATCH(1,(tblRCGRouteMasterCoProduct[RoutingCropGroupCode]="BEA") * (tblRCGRouteMasterCoProduct[RouteName]= "Sample Cleaning (2)") * (tblRCGRouteMasterCoProduct[LineNumber]= 10),0))
Another method would be to ensure that even a single cell will be an array. As pointed out by #JvdV, this can be done in the simplified fashion with
=MATCH(1,INDEX(1,),0)
which, for your formula, would be:
=MATCH(1,INDEX((tblRCGRouteMasterCoProduct[RoutingCropGroupCode]="BEA") * (tblRCGRouteMasterCoProduct[RouteName]= "Sample Cleaning (2)") * (tblRCGRouteMasterCoProduct[LineNumber]= 10),0),0)

SUM and COUNTIFS with multiple arrays?

I am trying to obtain a count of rows where specific criteria are met. I have searched on SO for an answer to this, but have been unable to locate a solution thus far (NOTE: I have found solutions to using SUM and COUNTIFS with one array, but not multiple).
Here is the formula I am using:
=SUM(COUNTIFS('Sheet1'!$AA:$AA,"<="&$B1,'Sheet1'!$AE:$AE,"="&"",'Sheet1'!$Q:$Q,"<>"&{"Value1","Value2","Value3","Value4"},'Sheet1'!$S:$S,"<>"&{"ValueA","Value B","ValueC","ValueD","ValueE"}))
The data is customer case data. Cell B1 is a week ending date. Column AA in Sheet 1 is the Case Open Date, and Column AE is the Case Closed Date. Column Q and S are filter criteria.
The expected count returned is a count of cases that were opened during the current week or earlier (Criteria 1) that are still open (Criteria 2) where the case Category (Column Q) is not one of the values in Array 1 (Criteria 3) and the case Type is not one of the values in Array 2 (Criteria 4). The result being returned is higher than expected (this was validated by do a manual filtering on the data set using these same criteria).
My guess is that double counting is going on, since the formula returns 1,828 cases for one week tested but manually filtered validation shows 1,241 (the expected result). Any thoughts or ideas on this is much appreciated.
The problem is that the arrayed count against the Value1, Value2, Value3, Value4 is being treated as an OR condition, not an AND condition. Even if Q2 has Value1 in it, it doesn't have Value2, Value3 or Value4 so the count is coming back as true. You need to make sure that Q2 has none of Value1, Value2, Value3, Value4 in it. The same goes for column S and ValueA, ValueB, ValueC, ValueD. This OR behavior works well when you are trying to see in the values are included but not when they are intended to be excluded.
    
An old-fashioned SUMPRODUCT function can handle the OR conditions.
        
The formula with array constants in B2 is,
=SUMPRODUCT((Sheet1!$AA$2:$AA$999<=$B1)*SIGN(Sheet1!$AA$2:$AA$999)*(Sheet1!$AE$2:$AE$999="")*ISERROR(MATCH(Sheet1!$Q$2:$Q$999, {"Value1","Value2","Value3","Value4"}, 0))*ISERROR(MATCH(Sheet1!$S$2:$S$999, {"ValueA","Value B","ValueC","ValueD","ValueE"}, 0)))
The alternate formula in B3 using the values from the ranges in D4:D7 and E4:E8 is,
=SUMPRODUCT((Sheet1!$AA$2:$AA$999<=$B1)*SIGN(Sheet1!$AA$2:$AA$999)*(Sheet1!$AE$2:$AE$999="")*ISERROR(MATCH(Sheet1!$Q$2:$Q$999, $D$4:$D$7, 0))*ISERROR(MATCH(Sheet1!$S$2:$S$999, $E$4:$E$8, 0)))

Resources