UPDATE WITH ANSWER:
I misunderstood the operation of MATCH
and LOOKUP
; they apparently do not automatically select the last value.
The updated formula is as follows:
=IF(
ISNUMBER( MATCH(2,INDEX(1/($D2=$D$1:$D1),0)) ),
($A2+$B2) - (LOOKUP(2,1/($D2=$D$1:$D1),$A$1:$A1)+LOOKUP(2,1/($D2=$D$1:$D1),$B$1:$B1))
,0 )
The main differences are that MATCH
is now MATCH(2,INDEX(1/($D2=$D$1:$D1),0))
and LOOKUP
is now LOOKUP(2,1/($D2=$D$1:$D1),$A$1:$A1)
.
Thanks barry houdini and Nanashi for their help!
I am working on what was supposed to be a simple spreadsheet, but one of my formulas is giving me unanticipated results. A screenshot of my data can be seen below:
![Spreadsheet Screenshot][1]
In column C, I am trying to get the time difference between the previous data point with the same type and the current data point. My formula (formatted for easier reading, and taken at cell C11) is as follows:
=IF(
NOT( ISNA( MATCH($D11,$D$1:$D10,1) ) ),
($A11+$B11)-(
LOOKUP($D11,$D$1:$D10,$A$1:$A10)+LOOKUP($D11,$D$1:$D10,$B$1:$B10)
),FALSE)
The cell numbering changes for the appropriate cell–for example, C10 references $D10 and the range $D$1:$D9, etc.
- The conditional (
NOT( ISNA( MATCH($D11,$D$1:$D10,1) ) )
checks to make sure that in column C there is a previous value that equals the current one; otherwise it returns FALSE
, as it does in rows 1 and 12.
- The calculation
($A11+$B11)-(LOOKUP($D11,$D$1:$D10,$A$1:$A10)+LOOKUP($D11,$D$1:$D10,$B$1:$B10)
takes the current row date and time and subtracts the date and time in the row corresponding with the previous instance of the same type (the LOOKUP
function is supposed to return the last occurrence of a value in an array, according to the documentation).
My issue is this: after row 12, the MATCH
and LOOKUP
functions are all picking up row 11 as the last row in which column C has the value of 1. I've tried testing them separately, and both functions return their appropriate values from row 11. For example, putting the following formula in E16: =MATCH(1,$D$1:$D16,1)
returned 11 when I would expect the value to be 16.
What am I doing wrong?