Reputation: 361
I'm trying to get the nearest date from one column based on a date and id from another column. 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. There has to be some sort of array index match with Max to do this, I just can't put it all together.
Upvotes: 0
Views: 2422
Reputation: 2282
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. 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. We do this twice. With one of these arrays we apply the MIN function, leaving us with the smallest number in that array. 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. We then match this array with 1. the first match, 1 with a 1 in the array, will get us our row.
Upvotes: 2