Sub topic 11
Two-Way Lookups with Nested XLOOKUP
This chapter assumes you can write a basic XLOOKUP and know the if_not_found and match_mode arguments from earlier chapters. You should also be comfortable with absolute references such as $E$2 and have Excel for Microsoft 365, Excel 2021 or later, or Excel for the web.
What a two-way lookup is
A one-way lookup finds a value in one list and returns the item beside it. A two-way lookup, also called a matrix lookup, uses two keys. One key picks the row and the other picks the column. The answer is the cell where that row and column cross.
Common examples include:
- Sales by region and quarter.
- Shipping cost by weight band and zone.
- A price by product and size.
XLOOKUP has no second "column" argument, so you nest two XLOOKUP calls. The inner call picks a column. The outer call picks a row from that column.
The sample data
The examples use this grid. The corner cell A1 holds a label, the quarter names are in B1:E1, and the region names are in A2:A6.
| Region | Q1 | Q2 | Q3 | Q4 |
|---|---|---|---|---|
| North | 120 | 135 | 150 | 180 |
| South | 90 | 95 | 110 | 130 |
| East | 105 | 115 | 125 | 140 |
| West | 80 | 85 | 100 | 120 |
| Online | 60 | 90 | 140 | 210 |
Two input cells drive the lookup. G1 holds a region name such as East. G2 holds a quarter name such as Q3. Labels in F1 and F2 can say "Region" and "Quarter".
The nested formula
=XLOOKUP(G1, A2:A6, XLOOKUP(G2, B1:E1, B2:E6))With East in G1 and Q3 in G2, the formula returns 125. Work through it from the inside out.
Step 1: the inner XLOOKUP picks a column
=XLOOKUP(G2, B1:E1, B2:E6)The lookup array is the header row B1:E1. It is horizontal, so XLOOKUP compares it to the return array by columns. Q3 is the third header, so the inner call returns the third column of B2:E6. That column is a vertical array of five values:
| Position | Value |
|---|---|
| 1 | 150 |
| 2 | 110 |
| 3 | 125 |
| 4 | 100 |
| 5 | 140 |
Step 2: the outer XLOOKUP picks a row
The outer call now behaves as if its return array were that five-value column:
=XLOOKUP("East", A2:A6, {150;110;125;100;140})East is the third item in A2:A6, so the outer call returns the third value in the column, which is 125.
The inner call is evaluated first. The outer call receives its result as the return_array argument.
Why the shapes must agree
Both XLOOKUP calls rely on a shape rule. The return array must have the same length as the lookup array along the search direction.
- The inner call searches B1:E1, which is 4 columns wide. The return array B2:E6 is also 4 columns wide.
- The outer call searches A2:A6, which is 5 rows tall. The inner call's result is a column with 5 rows.
If you mismatch the ranges, the formula returns #VALUE!. A common mistake is a return range that includes the header row, such as B1:E6. Then the inner result has 6 rows and the outer lookup array has 5. Another mistake is starting the row labels at A1 while the data starts at B2.
Swapping the nesting order
You can nest in the other direction, with the outer call choosing the column and the inner call choosing the row:
=XLOOKUP(G2, B1:E1, XLOOKUP(G1, A2:A6, B2:E6))Here the inner call returns one row of B2:E6, a horizontal array of 4 values. The outer call then picks one value from it by header position. Both formulas give 125 for East and Q3.
Neither order is more correct. Choose the one that matches how you think about the problem, or the one where the inner result is easier to inspect.
Handling missing keys
If either key is missing, the result is #N/A. The place you handle it matters, because the two calls do not behave the same way.
- An error from the inner call travels into the outer call as an argument. The outer call returns that same error, and its own
if_not_founddoes not catch it. - Setting
if_not_foundon the inner call to a text message does not help. The outer call then receives a single text value instead of a five-row column. The shapes no longer agree, and you get#VALUE!.
The reliable approach is to wrap the whole formula:
=IFNA(XLOOKUP(G1, A2:A6, XLOOKUP(G2, B1:E1, B2:E6)), "Check region or quarter")IFNA catches only #N/A. A #VALUE! caused by misaligned ranges still shows, so a real mistake in the formula is not hidden. IFERROR would hide it too.
This wrapper cannot tell you which key was missing. To report that, check the keys separately:
=IF(ISNA(MATCH(G1, A2:A6, 0)), "Unknown region",
IF(ISNA(MATCH(G2, B1:E1, 0)), "Unknown quarter",
XLOOKUP(G1, A2:A6, XLOOKUP(G2, B1:E1, B2:E6))))Using match modes on each axis
Each XLOOKUP has its own match_mode, so each axis can use a different rule. This lets you build lookups where one key must match exactly and the other falls into a band.
Suppose a shipping table has weight bands in A2:A5 (0, 5, 10 and 20 kg) and zone numbers 1 to 4 in B1:E1. The prices are in B2:E5. The weight in G1 should use the next smaller band, and the zone in G2 should match exactly:
=XLOOKUP(G1, A2:A5, XLOOKUP(G2, B1:E1, B2:E5), "Out of range", -1)A weight of 12 kg finds the 10 kg band, because -1 returns the largest bound that is less than or equal to the lookup value. A weight below 0 returns "Out of range".
In this case if_not_found on the outer call does work. A missing weight is a miss of the outer lookup itself, not an error passed in from the inner call. A missing zone would still produce #N/A. Wrap the formula in IFNA if you want to handle that case too.
You can also add approximate matching to the inner call. Pass -1 as its match_mode and skip the unused if_not_found argument with an empty slot:
=XLOOKUP(G1, A2:A5, XLOOKUP(G2, B1:E1, B2:E5, , -1))This suits a grid where both the row keys and the column keys are numeric thresholds.
Returning a whole row or column
Sometimes you need only one half of the lookup. The inner formula alone returns a whole column. Used by itself, it spills into the cells below:
=XLOOKUP(G2, B1:E1, B2:E6)The row version is the one-key lookup you already know:
=XLOOKUP(G1, A2:A6, B2:E6)Wrapping either one in a function gives totals and averages without a helper range:
=SUM(XLOOKUP(G2, B1:E1, B2:E6))This sums the chosen quarter across all regions. Because XLOOKUP returns references, not copies, this works well inside functions that expect ranges.
Common errors and checks
| Symptom | Likely cause | Fix |
|---|---|---|
| `#N/A` | A key is not in its axis, or has a stray space, or differs in data type | Check spelling, use `TRIM`, and make sure numbers are not stored as text |
| `#VALUE!` | The return array and lookup array differ in length | Align the ranges so header and label ranges match the data block |
| Wrong value, no error | Duplicate labels. XLOOKUP returns the first match on each axis | Make labels unique, or use `search_mode` -1 to prefer the last |
| Value shifts when copied | Ranges are relative | Anchor with `$`, for example `$A$2:$A$6` |
| Spill error `#SPILL!` | A key cell holds a multi-cell range, so the result tries to spill into occupied cells | Use single-cell keys or clear the cells below |
A typical data-type problem is headers such as 2024 typed as numbers while the input cell holds the text "2024". They look identical but do not match. Test with =ISNUMBER(G2) and convert with VALUE or --.
Also remember that XLOOKUP ignores case in text. east and East match the same row.
Comparison with INDEX/MATCH
The classic two-way formula is:
=INDEX(B2:E6, MATCH(G1, A2:A6, 0), MATCH(G2, B1:E1, 0))It gives the same result and works in every Excel version. Here is how the two approaches compare.
| Point | Nested XLOOKUP | INDEX/MATCH |
|---|---|---|
| Availability | Microsoft 365, Excel 2021 and later, Excel for the web | All versions |
| Reading order | Keys sit next to their lookup ranges | Keys and ranges are separated across two MATCH calls |
| Missing key | One `IFNA` wrapper (see above) | One `IFNA` wrapper, same limits |
| Match options | A separate `match_mode` for each axis | `match_type` of 0, 1 or -1 on each MATCH |
| Misaligned ranges | `#VALUE!` | A wrong cell, or `#REF!` |
The practical difference is that INDEX/MATCH counts positions. It does not check that the ranges line up. If you shift one range by a row, INDEX/MATCH quietly returns the neighboring cell. Nested XLOOKUP flags a size mismatch as an error. That is safer, though it only catches mismatched lengths and not ranges shifted together.
If anyone who opens the file may use Excel 2019 or earlier, use INDEX/MATCH. In those versions XLOOKUP shows as #NAME?.
Step-by-step build checklist
- Put the corner label in A1, the row labels in A2:A6 and the column headers in B1:E1.
- Enter the row key in G1 and the column key in G2.
- Write the inner call first,
=XLOOKUP(G2, B1:E1, B2:E6), and confirm it spills one column of 5 values. - Delete the test spill, then wrap the inner call in the outer call.
- Anchor all ranges with
$if you will copy the formula. - Add
IFNAand test a missing row key, a missing column key and a key with a trailing space.
Exercise
Using the sample grid, what does =XLOOKUP("Online", A2:A6, XLOOKUP("Q2", B1:E1, B2:E6)) return, and which formula returns the same value with INDEX/MATCH?
Show answer
It returns 90. The inner call returns the Q2 column (135, 95, 115, 85, 90), and the outer call picks the fifth value because "Online" is the fifth row label. The INDEX/MATCH equivalent is =INDEX(B2:E6, MATCH("Online", A2:A6, 0), MATCH("Q2", B1:E1, 0)).
Exercise
A colleague changes the inner range to B1:E6 (including the header row) so the formula reads =XLOOKUP(G1, A2:A6, XLOOKUP(G2, B1:E1, B1:E6)). What happens, and why?
Show answer
The result is #VALUE!. The inner call now returns a column of 6 cells (the header plus five data rows), but the outer lookup array A2:A6 has only 5 cells. The return array must have the same length as the lookup array. Changing B1:E6 back to B2:E6 fixes it.
Exercise
Write a formula for the shipping table (weight bands in A2:A5, zones 1 to 4 in B1:E1, prices in B2:E5) that matches the zone in G2 exactly, uses the next smaller weight band for the weight in G1, and shows "Out of range" for weights below the first band.
Show answer
=XLOOKUP(G1, A2:A5, XLOOKUP(G2, B1:E1, B2:E5), "Out of range", -1). The inner call matches the zone exactly by default. The outer call uses matchmode -1 for the next smaller band, and its ifnot_found catches weights below the first band.
Exercise
Why does putting ifnotfound text on the inner XLOOKUP fail to give a friendly message when the column key is missing, and what is a better approach?
Show answer
The inner call would return a single text value, and the outer call expects a return array with the same number of rows as its lookup array, so the result is #VALUE! instead of a message. Wrap the whole formula in IFNA, or test each key with MATCH inside IF statements to report which key is missing.