简体   繁体   English

在Excel中查找以前的日期

[英]Lookup previous date in excel

Looking for way to find out the last time an event occurred. 寻找一种方法来找出上一次事件发生的时间。 Here is what I have for data in excel. 这是我在Excel中获得的数据。 I would like to be able to look up the Home column name in both the Home and away column then Subtract the date from the last time that name occurred. 我希望能够在“首页”和“离开”栏中查找“首页”列的名称,然后从上次出现该名称的日期中减去日期。 Is this possible? 这可能吗? If so what formula am I looking for? 如果是这样,我在寻找什么公式?

Date       Home Away
10/4/2017   TOR WPG
10/4/2017   STL PIT
10/4/2017   CGY EDM
10/4/2017   PHI SJS
10/5/2017   NSH BOS
10/5/2017   MTL BUF
10/5/2017   COL NYR
10/5/2017   WSH OTT
10/5/2017   MIN DET
10/5/2017   PIT CHI
10/5/2017   ARI ANA
10/5/2017   PHI LAK
10/6/2017   NYI CBJ
10/6/2017   FLA TBL
10/6/2017   VGK DAL
10/7/2017   NYR TOR
10/7/2017   DET OTT
10/7/2017   TBL FLA
10/7/2017   COL NJD
10/7/2017   BUF NYI
10/7/2017   NSH PIT
10/7/2017   MTL WSH
10/7/2017   MIN CAR

Desired outcome: 期望的结果:

Date    |  Home|Away|Home_last|Away_Last
10/4/2017   TOR WPG 0   0
10/4/2017   STL PIT 0   0
10/4/2017   CGY EDM 0   0
10/4/2017   PHI SJS 0   0
10/5/2017   NSH BOS 0   0
10/5/2017   MTL BUF 0   0
10/5/2017   COL NYR 0   0
10/5/2017   WSH OTT 0   0
10/5/2017   MIN DET 0   0
10/5/2017   PIT CHI 1   0
10/5/2017   ARI ANA 0   0
10/5/2017   PHI LAK 1   0
10/6/2017   NYI CBJ 0   0
10/6/2017   FLA TBL 0   0
10/6/2017   VGK DAL 0   0
10/7/2017   NYR TOR 2   3
10/7/2017   DET OTT 2   2
10/7/2017   TBL FLA 1   1
10/7/2017   COL NJD 2   0
10/7/2017   BUF NYI 2   1
10/7/2017   NSH PIT 2   2

D2=IFERROR(DAYS($A2,INDIRECT("A"&MAX(IF(B2=$B$1:$C1,ROW($B$1:$C1),0)))),0)

  1. We are going to check B2 and looking backward to see if there is a match. 我们将检查B2并向后看是否存在匹配项。 If so, we need it's Row number, by using ROW() , otherwise it should be 0 . 如果是这样,我们需要使用ROW()作为Row号,否则应为0 However, an ordinary formula can only check one cell at a time, so we need it to be an Array Formlua . 但是,一个普通公式一次只能检查一个单元格,因此我们需要将其设为Array Formlua That is, it can scan through the given area one by one at a time. 也就是说,它可以一次一次扫描给定区域。

  2. So we have a bunch of row numbers now. 所以我们现在有很多行号。 We use formula MAX() to pick the biggest one, since the data are sorted by date already. 我们使用公式MAX()选择最大的一个,因为数据已经按日期排序。

  3. Now we have the Row number of the wanted date, and we know that the date is always in Column A . 现在我们有了所需日期的Row号,并且知道日期始终在Column A An Indirect() formula can easily put them together and get the date written in that cell. 一个Indirect()公式可以轻松地将它们放在一起,并获得在该单元格中写入的日期。

  4. DAYS() is designed for subtracting two dates. DAYS()用于减去两个日期。 First argument is the date where the formula is, and second argument is the date we found by step 1 to 4. 第一个参数是公式所在的日期,第二个参数是我们在步骤1至4中找到的日期。

  5. However there could be some entries that no date can be found. 但是,可能有一些条目找不到日期。 So we put an IFERROR() outside our formula for preventing any error codes. 因此,我们将IFERROR()放在公式之外,以防止出现任何错误代码。

  6. Remember this is an Array Formula , so press Ctrl + Shift + Enter to complete the formula. 请记住,这是一个数组公式 ,因此请按Ctrl + Shift + Enter键以完成公式。 And you should see a {} outside you formula if you have succeeded entering it correctly. 如果您已成功输入正确的公式,则应该在公式之外看到{}

  7. Lastly, fill this formula in your chart and maybe some adjustments if unlucky. 最后,在您的图表中填写此公式,如果不幸,还可以进行一些调整。

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

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