Excel Find the first zero after a non zero within a range - excel-formula

I have a range, that contains data which follows a distribution *typically starts at 0, then goes up, then down, and finally back to zero.
I want to return the value of another column at the point that the data returns back to Zero (in my case you can see that Cell B6 is the point at which this event occurs, and I want my cell C2 to return the value "E" from cell A6...
Currently, the best I can get to is the following:
=INDEX(A2:B8,FIND(0,
TEXTJOIN(,,B2:B8),
MATCH(AGGREGATE(4,4,B2:B8),B2:B8,0)) -1,1)
The problem comes in with the fact that there can be any number of zeros before the data appears.
i.e. like this. = Where I would still expect the highlighted match.

1] 1st zero after a non-zero value, in C2 enter formula :
=INDEX(A:A,INDEX(MATCH(1,1/B:B),0)+1)
2] Nos. of zero after a non-zero value, in D2 enter formula :
=MATCH(9^9,B:B)-INDEX(MATCH(1,1/B:B),0)

For excel you can use INDEX/AGGREGATE:
=INDEX(A:A,AGGREGATE(15,6,ROW(B2:B8)/(B2:B8=0),2))
Edit:
If you want get first zero value after last non zero value use formula:
=INDEX(A:A,AGGREGATE(14,6,ROW(B2:B8)/(B2:B8<>0),1)+1)

Related

If cell has zero entered, display zero. If cell has no value, display nothing [duplicate]

Suppose there is an empty excel sheet. Enter formula "=A1" into the B1 cell. The value of B1 would be 0.
My question is: why the value of B1 becomes zero when referring to an empty cell A1? Any rationales Excel behaves this way?
Thanks.
That is because the formula in B1 returns the value of cell A1.
Excel assigns the value 0 to a blank cell.
To distinguish 0 from blank, in B1 enter:
=IF(A1="","",A1)
FWIW, force a zero-length string with =A1&"". This can also be used to show (an apparently blank) cell when a VLOOKUP of INDEX/MATCH wants to return a zero after encountering a blank cell to return. Two caveats: first, a zero-length string is not truly blank and second, if used with VLOOKUP(...)&"" then any true number that should have been returned as a true number becomes text-that-looks-like-a-number. – Jeeped
Quoting the best answer so I can vote on it :)
I changed my application to =formula&"" according to Jeeped, and works great. Kinda dumb that Index returns Value(formula).

Excel Index with count function no working?

I'm trying to use an index with count function to return the last number in a specific array.
I have numeric data in C6:C17 and I want that F6 returns the last value from that array (C6:C7).
I've used this formula: =INDEX(C6:C17;COUNT(C6:C17)) and it works perfectly.
But when I use this formula: =INDEX(C6:C17;COUNT(C6:C17)-4), it returns the value of the fourth previous row. If I only have values on C6, C7, C8, C9 and the rest is empty, it returns the value of C6. If I choose F7 instead of F6 to return the value, the same formula gives me the value of C7.
It shouldn't give me an error, since my -4 is now outside of the array. It only happens in the row before the row where the array starts.
If I put (-5) in the same formula and with the same values, it gives me an error.
That is because 0 is a viable option for the row return:
=INDEX(C6:C17;0)
Will return the entire range as an array and since that array is being put in the one cell and not many through an array formula it returns the first value only of the array, C6.
Negatives are not a viable option:
=INDEX(C6:C17;-1)
will error.
If you want to stop that from happening you need to test if 0. A quick way is to get the reciprocal of the reciprocal:
=INDEX(C6:C17;1/(1/(COUNT(C6:C17)-4)))
This will now throw an error instead of returning the value if the count is less than 5.
Or you can just test and return a negative:
=INDEX(C6:C17;IF(COUNT(C6:C17)>4;COUNT(C6:C17)-4;-1))

MAX + Left Function Excel

I am trying to get the max value of a column based on the Left function
What I am doing is the following :
These are the results I get when i write this into column C :
=MAX(LEFT(A:A, 2))
But what I truly want is to get in column C the max value of all column A not for each cell.
So the result should be in this case 90 for all rows.
What should be the formula here?
Just another option that gets entered normally:
=AGGREGATE(14,6,--LEFT($A$1:INDEX(A:A,MATCH("ZZZ",A:A)),2),1)
Array formulas will calculate the enitre referenced array. So care should be taken to limit the number of iterations to only the data set.
The $A$1:INDEX(A:A,MATCH("ZZZ",A:A)) part of the formula does that. It finds the last cell in column A with data in it and sets that as the upper bound. So in this instance the reference range is A1:A3. But, it will grow dynamically as data in Column A is added, so no need to change the formula each time data is added.
Update 2
Here is another solution which I think is better than my original (below)
=INT(SUMPRODUCT(MAX(SUBSTITUTE(A:A,"-",".")*1)))
it can be entered as normal (just Enter)
Orignal Answer
You need numbers and arrays
=MAX(IFERROR(LEFT(A:A,2)*1,0))
Let's break this down. Multiplying by turns your strings into numbers - since Left only returns a string
LEFT(A:A,2)*1
Unfortunately this method returns #Value if you multiply an empty string by 1. You will definitely have some empty strings in the range A:A so we wrap the whole thing with an IFERROR function.
IFERROR(LEFT(A:A,2)*1,0)
Now we still need excel to treat this as an array (i.e. a whole column of numbers, rather than just one number). So we put the MAX formula in and enter it with Ctrl+Shift+Enter rather than just Enter. The result is that the formula looks like this in the formula bar
{=MAX(IFERROR(LEFT(A:A,2)*1,0))}
which would return 90 in your example, as required
Update 1
If you are using Excel 2013 or later, you can also use the NUMBERVALUE function
=MAX(NUMBERVALUE(LEFT(A:A,2)))
again, enter it with Ctrl+Shift+Enter

I want to list all values in a range where adjacent cell value have:1) time difference is less than 24hour, and other cells 2) equal specific text

TABLE + DESIRED RESULT
This is what I got but not sure why it doesn't work:
Formula entered in Sheet2!A2=
index($A$2:$A$100, match(0,if(OR(AND(now()-Sheet1!O2<1,D2="closed"),D2="Pending",""),0))
Show me the list of cells in Sheet1! when Condition 1: Now - Time in O2 < 1 Day & D2 = "closed"
Or Condition 2: D2 = "Pending"
Thanks for the help guys
Ok I think I got it, this is an array formula so you want to copy it into cell G2 and press CTRL+SHIFT+ENTER, then drag the formula down far enough to hold all the results. I changed the condition from "Open, waiting, pending or closed within the last 24 hours" to "not closed more than 24 hours ago" to make it easier to read
formula for G2 is:
=IFERROR(INDEX(A$2:A$11, SMALL(IF(NOT((C$2:C$11="Closed")*(NOW()-D$2:D$11>1)), ROW(A$2:A$11)-ROW(A$2)+1), ROWS(A$2:A2))), "")
The SMALL function looks like this =SMALL(array, n) and returns the nth smallest element of the array. In our case n is given by ROWS(A$2:A2) which will give the row number of the current row in the result output. i.e. the first row of your results will contain the 1st smallest number, the 2nd row will contain the 2nd smallest number etc, the trick is that its not giving the smallest number in the id list but its giving the smallest number in the array defined by this line:
IF(NOT((C$2:C$11="Closed")*(NOW()-D$2:D$11>1)), ROW(A$2:A$11)-ROW(A$2)+1
This part is a little complicated as its all array formulas/logic. Please note the * in this context represents a logical AND. If you want to understand it better you can highlight just this part of the formula in the formula bar in Excel and press F9, this will show the values of the array. Clicking in cell G2 and highlighting the line above gives this:
{1;FALSE;3;4;5;FALSE;FALSE;8;9;10}
You can see that the resulting array contains the row number for rows that meet the condition and FALSE for those who don't. The INDEX and SMALL functions then display the id of the row with the 1st smallest value then the second etc with the FALSE's used to ignore the rows that don't meet the condition.
syntax error
you have:
AND(now()-Sheet1!O2<1, D2="closed")
which is fine, problem is in your OR statement:
OR(AND(now()-Sheet1!O2<1,D2="closed"),D2="Pending","")
the last parentheses should be before the last comma I believe. Currently you have an OR statement with 3 arguments the last of which is "" which isn't going to give you what you want. I think you intended the "" to be the result of the IF statement not a condition for the OR statement, try this:
if(OR(AND(now()-Sheet1!O2<1, D2="closed"), D2="Pending"),"",0)

Formula Issue in Excel 2007

I am trying to add to an existing formula. The formula will first look to see if a cell is either greater than zero or is zero, and then based on that it will select an appropriate cell. Below is the current formula. However, I want it to look at cell E5 first and if it is equal to zero select cell E160. If it is greater than zero, I want it to take the minimum value of either cell E160 or E159.
=(IF(ISERR(E160),E159,(IF(ISERR(E159),E160,MIN(E160,E159)))))
I am also confused by the ISERR in your formula. If you are just assessing a number, it could use the > symbol, like this:
=if(e5=0, E160, if(e5>0, min(e160,e159),""))
You'll notice also that there is a third possibility accounted for here: if the value is not 0 nor greater than 0, the formula returns an empty string - the "" at the end of the formula.

Resources