I want to sum the total amount of consecutive numbers (similar to a count of consecutive numbers attached) - excel

In this article there is directions on determining the number of consecutive cells greater than 0. I want to do something similar, but instead of simply counting the number of consecutive columns there are, I want to sum them up.
For example, 0 0 0 1 2 0 0 5 would be 3 (1+2=3)
Edit: If for example, there is multiple max consecutive numbers (e.g., 0 0 1 3 0 0 4 5) I would want the highest sum (9) to be chosen.
Edit2: The data is set-up in rows (X axis is months I need to sum for consecutives and Y axis is "user") so the formula would match the examples I've given above.

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.

Intermediate steps in evaluation of Frequency formula

This has reference to [SO question]Counting unique list of items from range based on criteria from other ranges
Formula Suggested by Scot Craner is :
=SUM(--(FREQUENCY(IF(B2:B7<=25,IF(C2:C7<=35,COUNTIF(A2:A7,"<"&A2:A7),""),""),COUNTIF(A2:A7,"<"&A2:A7))>0))
I have been able to understand clearly the logic and evaluation of the formula except for this step shown in the attached snapshots.
As per MS Office document:
FREQUENCY(data_array, bins_array) The FREQUENCY function syntax has
the following arguments: Data_array Required. An array of or
reference to a set of values for which you want to count frequencies.
If data_array contains no values, FREQUENCY returns an array of zeros.
Bins_array Required. An array of or reference to intervals into
which you want to group the values in data_array. If bins_array
contains no values, FREQUENCY returns the number of elements in
data_array.
It is clear to me as to How {1;1;4;0;"";"") comes in data_array and also how {1;1;4;0;5;3} comes in bins_array.But how it evaluates to {2;0;1;1;0;0;0} is not clear to me.
Would appreciate if someone can lucidly explain it.
So you wants to know how
FREQUENCY({1;1;4;0;"";""},{1;1;4;0;5;3}) evaluates to {2;0;1;1;0;0;0}?
Problem is that the bins_array not needs to be sorted to make FREQUENCY working. But of course it internally must sort the bins_array to get the intervals into which to group the values in data_array. Then it groups and counts and then it returns the counted numbers in the same order the bins was given in bins_array.
Scores Bins
1 1
1 1
4 4
0 0
"" 5
"" 3
Bins sorted
0 (<=0)
1 (>0, <=1)
1 (>1, <=1) == not possible
3 (>1, <=3)
4 (>3, <=4)
5 (>4, <=5)
(>5)
Bin Description Result
1 Number of scores (>0, <=1) 2
1 Number of scores (>1, <=1) == not possible 0
4 Number of scores (>3, <=4) 1
0 Number of scores (<=0) 1
5 Number of scores (>4, <=5) 0
3 Number of scores (>1, <=3) 0
Number of scores (>5) 0

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.

EXCEL Count number of weeks in month based on date

I am trying to look up a value in a matrix based on a given date. The matrix has the first day of the week along the vertical axis, and the first day of the month along the horizontal axis.
For a given day, e.g. 31/08/15 I would like to match the exact date to the vertical axis of the matrix (i.e. 31/08/15), and the month to the horizontal axis (1/08/15).
So in the example below, an input of 31/08/15 should provide an output of 3.
01/06/2015 01/07/2015 01/08/2015 01/09/2015
03/08/2015 1 0 0 0
10/08/2015 0 2 0 0
17/08/2015 0 0 3 0
24/08/2015 0 0 0 4
31/08/2015 0 0 3 0
I am trying and failing with index and match formulae.
I have tried the following:
=index(area where to look, match(31/08/15,first column,0),match(and(month(31/08/15),year(31/08/15)),(and(month(first row),year(first row)),0)
Hope this is clear, thanks!
You can use an INDEX function with two MATCH functions top supply both the row and column.
    
The formula in D8 is,
=INDEX($B$2:$E$6,MATCH(C8,$A$2:$A$6,0),MATCH(DATE(YEAR(C8),MONTH(C8),1),$B$1:$E$1,0))
I'm a little concerned about the dates matching exactly down column A but a little maths manipulation with the WEEKDAY function would take care of that.
=INDEX($B$2:$E$6,MATCH(C9-WEEKDAY(C9, 2)+1,$A$2:$A$6,0),MATCH(DATE(YEAR(C9),MONTH(C9),1),$B$1:$E$1,0))
Here you go:
=INDEX($B$2:$E$6,MATCH(DATE(2015,8,31),$A$2:$A$6,),MATCH(DATE(2015,8,1),$B$1:$E$1,))

Resources