Sub topic 8
Approximate match for tiers and bands
This chapter assumes you can write a basic exact-match XLOOKUP and know how absolute references like $E$2 work. It uses the match_mode argument, which you met in the previous chapter.
When exact match is not enough
Some lookups have no exact entry to find. A score of 85 does not appear in a grade table, but it still belongs to a band. Commission rates, tax brackets and shipping fees work the same way. The table lists the edges of each band, and you want the band the value falls into.
Setting match_mode to -1 or 1 tells XLOOKUP to look for an exact match first. If there is none, it uses the nearest neighbor in one direction.
| match_mode | Result when no exact match exists |
|---|---|
| 0 | Nothing is returned, so you get `#N/A` or your `if_not_found` text. This is the default. |
| -1 | The next smaller item |
| 1 | The next larger item |
Lower-bound tables with -1
Suppose the grade table has the lower bound of each band in E2:E6 and the letter in F2:F6:
| Lower bound (E) | Grade (F) |
|---|---|
| 0 | F |
| 60 | D |
| 70 | C |
| 80 | B |
| 90 | A |
To grade the score in B2, use match_mode -1:
=XLOOKUP(B2, $E$2:$E$6, $F$2:$F$6, "Below minimum", -1)For a score of 85, there is no exact 85 in the table. XLOOKUP takes the largest bound that is less than or equal to 85, which is 80, and returns B. A score of exactly 90 matches exactly and returns A.
The fourth argument matters here. A score below the smallest bound, such as -5, has no smaller entry. XLOOKUP then returns "Below minimum" instead of #N/A.
Upper-limit tables with 1
Some tables list the top of each band instead. A shipping table might say "up to 1 kg", "up to 5 kg" and "up to 10 kg". An item weighing 3 kg belongs to the "up to 5 kg" band, which is the smallest limit that is at least 3.
=XLOOKUP(B2, $E$2:$E$6, $F$2:$F$6, "Above range", 1)Match_mode 1 returns the entry for the smallest value that is greater than or equal to the lookup value. A weight above the largest limit returns "Above range".
Why the table does not need sorting
VLOOKUP's approximate mode requires ascending order and gives wrong answers without it. XLOOKUP with the default search mode checks the table from the first item to the last, so the order of the rows does not affect the result. You can add or reorder bands freely.
Sorting only becomes a requirement when you choose a binary search, which a later chapter covers.
Common mistakes
- Wrong direction. Using
-1on an upper-limit table puts every value in the band below the correct one. Test a value that sits exactly on a boundary, such as 80 or 90. - Numbers stored as text. If the bounds are text like
"80", a numeric score cannot find them. Convert the bounds to real numbers first. - Unlocked ranges. When you copy the formula down, write the table ranges as
$E$2:$E$6and$F$2:$F$6. Otherwise the table moves with each row. - Missing fourth argument. Without
if_not_found, an out-of-range value returns#N/A. Decide whether you want that error or a message.
Try it yourself
Exercise
A commission table has lower bounds 0, 10000, 25000 and 50000 in E2:E5 and rates 2%, 4%, 6% and 8% in F2:F5. Sales are in B2 and equal 30000. Write a formula that returns the rate, giving 0 if the sales figure is negative. What does it return for 30000?
Show answer
=XLOOKUP(B2, $E$2:$E$5, $F$2:$F$5, 0, -1). There is no exact 30000, so match_mode -1 uses the largest bound that is not above it, which is 25000. The formula returns 6%. A negative sales figure has no smaller bound, so the formula returns 0.