Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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."

like image 985
Ricardo Avatar asked Dec 24 '22 19:12

Ricardo


1 Answers

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),)))
like image 156
JPV Avatar answered Jan 13 '23 13:01

JPV