Reputation: 822
This is a simplified version of my 2 tables:
I need to be able to fill in the missing values in Table2 based on the closest match of the adjacent measurement value to those values in Table1 for that measurement & type.
For example, in row 18, I need to look up the closest match of cell B18 [4.3] to cells B3 and B11 (since those two are the only type A records), and return the adjacent measurement2 of the closest match into cell C18. Or, in row 19, I need to look up the closest match of cell C19 [2.3] in cells C6:C8 and return the adjacent measurement1 into cell B19.
Upvotes: 1
Views: 3284
Reputation: 71598
Ok, I don't know if it can be further simplified (there are some calculations made twice in the formula), but I came up with this for cell C18:
=INDEX(C$3:C$12,MATCH(9^99,IF(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))),C$3:C$12)))
And this for cell B19:
=INDEX(B$3:B$12,MATCH(9^99,IF(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19))=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),B$3:B$12)))
EDIT2: As barryhoudini pointed out, there was a mistake in there which his formula corrected. I edited the above two formulas. Otherwise, as he correctly mentioned, you can use this formula for C18 as well:
=LOOKUP(9^99,IF(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))),C$3:C$12))
Notes:
Those are array formulas and will work only with Ctrl+Shift+Enter instead of only Enter.
The formula will get the last match in the table, in this case for A with measurement1 of 4.3, it will return 2.8, in cases where there's the same difference.
EDIT1: Okay, for consistency, I came up with this one to always get the higher match in B19:
EDIT3: And used LOOKUP
instead of INDEX
/MATCH
for a little shorter formula and also fixed the first column issue.
=LOOKUP(9^99,IF(MAX(IF(ABS($C$3:$C$12-$C19)=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),C$3:C$12))=IF(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19))=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),C$3:C$12),IF(ABS($C$3:$C$12-$C19)=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),B$3:B$12)))
And this one for C18:
=LOOKUP(9^99,IF(MAX(IF(ABS($B$3:$B$12-$B18)=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),B$3:B$12))=IF(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),B$3:B$12),IF(ABS($B$3:$B$12-$B18)=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),C$3:C$12)))
For the lower match, B19:
=LOOKUP(9^99,IF(MIN(IF(ABS($C$3:$C$12-$C19)=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),C$3:C$12))=IF(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19))=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),C$3:C$12),IF(ABS($C$3:$C$12-$C19)=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),B$3:B$12)))
And for C18:
=LOOKUP(9^99,IF(MIN(IF(ABS($B$3:$B$12-$B18)=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),B$3:B$12))=IF(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),B$3:B$12),IF(ABS($B$3:$B$12-$B18)=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),C$3:C$12)))
EDIT4: For two types:
=LOOKUP(9^99,IF(MAX(IF(ABS($C$3:$C$12-$C19)=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),C$3:C$12))=IF(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19))=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),C$3:C$12),IF(ABS($C$3:$C$12-$C19)=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),B$3:B$12)))
And this one for C18:
=LOOKUP(9^99,IF(MAX(IF(ABS($B$3:$B$12-$B18)=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),B$3:B$12))=IF(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18))=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),B$3:B$12),IF(ABS($B$3:$B$12-$B18)=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),C$3:C$12)))
For the lower match, B19:
=LOOKUP(9^99,IF(MIN(IF(ABS($C$3:$C$12-$C19)=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),C$3:C$12))=IF(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19))=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),C$3:C$12),IF(ABS($C$3:$C$12-$C19)=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),B$3:B$12)))
And for C18:
=LOOKUP(9^99,IF(MIN(IF(ABS($B$3:$B$12-$B18)=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),B$3:B$12))=IF(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18))=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),B$3:B$12),IF(ABS($B$3:$B$12-$B18)=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),C$3:C$12)))
Upvotes: 2
Reputation: 26670
I would set up table 2 a bit differently, like so:
The array formula in cell D18 and copied down is:
=INDEX(IF(B18=$B$2,$C$3:$C$12,$B$3:$B$12),MATCH(MIN(IF($A$3:$A$12=A18,ABS(IF(B18=$B$2,$B$3:$B$12,$C$3:$C$12)-C18))),IF($A$3:$A$12=A18,ABS(IF(B18=$B$2,$B$3:$B$12,$C$3:$C$12)-C18)),0))
In order to confirm an array formula you will need to use Ctrl+Shift+Enter instead of just Enter.
Upvotes: 3