How to count current streak? - excel

I have a list of 1s and 0s in excel ranging from A1:A74 and I am looking to work out what the current streak of 1's is.
For example I have:
1
0
1
1
1
I would want the streak to give me 3.
I have tried the following formula which seems to work for smaller ranges, but for my full set it gives me the wrong amount:
=COUNTA(A1:A73)-MATCH(1,INDEX(1/(A1:A73=0),0))
Any help would be much appreciated.
edit - I believe i've fixed the formula above to work:
=COUNTA(S$2:S$74)-MATCH(2, 1/(S2:S$74=0), 1)
This is basically finding the last position of 0 and minusing this from the overall number of rows which have values.

As Above, I've figured out the answer to my own question by just simplifying exactly what I needed to do and it became very obvious:
=COUNTA(S$2:S$74)-MATCH(2, 1/(S2:S$74=0), 1)
This is basically finding the last position of 0 and minusing this from the overall number of rows which have values.

If you can afford a helper column then there is another very easy way to do this.
Enter this formula in B2 (or any other cell in row 2(Assuming you are using a header)) and copy it down.
=IF(AND(A2=0,A3=0),B1+1,1)
You can then pick up the maximum value from this range and if required hide the column.

Related

Finding uninterrupted sub-arrays in Excel - Kadane's algorithm variation?

Suppose you have an ordered, indexed list of positive values. These positive values are interrupted by 0 values. I want to determine if a consecutive sub-array exists which is not interrupted by 0 values and whose sum exceeds a certain threshold.
Simple example:
Index, Value
0 0
1 0
2 3
3 4
4 2
5 6
6 0
7 0
8 0
9 2
10 3
11 0
In the above example, the largest consecutive sub-array not interrupted by 0 is from index 2 to index 5 inclusive, and the sum of this sub-array is 15.
Thus, for the following thresholds 20, 10 and 4, the results should be FALSE, TRUE and TRUE respectively.
Note I don't necessarily have to find the largest sub-array, I only have to know if any uninterrupted sub-array sum exceeds the defined threshold.
I suspect this problem is a variation of Kadane's algorithm, but I can't quite figure out how to adjust it.
The added complication is that I have to perform this analysis in Excel or Google Sheets, and I cannot use scripts to do it - only inbuilt formulas.
I'm not sure if this can even be done, but I would be grateful for any input.
Start with
=B2
in c2
then put
=IF(B3=0,0,B3+C2)
in C3 and copy down.
EDIT 1
If you were looking for a Google sheets solution, try something like this:
=ArrayFormula(max(sumif(A2:A,"<="&A2:A,B2:B)-vlookup(A2:A,{if(B2:B=0,A2:A),sumif(A2:A,"<="&A2:A,B2:B)},2)))
Assumes that numbers in column B start with zero: would need to add Iferror if not. It's basically an array formula implementation of #Gary's student's method.
EDIT 2
Here is the Google Sheets formula translated back into Excel. It gives you an alternative if you don't want to use Offset:
=MAX(SUMIF(A2:A13,"<="&A2:A13,B2:B13)-INDEX(SUMIF(A2:A13,"<="&A2:A13,B2:B13),N(IF({1},MATCH(A2:A13,IF(B2:B13=0,A2:A13))))))
(entered as an array formula).
Comment
Maybe the real challenge is to find a formula that works both in Excel and Google sheets because:
Vlookup doesn't work the same way in Excel
The offset/subtotal combination doesn't work in Google sheets
The index/match combination with n(if{1}... doesn't work in Google sheets.
With data in columns A and B, insure column B end with a 0. Then in C2 enter:
=IF(AND(B3=0,B2<>0),SUM(B$1:$B2)-MAX($C$1:C1),"")
and copy downwards:
Column C lists the sums of consecutive non-zeros. In another cell enter something like:
=MAX(C:C)>19
where 19 is the criteria value.
You can avoid the "helper" column by using a VBA UDF.
EDIT#1:
Use this instead:
=IF(AND(B3=0,B2<>0),SUM(B$1:$B2)-SUM($C$1:C1),"")
Thanks to #Tom Sharpe and #Gary's Student for answering the question.
While I admittedly did not specify this in the question, I would prefer to achieve the solution without a helper column because I have to do this operation on 30+ successive columns. I just didn't think it was possible in Excel.
Full credit goes to user XOR LX on the Excelforum for coming up with this solution. It has blown my mind and took me the better part of an hour to wrap my head around, but it is certainly very creative. There is no way I could have come up with it myself. Re-posting it here for the benefit of everyone who is looking into this.
Copy and paste the table from my initial question into an empty Excel sheet such that the headers appear in (A1:B1) and the values appear in (A2:B13).
Then enter this formula as an array formula (ctrl+shift+enter), which gives the max of the sums of all the uninterrupted sub-arrays:
=MAX(SUBTOTAL(9,OFFSET(B2,A2:A14,,-FREQUENCY(IF(B2:B13,A2:A13),IF(B3:B14=0,A2:A13,0))-1)))
Note the deliberate offset to include one additional row below the end of the dataset.

excel min value different than 0 such that the next value is 0

I've the following matrix, and I need to get the hours of the minimum and maximum values from each block of ones.
In my first attempt, I've obtained the first 1 and the last one (8:30 and 17:00) using the minifs and maxifs formula.
Since I couldn't obtain 11:00 and 12:30, I changed my approach. I've created a help matrix with 48 columns that gives 0,1,2,3 or 4 in the positions that I want. See it:
However, doing this help matrix increases the file size a lot. I wish that I could get the middle hours by using just a formula. I don't want to use the help matrix.
Thanks in advance!
Put this in the first cell and drag across and down:
=INDEX($1:$1,AGGREGATE(15,7,COLUMN($B2:$AV2)/(($A2:$AU2=--(ISEVEN(COLUMN(A1))))*($B2:$AV2=--(ISODD(COLUMN(A1))))),INT((COLUMN(A1)-1)/2)+1)-ISEVEN(COLUMN(A1)))

Formula to read text as number

Okay so as you can see each column has a value of 1-3. Currently there's just a basic formula at the end that works out the percentage by adding whats there and then dividing by the total possible if every column was 3 and then multiplying by 100.
My problem is I would like to put 'n/a' down as a result sometimes where a number value/score wasn't relevant. But when it comes to calculating the percentage it would be marked lower since it would be three less than the total possible.
So I assume there a few different ways I could tackle this problem, either by a formula that calculates the possible total based upon only the cells that have a numerical value or a formula which reads n/a as the value 3. But I can't seem to find something that works.
Please help, thanks.
You can do this:
The countif() counts the n/a and multiplies by 3 to be subtracted from the 24.
Okay I think I figured this out and the solution was far more simple that I thought it would be.
Originally the Score as % formula was =SUM(T14/24*100) 24 being the total is all cells were 3.
So what I've done is add a new column called possible total with the formula =COUNT(K14:R14)*3 As the count function only counts cells that have a numerical value, thus will ignore any cells with n/a and since 3 is the maximum value that can be entered into the cell I've multiplied the count by 3.
Then it was a simple case of changing the score formula to =SUM(T14/S14*100) where the S column will be the new possible total.

Excel: How do I sum cell values every nth colum

I have the following table:
and I'd like to have the total for each player but values are every 3 columns.
As you can see from the picture on the bottom part I wrote what manually I should enter.
For player 1
=SUM(D3;G3;J3...)
Player 2
=SUM(D4;G4;J4...)
and so on. What formula should I use to calculate automatically every 3 columns? I know how the MOD works but on the net I found too many examples each one using different methods and none worked so far. Can anyone help me please or point me to the right direction to understand how it works to get this data since I'll be using this a lot (get value from cell every nth column).
thanks
It looks like you should just be using SUMIFS here:
=SUMIFS(3:3,$2:$2,"TOT")
This will sum every value on row 3 (Player 1) where the value in row 2 is "TOT" (every 3rd column). Put this in cell B18 and just copy down in your column B.
Or, in case you change your column labels, you can refer to cell D2 instead of typing "TOT" in the formula:
=SUMIFS(3:3,$2:$2,$D$2)
Try this, it will total all the cells that occur every 3 columns beginning at column D. Z3 can be increased to any column you require:
=SUMPRODUCT((D3:Z3)*(MOD(COLUMN(D3:Z3)-1,3)=0))
The explanation on how it works can be found here (I advise you to bookmark this site for future references. They have many other helpful formulas and functions).
Applying this formula to your reality should be something like this for Player 1 (Not tested. Adjust the ranges as required):
=SUMPRODUCT(--(MOD(COLUMN(D3:Z3)-COLUMN(D3)+1,3)=0),D3:Z3)

Increment constant in Excel Formula

Suppose, in Excel, I have a formula =$C$8+1, what must I add to the 1 such that when I drag, it becomes $C$8+2?
Can this be done?
Below is my actual formula.. I wish to increment 1 to 2 such that it becomes =2
IF((NOW()-$C8)=1,"1","0")
Well.. there might be several ways to do it, but the only one I have in the top of my head is using the =ROW() function.
Let's say you're starting the formula at row 5.
=IF((NOW()-$C8)=ROW()-4,"1","0")
The =ROW() will return the row number you currently are (i.e. 5, in this case). Thus, in the first row we'll have =1 (from 5-4) and then =2 (6-4) and so on.
Still, it seems you're comparing dates, right? I'd say you'd need to truncate the values to have a day comparison between them...
=IF((TRUNC(NOW())-TRUNC($C8))=ROW()-4,"1","0")
Hope it helps... or at least give you a path to chose your solution.

Resources