XLOOKUP
Short answer
Map every GL row to its statement line
XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])
- Category
- Lookup & Reference
- Excel version
- 2021+
- Also searched as
- vlookup replacement, lookup, mapping table lookup
What it returns
XLOOKUP finds lookup_value in lookup_array and returns whatever sits in the same position of return_array. The two ranges are separate arguments, so the return column can be anywhere: left of the key, right of it, on another tab. Exact match is the default. If nothing matches, you get #N/A, or whatever you put in if_not_found.
In a model, that makes it the function for attaching an attribute to a key. Account code to statement line. Cost center to function. Tranche to margin. One key in, one attribute out.
It is the wrong function for pulling a value by period. Periods have duplicates and gaps, and XLOOKUP returns the first hit or an error. Use SUMIFS there. See the play on SUMIFS over lookups for period math.
Video: the XLOOKUP Short on YouTube shows the core pattern in under a minute.
Arguments that matter
| Argument | Required | What it does |
|---|---|---|
lookup_value | Yes | The key you are looking for. In a schedule this is usually the code in the current row, anchored to its column: $B12. |
lookup_array | Yes | The single column or row that holds the keys. Map[Code]. Must be the same length as return_array. |
return_array | Yes | The column that holds the answer. Map[Line]. Can be several adjacent columns, in which case the result spills. |
if_not_found | No | What to return when the key is missing. Skip it and you get #N/A. |
match_mode | No | 0 exact (default). -1 exact or next smaller. 1 exact or next larger. 2 wildcard. |
search_mode | No | 1 first to last (default). -1 last to first. 2 and -2 are binary search on sorted data. |
Three of these deserve attention.
if_not_found is the argument that separates a model from a spreadsheet. Set it to a visible flag, "UNMAPPED", and count the flags in your check row. Set it to "" or 0 and a missing mapping drops out of the totals with no trace. Leave it empty and one new account turns the whole statement into #N/A, which at least gets noticed.
match_mode of -1 handles tiered tables: a leverage grid, a tax bracket, a volume discount. Excel returns the exact threshold or the next one below it. Unlike approximate VLOOKUP, the table does not need to be sorted.
search_mode of -1 returns the last match instead of the first. Useful when a log holds multiple rates per key and the newest is at the bottom. Dangerous when you did not know the key was duplicated.
In a financial model
The core use is the mapping table. A trial balance arrives with account codes. The statements need line items. Somewhere between the two, every code has to resolve to exactly one line.
The play on one mapping table per dimension says where that logic lives: one Excel Table, one owner, referenced everywhere. XLOOKUP is how the trial balance reads it. Each TB row pulls its line item with one formula, and SUMIFS keyed on the line item builds the statement from there.
The same pattern covers every attribute lookup in the model:
- Debt schedule: tranche code to margin, base rate, maturity.
- Headcount roll: role to fully loaded cost, department to cost center.
- FX: currency to rate for the period.
- Capex: asset class to useful life.
Each of these has a small table with a unique key and a handful of attributes. XLOOKUP reads them. Nested IFs and hard-coded rates do not belong in the calculation block.
Worked example
A trial balance on tab TB, formatted as a Table with columns Code, Name, Amount. A mapping table on tab Map, also a Table, with columns Code, Line, Statement.
Add a Line column to the TB:
=XLOOKUP([@Code], Map[Code], Map[Line], "UNMAPPED")
Then the check, in the model’s check row:
=COUNTIF(TB[Line], "UNMAPPED")
Anything above zero means a new account nobody mapped. It shows up the day the TB lands, not at close.
The income statement then reads the TB by line, not by code:
=SUMIFS(TB[Amount], TB[Line], $B12)
Three formulas. The mapping table owns the logic, the TB carries the result, and the statement sums it.
A second pattern, the tiered grid. A pricing schedule has leverage thresholds in Grid[Threshold] and margins in Grid[Margin]. To pull the margin for the current period’s leverage in F20:
=XLOOKUP(F20, Grid[Threshold], Grid[Margin], "CHECK", -1)
The -1 returns the margin for the highest threshold at or below F20. Thresholds are lower bounds. If leverage falls below the lowest threshold, "CHECK" fires instead of a silently wrong rate.
Old way vs new way
The legacy mapping column used INDEX and MATCH, wrapped in IFERROR to catch new codes:
=IFERROR(INDEX(Map[Line], MATCH([@Code], Map[Code], 0)), "UNMAPPED")
The XLOOKUP version above does the same job in one call. Its if_not_found argument only catches a missing key, so a #REF! from a broken range still surfaces instead of being relabelled as unmapped.
Where it breaks
Duplicated keys. XLOOKUP returns the first match and says nothing about the second. A mapping table with account 6100 listed twice, pointing at two different lines, returns whichever row is higher. Guard the key column: =ROWS(Map[Code])-ROWS(UNIQUE(Map[Code])) in the check row, expected to be zero.
Text against numbers. A code stored as the number 4000 will not match the text "4000". Exports from the GL often come through as text while the mapping table holds numbers, or the other way around. The symptom is if_not_found firing on every row. Fix the type at the source, in Power Query or a helper column, not by wrapping the formula in VALUE.
IFERROR around it. IFERROR(XLOOKUP(...), "") catches the missing key. It also catches #REF! when someone deletes the mapping column, and #VALUE! when the two arrays no longer match in length. All three look like a blank cell. Use if_not_found for the missing-key case. If you need an outer wrapper for some other reason, use IFNA so structural errors still surface.
Array length mismatch. lookup_array and return_array must be the same size. Referencing a whole column for one and a Table column for the other gives #VALUE!. Inserting rows across one range but not the other does the same thing, which is one more reason to use Tables.
Spilling inside a Table. A multi-column return_array spills, and Tables do not allow spilled results. Inside a Table, use one XLOOKUP per column.
Legacy Excel. XLOOKUP needs 365 or 2021. Send the file to Excel 2019 and every instance reads #NAME?. If the model will travel, use INDEX and MATCH, which behave the same way and work everywhere.
Related functions
- INDEX and MATCH. Same separate-range logic, available in every Excel version. The fallback when the file has to open in 2019 or earlier.
- XMATCH. Returns the position instead of the value. Use it when you need the row number for a check or for a second INDEX.
- VLOOKUP. What XLOOKUP replaces. The column-number argument breaks when a column is inserted, and it cannot look left.
- SWITCH. Fine for a handful of fixed codes inside a formula. Past about eight, the codes belong in a table that XLOOKUP reads.
- SUMIFS. For pulling values by period, where duplicates should add and gaps should read zero.
