简体   繁体   中英

Use an array formula to calculate datedif on criteria

Lets say I have a record of logged flights in a range, example below

    [A]      |   [B]   |   [C][D][...]    |    [G]
1  Date      |   Mode  |   More Data....  |    Days Since
2  1 May     |   Day   |   ....           |    Formula here
3  4 May     |   Night |   ....           |    Formula here
4  6 May     |   Day   |   ....           |    Formula here
5  8 May     |   Night |   ....           |    Formula here

I can use a formula to get the datedif between each row in column G, similar to

=DATEDIF(A2,A3,"d")

and copy it all the way down the column, but I'm guessing I need an array formula to go back and find the first row above the current row that matches in column B and get the datedif or days between those two dates. I'm assuming an array formula, but what would the best way to go about that be? I need the result to be the days between row 5 and 3 (night) and 4 and 2 (day) and then copied down about 300 rows...

I was looking at another array formula for sorting rows and eliminated blanks, but not sure how to adapt it to this scenario.

To get the difference in days you only have to subtract one date from the other.

LOOKUP function can be used to find the previous match, so try this formula in G2 copied down

=IFERROR(A2-LOOKUP(2,1/(B$1:B1=B2),A$1:A1),"")

format result cell as number with no decimal places

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM