简体   繁体   English

使用数组公式根据条件计算datedif

[英]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 我可以使用公式获取G列中每一行之间的datedif,类似于

=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. 并一直沿列向下复制,但是我想我需要一个数组公式以返回并找到与B列匹配的当前行上方的第一行,并获取这两个日期之间的datedif或days。 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... 我需要将结果设置为第5行和第3行(夜间)和第4行和第2行(白天)之间的日期,然后向下复制约300行...

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 可以使用LOOKUP函数查找先前的匹配项,因此请尝试将G2此公式复制下来

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

format result cell as number with no decimal places 将结果单元格格式化为不带小数位的数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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