Calculate current streak in Excel row - excel

I have a list of 1s and 0s in excel row ranging from B2:K2, I want to calculate the current streak of 1's in cell M2,
example dataset where streak would be 4
1 0 1 0 1 1 1 1 0
Is there a simple way of doing this? I have tried research but not been able to find anything specific.
Any help would be much appreciated.

Here is a way of doing this with just one formula, no helper columns/rows needed:
The formula used translates to:
{=MAX(FREQUENCY(IF(B1:K1=1,COLUMN(B1:K1)),IF(B1:K1=1,0,COLUMN(B1:K1))))}
Note: It's an array formula and should be entered through CtrlShiftEnter

Assuming your data is layed out horizontally like the image below, the following two formulas should do it for you.
The first cell requires a different formula as the is no cell to the left to refer to. so a simple formula to check if the first cell is one or not is entered in B2.
=--(A1=1)
The part in the bracket will either be true or false. A quirk of excel is that if you send a true or false value through a math operation it will be converted to 1 for true and 0 for false. That is why you see the double - in front. could have also done *1, /1, +0,-0 at the end.
In B2 place the following formula and copy right as needed:
=(A2+1)*(B1=1)
Basically it adds 1 to the series, then check if the number in the sequence is 1 or 0. In the event its one, it keeps the value as it is TRUE sent through the math operator *. If it is false it set the sequence back to zero by multiplying False by the math operator *.
Alternate IF
Now the above while it works and may save a few characters is not necessarily intuitive for most. The go to option would be to use an IF function. The above formulas can be replaced with the following:
A3
=IF(A1=1,1,0)
B3 ->Copied right
=IF(B1=1,A3+1,0)
Longest streak
To get the longest streak, the highest value in your helper row is what you want. You can grab this with the following formula in an empty cell.
=MAX(2:2)
=MAX(A2,I2)
If you have no other numbers in your helper row, you can use the first formula which looks in the entire row. If there are other numbers due to calculations off to the left or right as an example, then you will want to restrict your range to you data as in the second formula.

I've put those values in cells B2 to B8.
In cell C3, I've put this formula:
=IF(AND(B3=1;B2=1);C2+1;1)
Dragging this downto C8, and then take the maximum of the C column.

Related

Get Count of Cells used in Excel Formula

I want to get the count of cells used in an excel function.
For example say I have a sum function ='CV'!D11+Farmer!D11+'County'!D11+Rt!D11+WT!D11+'Country'!D11
I need a function that will tell me how many cells were used to get the total sum. In this case it is 6. The tricky part is if one of the cells used is blank I do not want it counted. For instance say cell D11 on the Farmer sheet is blank I do not want it counted in the total. So the total should be 5.
Use COUNT:
=COUNT('CV'!D11,Farmer!D11,'County'!D11,Rt!D11,WT!D11,'Country'!D11)
It will only count the cell if it has a number
You should really try to collate all your data in to a single sheet before running calculations. For the sake of example, I'll assume you have it in the range A1:A5, then you can add handling of the various cases using array formulas:
Get the count of non-empty cells (the ISBLANK function is untrustworthy in my experience): {SUM(IF(LEN(A1:A5)>0,1,0))}
Get the sum of those cells: SUM(A1:A5)
(must use Ctrl+Shift+Enter to enter the formula as an array formula, you will know it worked if the formula shows like {IF(...)} with the curly brackets)
Because blank/missing values are treated implicitly as 0 in the SUM function, this case is simple. If you have other validations then you'd have to write an array formula for the summation as well. For example, only including numbers between a min and max threshold (e.g. if you want to exclude outliers):
{SUM(IF(AND(A1:A5 >= yourMinValue, A1:A5 < yourMaxValue), A1:A5, 0)}.
If I understand your question correctly, you want to literately count the number of cells used in a formula which in your example is summing 6 values from 6 different locations.
I used the following example to demonstrate my solution:
The sum of =A1+B1+C1+D1+E1+F1 is 10 where cell C1 has a 0 value in it but cell E1 is blank.
Using the following array formula I was able to count the number of cells that have a value other than 0:
=SUMPRODUCT(IFERROR(ABS(N(INDIRECT(TRIM(MID(SUBSTITUTE(RIGHT(FORMULATEXT(A3),LEN(FORMULATEXT(A3))-1),"+",REPT(" ",100)),100*ROW(INDIRECT("1:"&LEN(FORMULATEXT(A3))))-99,100)))))>0,0)*1)
Please note you MUST press Ctrl+Shift+Enter upon finishing the formula in the formula bar otherwise they will not function correctly.
The logic is to use a combination of TRIM+MID+SUBSTITUTE+RIGHT+FORMULATEXT+REPT+ROW+INDIRECT to extract the cell addresses from the original formula, then use INDIRECT to convert the cell address into the values stored in those cells, then use a combination of IFERROR+ABS+N to find out if any of these values are not 0, and lastly use SUMPRODUCT to add up all the TRUE results.
It is obvious that there are a couple limitations of my solution:
If your actual formula is not strictly in the form of A+B+C+D+E+F, then my SUBSTITUTE part of formula will need further modification;
The formula will treat cells containing 0 as blank and does not include them in the count.
Let me know if you have any questions. Cheers :)

Why is =SUMIF(C5:C19,NOT(ISFORMULA(C5:C19))) returning zero?

I have a spreadsheet that I track my hours. Each cell initially is populated with a formula, i.e. =IF(WORKDAY(B24-1,1,holidays2019)=B24,OFFSET(C24,-1,2),0)
and then as the month progresses I enter my actual time.
In the following excerpt all values through 5/10/2019 are entered.
The formula =SUMIF(C5:C19,NOT(ISFORMULA(C5:C19))) shows zero. I do not understand why this does not work.
I appreciate any help! Column B in my spreadsheet corresponds to the dates shown below and Column C to the time entries.
Expected Result: 48.9
=SUMPRODUCT(J6:J20,--NOT(ISFORMULA(J6:J20)))
The key to this solution is the -- in front of the NOT(). A boolean that is processed by a math operator gets converted to 1 or 0. --, +0, -0, *1, /1 would have all worked to do the conversion. So now you wind up with an array of values you may want to sum being multiplied by an array of 1 and 0 to indicate the ones you want. The 1 are manual entry and the 0 are your formulas entries.
Now SUMPRODUCT performs array like calculations. As a result avoid using full column/row references inside it or you will wind up with a lot of excess calculations. Adjust the ranges in the answer to suit your needs.
Here's the MSDN definition of the Criteria in =SUMIF
criteria Required. The criteria in the form of a number, expression,
a cell reference, text, or a function that defines which cells will be
added. For example, criteria can be expressed as 32, ">32", B5, "32",
"apples", or TODAY().
Important: Any text criteria or any criteria that includes logical or
mathematical symbols must be enclosed in double quotation marks (").
If the criteria is numeric, double quotation marks are not required.
So, the reason, why your SUMIF returns 0 is, because none of the cells match the criteria, as they return a number and meanwhile they expect FALSE
Another issue here being, that the ISFORMULA will return TRUE, even when a range contains a single formula while all the rest has none. So basically you need to drag the formula down for each cell individually and sum them up only when a value is TRUE
Starting from cell D1:
=ISFORMULA(B1)
And then you can simply sum them up with the formula you provided.
=SUMIF(D1:D16,TRUE,C1:C16)
Obviously, you can hide the column D to make it more aesthetically pleasing.
Your formula fails because the criteria you're matching against, is TRUE/FALSE. Obviously the values in C5:C19 don't contain any booleans, so the sum is 0.
To solve this, you can add the correct criteria in cell D5 and below: =ISFORMULA(C5)
Then use =SUMIF(D5:D19,FALSE,C5:C19) to sum the values in column C.

Syntax: Applying IF to a cell range in Excel

I've been trying to write a formula to summarise in one cell the presence/ absence of certain values in a different range of of cells in Excel
So in the one table and worksheet, I wrote
=IF(B1:F1=1,1,0) Formula 1
which is supposed to mean
If any of the values in cells B1:F1 are equal to 1, then note 1, otherwise not 0.
But somehow the syntax isn't working.
I've applied "" and ; and brackets right left and centre, but to no avail.
I'm pretty sure I done this before and it was pretty simple when I hit upon the right synstax, but the how and where fell through the colander which is my brain today :-?
Additionally I will want to ask the formula to apply another condition to the output cell which is
=if (A1 = value n or certain values, 1, 0) Formula2
Column A has numerically coded ordinal values 0-9, so an aexample of teh 1 conditions might be any of values 1, 2 or 9 in column, should produce a 1 in the result cell in which Formula 1 and 2 will be written.
So the result cell would contain somelike
=Formula1_or_Formula2_contain_certain_values, 1, 0) Formula 3
Obviously the systax of Formulas 2 and 3 is awol, but I write to demonostrate the formulae intended purposes.
The easiest way to make the first formula is like this:
=IF(SUM(B1:E1)>0,1,0)
No arrays, no problems :)

Average a list of numbers if greater than 0

How do I average a list of numbers whose values are greater than 0? I know I can use AVERAGEIF function in Excel
My data is located in A2, A5, A6, A10, A17.
I only want to average it if the data is greater than 0.
Since my data is not an range, I am not able to use AVERAGEIF Function range.
Need some help on this.
EDIT
For example,
I tried with three numbers:
1) 98.068 and 98.954 and 0 so my forumla looked like this:
=AVERAGE(IF(N(OFFSET(A2,{0,5,10},))>0,N(OFFSET(A2,{0,5,10},))))
The answer came out as 99.106. Not sure why.
A few options:
1)=SUM(SUMIF(INDIRECT({"A2","A5","A6","A10","A17"}),">0"))/SUM(COUNTIF(INDIRECT({"A2","A5","A6","A10","A17"}),">0"))
2)=AVERAGE(IF(N(INDIRECT({"A2","A5","A6","A10","A17"}))>0,N(INDIRECT({"A2","A5","A6","A10","A17"}))))
3)
=AVERAGE(IF(N(OFFSET(A2,{0,3,4,8,15},))>0,N(OFFSET(A2,{0,3,4,8,15},))))
2) and 3) must be committed as array formulas**
Regards
(0) A simple method
=SUM(A2*(A2>0),A5*(A5>0),A6*(A6>0),A10*(A10>0),A17*(A17>0))/SUM(A2>0,A5>0,A6>0,A10>0,A17>0)
(4) A more general method
=SUM((A1:A20>0)*A1:A20*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))/
SUM((A1:A20>0)*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))
The second one is an array formula and must be entered with CtrlShiftEnter
If it's possible to have text in the cells rather than numbers, then this should replace the first formula:-
=SUM(N(A2)*(A2>0),N(A5)*(A5>0),N(A6)*(A6>0),N(A10)*(A10>0),N(A17)*(A17>0))/SUM(N(A2)>0,N(A5)>0,N(A6)>0,N(A10)>0,N(A17)>0)
(I haven't used N in the > brackets in the numerator because I reason that if A2 etc. is text, the product will always be zero)
I can't persuade N to work with arrays in the second formula, so at the moment I have the rather lengthy
=SUM((IF(ISNUMBER(A1:A20),A1:A20,0)>0)*IF(ISNUMBER(A1:A20),A1:A20,0)*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))/
SUM((IF(ISNUMBER(A1:A20),A1:A20,0)>0)*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))
but I have tested it on text values and negative numbers and it does seem fine.
The only exception is if one of the cells contains TRUE. In this case the first formula will count it as 1, the second formula will ignore it.

Summing numeric portion of cell when alpha portion of cell is the same

I have a spreadsheet that has columns for dates and the values can be either "1v, .5v, 1p, .5p, 1s, .5s"
I have 3 columns in each row one for each letter "v, p and s". I want to be able to add the total of all cells in the range grouped by letter and then display the sum for each letter in it's respective column (v, p or c).
Here is an example of the sheet:
Name Vacation Personal Sick 1/5/15 1/6/15 1/7/15 1/8/15
Billy 1.5 1 0 .5v 1v 1p
It is the formula that goes in the vacation/personal/sick cell that I just can't figure out.
I went down the array formula route and came up with essentially the same formula as #Sancho.s :-
=SUM(LEFT($E2:$H2,LEN($E2:$H2)-1)*(RIGHT($E2:$H2)="v"))
You could modify it to take account of blanks:-
=SUM(IF($E2:$H2<>"",LEFT($E2:$H2,LEN($E2:$H2)-1)*(RIGHT($E2:$H2)="v")))
Perhaps this would be better, to ignore any mis-formatted cells:-
=SUM(IFERROR(LEFT($E2:$H2,LEN($E2:$H2)-1)*(RIGHT($E2:$H2)="v"),0))
These all have to be put in with Ctrl-Shift-Enter.
Assuming the range you posted starts at A1, use
=SUMPRODUCT((RIGHT($E2:$G2,1)="v")*LEFT($E2:$G2,LEN($E2:$G2)-1))
in B2. Change "v" and the range to use suitably.
Pro:
It is not an array formula. See why this may be important
Con:
I could not make it work with blank cells.
This "array entered" version will also allow blanks
=SUM(IF(RIGHT(E2:G2)="v",SUBSTITUTE(E2:G2,"v","")+0))
confirmed with CTRL+SHIFT+ENTER

Resources