Search code examples
arraysgoogle-sheetsmatchflattengoogle-query-language

Google Sheets - How to count streaks of non-zero numbers on one column


I have a wordcount column with various numbers but each cell has a formula. So I'd like to count only the cells that have a non-zero number and stop when it hits a zero. A streak in other words.

100
200
400
100
200
0
200
300
0

the longest streak in this example would be 5. What formula could I use for this? I've tried this already, but it's looking for blank cells not cells with "0" =ArrayFormula(match(TRUE,ISBLANK('Daily Count'!B2:B),0)-1)


Solution

  • try:

    =ARRAYFORMULA(MMULT(SPLIT(FLATTEN(SPLIT(TRIM(QUERY(A1:A,,9^9)), " 0 ", )), " ")*1, 
       SEQUENCE(COLUMNS(SPLIT(FLATTEN(SPLIT(TRIM(QUERY(A1:A,,9^9)), " 0 ", )), " ")), 1, 1, )))
    

    enter image description here

    for longest strike use:

    =INDEX(COLUMNS(SPLIT(FLATTEN(SPLIT(TRIM(QUERY(A1:A,,9^9)), " 0 ", )), " ")))
    

    enter image description here

    to list all strikes:

    =ARRAYFORMULA(LEN(QUERY(SUBSTITUTE(FLATTEN(SPLIT(TRIM(QUERY(
     IF(A1:A<>0, 1, 0),,9^9))&" ", " 0 ", )), " ", ), "where Col1 <> '0'")))
    

    enter image description here