Sub topic 11
Two-way lookups with nested XLOOKUP
A two-way lookup finds a value at the intersection of a row and a column. You pick the row by one label and the column by another. This chapter assumes you can write a basic XLOOKUP, use absolute references such as $E$2, and run Excel for Microsoft 365, Excel 2021 or later, or Excel for the web.
The problem: a grid with two keys
Suppose a sales grid has product names down column A and month names across row 1. To find the sales for one product in one month, you need to match on both. A single XLOOKUP matches one key. To use two keys, you nest one XLOOKUP inside another.
| Row | A | B | C | D | E |
|---|---|---|---|---|---|
| 1 | Product | Jan | Feb | Mar | Apr |
| 2 | Apples | 120 | 135 | 150 | 160 |
| 3 | Pears | 90 | 95 | 110 | 105 |
| 4 | Plums | 60 | 70 | 65 | 80 |
| 5 | Figs | 40 | 45 | 55 | 50 |
| 6 | Kiwis | 30 | 35 | 38 | 42 |
Assume G1 holds a product name such as Plums and G2 holds a month such as Mar.
The formula
=XLOOKUP(G1, A2:A6, XLOOKUP(G2, B1:E1, B2:E6))The formula has two parts.
- The inner XLOOKUP,
XLOOKUP(G2, B1:E1, B2:E6), searches the header row for the month. It returns the whole column ofB2:E6that sits under the matching header. ForMar, that is the three-letter columnD2:D6, holding 150, 110, 65, 55 and 38. - The outer XLOOKUP,
XLOOKUP(G1, A2:A6, ...), uses that column as its return array. It finds the product inA2:A6and returns the value at the same position. ForPlums, that is the third item, so the result is 65.
The key idea is that a return array can be the output of another formula. The inner call hands the outer call a single column to read from.
Reading the formula from the inside out
Excel evaluates the inner call first. When you debug, test it alone in a spare cell. Enter =XLOOKUP(G2, B1:E1, B2:E6) and check that it spills a column of five values. If it does, the column choice works. Then wrap it with the outer call.
The same pattern works the other way. You can place the row lookup inside and the column lookup outside:
=XLOOKUP(G2, B1:E1, XLOOKUP(G1, A2:A6, B2:E6))Here the inner call returns the matching row, such as B4:E4 for Plums, and the outer call picks the month from that row. Both versions give the same result. Choose the one that is easier for you to read.
Making it safer
Add an if_not_found argument to each call, so a typo in a label produces a clear message and not #N/A. Because the inner result is the return array of the outer call, an inner failure would pass its error outward. Setting the argument on both calls is cheap protection.
=XLOOKUP(G1, A2:A6, XLOOKUP(G2, B1:E1, B2:E6, "Bad month"), "Bad product")If the month is wrong, the inner call returns the text Bad month. The outer call then looks up the product and returns that text from its return array. This gives a useful message, though it can produce an odd spill, so check the result on a bad month before relying on it.
Common mistakes
- Mismatched sizes. The inner return array must have the same number of rows as the outer lookup array. Here both cover rows 2 to 6. If they differ, the formula returns
#VALUE!. - Header spelling and spaces.
Marwith a trailing space does not matchMar. Exact matching is the default, so labels must agree exactly, apart from letter case. - Mixed types. A month stored as text will not match a real date. Keep both sides as the same type.
- Relative references when copying. Lock the ranges with
$, as in$A$2:$A$6, if you copy the formula down a report.
Exercise
Exercise
A table has employee names in A2:A8, quarter names (Q1 to Q4) in B1:E1, and bonus amounts in B2:E8. Cell H1 holds an employee name and H2 holds a quarter. Write a formula that returns that employee's bonus for that quarter, and say which part runs first.
Show answer
=XLOOKUP(H1, A2:A8, XLOOKUP(H2, B1:E1, B2:E8)). The inner XLOOKUP runs first. It finds the quarter in B1:E1 and returns that column of B2:E8. The outer XLOOKUP then finds the employee in A2:A8 and returns the value in the same position of that column.