2

I have two worksheets, one with two columns of values like this:

Num1    Num2    Result
0.01    0.99    
0.01    0.98    
0.01    0.95    
0.01    0.95    
0.01    0.94    
0.01    0.93    
0.01    0.91    
0.01    0.91    
0.01    0.91    
0.01    0.91    
0.01    0.9 
0.01    0.89    
0.01    0.87    
0.01    0.84    
...     ... 

And one with two columns and a value to look up, like this:

Num1    Num2    Country
0.01    0.99    Norway
0.01    0.80    Slovenia
0.01    0.41    Ukraine
0.02    0.65    Belarus
0.03    0.79    Slovakia
0.04    0.90    Iceland
0.04    0.72    Kazakhstan
0.05    0.88    Finland
...     ...     ...

My desired result is to populate column C in Sheet 1 with the country (from Sheet 2) whose Num1 and Num2 values most closely match those in columns A and B. Just from eyeballing the data I can see that exact pair matches are going to be rare, so it needs to be "fuzzy."

This is as far as I've got in terms of a formula for column C:

=INDEX(Lookup!$C$2:$C$123,MATCH(TRUE,INDEX(ABS(Lookup!$B$2:$B$123-Sheet1!B2)=MIN(INDEX(ABS(Lookup!$B$2:$B$123-Sheet1!B2),,)),,),0))

Obviously this isn't what I'm after, because it's not conditional on the Num1 columns matching, which I think it needs to be. But my thinking was that I could do this conditionally for Num1, then do it again in another column for Num2, and then do something clever with Vlookups to figure out the deltas between the pairs of Num1s and Num2s. Then I could use those deltas to select the conditional match that best reflected the "closest" value.

I have a feeling there's a better way to do that last bit, but I can't even get past step one. Thanks!

4
  • 1
    How are you defining "close match" here? For example, if you were looking for the "closest match" to: 0.01|0.99, then which of the following two would you consider the best candidate: 0.01|0.95 or 0.03|0.97? They both have a total difference of 0.04, though you might prefer the first since at least one of the two is an exact match, albeit at the cost of the other being not so good a match.
    – XOR LX
    Mar 7, 2016 at 10:08
  • Yeah, I think the first one makes the most sense to me. I guess the logic is "give me the closest value to Num1, and then the closest value to Num2." Although now that I think about it there's no reason why that should be the case - Num1 doesn't carry any more weight than Num2 in this exercise.
    – Nathaniel
    Mar 7, 2016 at 11:54
  • So what's your decision?
    – XOR LX
    Mar 7, 2016 at 12:04
  • Closest to Num1, and then closest to Num2. For this specific exercise I don't think the method used matters too much, though, so if the other way's easier that suits me.
    – Nathaniel
    Mar 8, 2016 at 0:59

1 Answer 1

0

Assuming that the results table and source table are in Sheet1!A1:C15 and Sheet2!A1:C9 respectively (both with headers in row 1), in Sheet1!C2, array formula**:

=INDEX(Sheet2!C$2:C$9,MATCH(MIN(IF(ABS(Sheet2!A$2:A$9-A2)=MIN(ABS(Sheet2!A$2:A$9-A2)),ABS(Sheet2!B$2:B$9-B2))),IF(ABS(Sheet2!A$2:A$9-A2)=MIN(ABS(Sheet2!A$2:A$9-A2)),ABS(Sheet2!B$2:B$9-B2)),0))

Copy down as required.

Regards

**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

3
  • Thanks, this works really well. I'm going to try to take it a bit further because there are a couple of cases where this approach isn't working. e.g. I've got a .99/.98 pair in my data, but there's only one .99 to match it to in the lookup, with a pair of .54. So Maybe lowest numerical distance is the way to go. Anyway, this gives me something to work with. Thanks!
    – Nathaniel
    Mar 8, 2016 at 22:29
  • Ok, I ended up taking a completely different approach. I treated the Num1/Num2 and the lookup Num1/Num2 values as two x/y coordinate pairs, and then just calculated the distance between them. I'm pretty sure this is valid, because they're percentile values. Then I just did some vlookups and matching to find the smallest distance. XOR LX's formula is still really nice, and answers the question as asked.
    – Nathaniel
    Mar 9, 2016 at 5:43
  • Ok, glad you've got it sorted, and I like the sound of your approach. To be honest, I was leaning towards an average absolute difference solution as opposed to that which I gave, which is why I initially asked for your opinion on that matter. Cheers.
    – XOR LX
    Mar 9, 2016 at 7:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .