Count all cells after the first non zero value found - Excel? - excel

I have this example:
I need a formula to count all the numbers after the first aparition of a non zero value.
The Values range is A1:H4. But i need count for every line A1:H1; A2:H2...A4:H4.
0 0 7 2 0 0 0 9 - result of numbers counted 6
5 0 4 0 2 0 0 0 - result of numbers counted 8
0 0 0 0 0 0 7 0 - result of numbers counted 2
0 0 0 0 0 0 0 4 - result of numbers counted 1
Thank you!
c

If the values are in A1:H1, array formula**:
=COUNT(H1:INDEX(1:1,MATCH("",T(1/A1:H1),0)))
To take an example: with A1:H1 containing:
0 0 7 2 0 0 0 9
the reciprocation:
1/A1:H1
will return:
{#DIV/0!,#DIV/0!,0.142857142857143,0.5,#DIV/0!,#DIV/0!,#DIV/0!,0.111111111111111}
Applying the T function to this array returns:
{#DIV/0!,#DIV/0!,"","",#DIV/0!,#DIV/0!,#DIV/0!,""}
i.e. all non-zeroes in the original range have now been reduced to the null string ""; all zeroes to an error.
We can now perform an exact match for the null string, such that:
MATCH("",T(1/A1:H1),0)
which is:
MATCH("",{#DIV/0!,#DIV/0!,"","",#DIV/0!,#DIV/0!,#DIV/0!,""},0)
gives:
3
And so:
INDEX(1:1,MATCH("",T(1/A1:H1),0))
which is:
INDEX(1:1,3)
returns a range reference to the cell:
C1
and so, finally:
COUNT(H1:INDEX(1:1,MATCH("",T(1/A1:H1),0)))
which is:
COUNT(H1:C1)
in which Excel will correct the range reference to:
COUNT(C1:H1)
is:
6
as required.
Regards
**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

=LEN(RIGHT(A1,FIND("1",A1&"1")))

Related

Calculate current streak in Excel Row with Conditions

I have a list of numbers (ex. 5, 7, 10, 11, etc.) and 0s in excel rows from D2:BH2, and I want to calculate the longest streak of 0s in each row (with 2 conditions).
The first condition is to ignore streaks that start the row with 0
Ex. (0 0 0 1 5 6 0 0 1) -> this would have a longest streak of 2 instead of 3 due to the first condition
Ex. ('1 0 0 0 1 5 6 0 0 1') -> this would have a longest streak of '3'
The second condition is to ignore streaks that end the row with 0
Ex. (0 1 5 6 0 0 1 0 0 0 0 ) -> this would have a longest streak of 2 instead of 4 due to the second condition.
Ex. (' 0 1 5 6 0 0 1 0 0 0 0 1') -> this would have a longest streak of '4'
Is there a simple way of calculating the streak of 0s based on these two conditions for each row (in one cell formula)?
Currently I'm using a formula:
=MAX(FREQUENCY(IF(D2:BH2=0,COLUMN(D2:BH2)),IF(D2:BH2=0,0,COLUMN(D2:BH2)
This calculates the longest streak; however, does not take into account the two conditions.
Based on this answer where we could trim only leading/trailing spaces, you could try:
Formula in L1:
=BYROW(A1:J4,LAMBDA(a,LET(x,CONCAT(SIGN(a)),y,TEXTSPLIT(x,0,,1),IFERROR(MAX(LEN(DROP(DROP(TEXTSPLIT(0&x&0,,y),1),-1))),0))))
This also seems to work if you modify the frequency formula slightly so that the counts of all leading and trailing zeroes are gathered into the first and last cells of the Frequency array then drop those cells:
=MAX(DROP(DROP(FREQUENCY(IF(A1:J1=0,COLUMN(A1:J1)),IF(A1:J1<>0,COLUMN(A1:J1))),1),-1))
If you have all zeroes or only one non-zero value it will error but the correct answer should be zero so updated formula should be:
=IFERROR(MAX(DROP(DROP(FREQUENCY(IF(A1:J1=0,COLUMN(A1:J1)),IF(A1:J1<>0,COLUMN(A1:J1))),1),-1)),0)
Of course you can byrow it:
=BYROW(A1:J6,LAMBDA(r,IFERROR(MAX(DROP(DROP(FREQUENCY(IF(r=0,COLUMN(r)),IF(r<>0,COLUMN(r))),1),-1)),0)))

excel extendable formula for conditional sums over multiple columns

I have an arbitrary number of columns, one for each period a course is offered, in chronological order, and an arbitrary number of rows, one for each unique participant. The values are '1' for participation in that month, '0' for non-participation.
Fall2019 Spring2019 Fall2018 Spring2018 Fall2017
1 1 0 1 0
0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
0 1 0 1 1
0 1 1 0 0
0 1 1 0 0
0 1 1 0 0
1 0 0 0 0
I would like to take a sum, at the bottom of each column, for how many participants were first time attendees that period, i.e. the sum of '1's where all values in the row to the right of that '1', are '0'.
In the given example set, Spring2018 should sum to 1, Fall2018 should sum to 3.
Something like the formula below will work for 'Spring2018' when there is just one previous column to compare:
=SUMPRODUCT((D2:D9)*(E2:E9=0))
But this formula cannot be 'autofilled' or extended across multiple columns... i.e. none of these variations work:
=SUMPRODUCT((C2:C9)*(D2:$E9=0))
=SUMPRODUCT((C2:C9)*(SUM(D2:$E9)=0))
=SUMPRODUCT((C2:C9)*(SUMIF(D2:$E9,"0")))
And while it will work, I do NOT want to have to manually create extended versions of this formula e.g.
=SUMPRODUCT((C2:C9)*(D2:D9+E2:E9=0))
=SUMPRODUCT((B2:B9)*(C2:C9+D2:D9+E2:E9=0))
... and so on
I have tried several variations on arrayformula, sumproduct, and sumif, but I'm really stuck. Any assistance is appreciated.
use this array formula:
=SUM(A2:A10*(MMULT(--(B$2:$E$10=1),TRANSPOSE(COLUMN(B$2:$E$10)^0))=0))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.

Count of consecutive repeated value only when repeated 3+ times

My goal is to count and potentially conditionally format the occurrences when a certain number of days pass with 0 sales.
I am trying to return the number of times 0 is repeated consecutively 3 or more times. So for this example I would like to see the return value of 3. So far I can't wrap my brain around how to do this, any ideas?
1
5
0
0
0
0
0
2
0
0
1
0
2
0
0
0
5
0
0
0
0
Thanks!
So #Barry Houdini's method applied to this problem would give
=SUM(--(FREQUENCY(IF(A1:A21=0,ROW(A1:1A21)),IF(A1:A21>0,ROW(A1:A21)))>=3))
entered as an array formula using CtrlShiftEnter
If you wanted to make it more dynamic and exclude blanks you could use
=SUM(--(FREQUENCY(IF(A1:A100<>"",IF(A1:A100=0,ROW(A1:A100))),IF(A1:A100>0,ROW(A1:A100)))>=3))
How about you make a help column with a simple sum formula with a "window" of three rows (or whatever you need). Then you conditionally format all values which are 0 in that column. That should provide you with the information you are looking for.

Counting digit in column based on subject

I am just using formulas in excel and was wondering how you could count all the 0s until a 1 is reached, and then start the process over again, based on subject number. If this is not possible simply with formulas, how could I write a VBA code for this?
Right now I am trying to use,
=IF(OR(F4=0,F3=1),"",COUNTIFS($A$2:A2, $A$2,$F$2:F2,0)-SUM($I$2:I2))
which I input in I3 and I change the COUNTIFS($A$#:A#, $A$#...) part for each subject number.
This seems to work with the exception of the last grouping, as it won't output a number before the next subject.
Example Data:
subid yes number_yes(output)
1 0
1 0
1 0 3
1 1
1 0 1
1 1
1 0
2 0
2 0 2
2 1
2 0
2 0
3
etc.
A blank cell is numerically zero and that is one of your accepted conditions. Differentiate between blanks and zero values.
=IF(and(f4<>"", OR(F4=0,F3=1)),"",COUNTIFS($A$2:A2, $A$2,$F$2:F2,0)-SUM($I$2:I2))
Based on #Jeeped answer. If you use -SUMIF($A$2:A2,A3,$I$2:I2) instead of -SUM($I$2:I2) you don't need to adjust this part for each subject number. Just use the following formula in I3 and copy it down.
=IF(AND(F4<>"",OR(F4=0,F3=1)),"",COUNTIFS($A$2:A3,A3,$F$2:F3,0)-SUMIF($A$2:A2,A3,$I$2:I2))
Note that I also changed the second parameter in the COUNTIFS to A3.

Column name of last non zero value in row in Excel

How I can get column name of last non zero value in row, using Excel functions.
Example:
A:E - columns with values
x - function result
A B C D E x
0 0 1 0 0 C
1 0 2 1 2 E
0 1 1 0 1 E
1 1 1 1 1 E
0 0 0 0 0
This works:
=IFERROR(CHAR(AGGREGATE(14,6,COLUMN(A1:E1)/(A1:E1<>0),1)+64),"")
Enter with CTRL + Shift + Enter:
=IFERROR(MID(ADDRESS(ROW(),MAX(IF(A2:E2,COLUMN(A2:E2)))),2,SEARCH("$",ADDRESS(ROW(),MAX(IF(A2:E2,COLUMN(A2:E2)))),2)-2),"")
These are array-formulas that must be entered using CTRL+SHIFT+ENTER:
This works up to ZZ:
=IFERROR(SUBSTITUTE(MID(CELL("address",OFFSET(A1,0,MAX(IF(A1:AA1<>0,COLUMN(A1:AA1),0))-1)),2,2),"$",""),"")
This works for all columns:
=IFERROR(MID(CELL("address",OFFSET(A1,0,MAX(IF(A1:AAA1<>0,COLUMN(A1:AAA1),0))-1)),2,FIND("$",MID(CELL("address",OFFSET(A1,0,MAX(IF(A1:AAA1<>0,COLUMN(A1:AAA1),0))-1)),2,99))-1),"")
Update: The accepted answer is more efficient. It assumes numeric values but could be easily updated to handle non-numeric values as well. However it could be optimised further:
=IFERROR(MID(SUBSTITUTE(ADDRESS(1,MAX(IF(A2:XED2,COLUMN(A2:XED2)))),"$1",""),2,3),"")

Resources