简体   繁体   English

从两列中找到Excel索引/匹配项中的最近日期

[英]Find nearest date in excel index/match from two columns

I'm trying to get the nearest date from one column based on a date and id from another column. 我正在尝试根据另一列的日期和ID从一列获取最近的日期。 For example: 例如:

01-001  1/5/2017
01-002  12/28/2016
01-003  1/5/2017
01-001  8/21/2017

The second set of data would be: 第二组数据为:

01-002  11/4/2016
01-002  6/24/2016
01-002  12/25/2016
01-003  1/14/2017

For 01-002, I'd want to return 12/25/2016 from the second set of data. 对于01-002,我想从第二组数据返回12/25/2016。 There has to be some sort of array index match with Max to do this, I just can't put it all together. 必须与Max进行某种数组索引匹配才能做到这一点,我只是不能将它们全部放在一起。

These work! 这些工作! See photos below. 见下面的照片。 Unfortunately cannot see ranges highlighted without entering formula bar...which removes the array formula brackets { } 不幸的是,如果不输入公式栏就无法看到突出显示的范围...这将删除数组公式括号{}

You likely forgot to set the equation using the array formula. 您可能忘记了使用数组公式设置方程式。 You can do this by hitting CTRL SHIFT ENTER rather than enter. 您可以通过按CTRL SHIFT ENTER而不是Enter来做到这一点。 It would not have mattered though as the formula was broken! 没关系,因为公式被打破了! Sometimes tinkering gets you into trouble! 有时修补会惹上麻烦!

New equation. 新方程式。

{=INDEX(C7:C10,MATCH(1,(MIN(IF(C7:C10-E6>0,C7:C10-E6,(C7:C10-E6)*-1))=IF(C7:C10-E6>0,C7:C10-E6,(C7:C10-E6)*-1))*1,0))}

I am keying off of the matching date so as to avoid making this formula super huge. 我正在确定匹配日期,以免使这个公式变得太大。

Equation itself is simple enough: We take a range and subtract each cell in that range by another cells value. 公式本身很简单:我们取一个范围,然后用另一个单元格值减去该范围内的每个单元格。 If the output one of those calculations is negative we make it positive by multiplying by -1. 如果这些计算之一的输出为负,则将其乘以-1使其为正。 We do this twice. 我们这样做两次。 With one of these arrays we apply the MIN function, leaving us with the smallest number in that array. 对于这些数组之一,我们应用MIN函数,从而使该数组中的数字最小。 We then compare each cell in the remaining array with this min value. 然后,我们将剩余数组中的每个单元格与此最小值进行比较。 The result is an array with same number items as the array we just used in the comparison, filled with trues and falses, that is equal to or not equal to our min. 结果是一个数组,该数组具有与我们刚才在比较中使用的数组相同的项,并填充有对与错,等于或不等于最小值。 We then take array and multiply each item in it by 1, thereby converting the true/false values into binary 0 and 1s. 然后,我们采用数组并将其中的每个项目乘以1,从而将true / false值转换为二进制0和1s。 We then match this array with 1. the first match, 1 with a 1 in the array, will get us our row. 然后,我们将此数组与1匹配。第一个匹配(数组中的1与1)将获得我们的行。

在此处输入图片说明

在此处输入图片说明

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

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