Sub topic 10
Searching first to last or last to first
This chapter covers the sixth argument of XLOOKUP, search_mode. It controls the direction XLOOKUP scans the lookup array, and it decides which row wins when a value appears more than once. It assumes you can write a basic XLOOKUP, you know the match_mode argument from earlier chapters, and you use absolute references such as $E$2 without trouble. You need Excel for Microsoft 365, Excel 2021 or later, or Excel for the web.
Why direction matters
A lookup returns one result, but real data often holds a value more than once. A customer places several orders. A product's price changes several times. An employee appears in a log on many days.
In those cases the question "which row do I get?" has a real answer, and the answer depends on the search direction. By default XLOOKUP scans from the first item to the last and stops at the first match. Any later duplicates are never read.
The search_mode argument lets you change that behavior.
The search_mode argument
Here is the full syntax with search_mode in position six:
=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])The argument takes one of four values.
| Value | Behavior | Data requirement |
|---|---|---|
| 1 | Search first to last. This is the default. | None |
| -1 | Search last to first. | None |
| 2 | Binary search, ascending order. | Lookup array sorted ascending |
| -2 | Binary search, descending order. | Lookup array sorted descending |
This chapter focuses on 1 and -1. Values 2 and -2 are covered in the binary search chapter. They change speed and assumptions about sorting, not just direction.
The default: first to last
When you omit search_mode, or pass 1, XLOOKUP reads the lookup array from the top and returns the first match it finds.
Suppose a sheet records price changes for products, oldest at the top:
| Row | A (Product) | B (Date) | C (Price) |
|---|---|---|---|
| 1 | Product | Date | Price |
| 2 | Widget | 2026-01-05 | 10.00 |
| 3 | Gadget | 2026-01-12 | 25.00 |
| 4 | Widget | 2026-03-02 | 11.50 |
| 5 | Widget | 2026-08-19 | 12.75 |
| 6 | Gadget | 2026-09-01 | 27.00 |
With "Widget" in E2, this formula returns the first Widget row:
=XLOOKUP(E2, A2:A6, C2:C6)The result is 10.00, the January price. That is the oldest price, which is rarely what you want when asking for the current one.
Returning the last match
To get the most recent price, search from the bottom. Set search_mode to -1:
=XLOOKUP(E2, A2:A6, C2:C6, "None", 0, -1)XLOOKUP now starts at row 6, works upward, and stops at the first Widget it meets. That is row 5, so the result is 12.75.
Notice the arguments in between. You cannot skip to the sixth position without filling the fourth and fifth, at least with placeholders:
"None"is theif_not_foundtext.0ismatch_mode, which is exact match and the default.
If you prefer to leave if_not_found at its default behavior, leave that slot empty and keep the comma:
=XLOOKUP(E2, A2:A6, C2:C6, , 0, -1)An empty slot means "use the default", so a missing value still returns #N/A.
How the scan works
The lookup array is the same in both cases. Only the starting point and the stepping direction change. The diagram below shows the two scans over the Widget and Gadget data when you look up "Widget".
Two details follow from this.
- XLOOKUP stops at the first hit in its scan direction. It does not compare duplicates or pick the "best" one.
- The order of your data is therefore part of your formula's logic. If someone re-sorts the table, a
-1formula can silently return a different row.
Last match depends on row order, not dates
A common mistake is to assume -1 returns the latest date. It returns the lowest row in the range that matches. That equals the latest date only when the data is sorted by date ascending.
If your table is not in date order, there are two safe options:
- Sort the table by date ascending before you rely on
search_mode-1. - Use a different technique that tests the dates explicitly, such as a
MAXIFSformula that finds the latest date and a lookup on that.
Here is the second option for the current price of the product in E2:
=XLOOKUP(1, (A2:A6=E2)*(B2:B6=MAXIFS(B2:B6, A2:A6, E2)), C2:C6, "None")The expression (A2:A6=E2)*(B2:B6=MAXIFS(...)) builds an array of 1s and 0s. A 1 appears only on the row where both the product and the date match. XLOOKUP then looks for that 1. This formula does not depend on row order, because the date test does the work. It does assume a product has only one row on its latest date.
Combining search_mode with match_mode
The two modes are independent. You can combine direction with approximate or wildcard matching.
For example, to find the last entry whose text contains the value in E2:
=XLOOKUP("*"&E2&"*", A2:A100, C2:C100, "No match", 2, -1)Here match_mode 2 turns on wildcards and search_mode -1 makes the scan run bottom to top, so you get the last row that contains the text.
With approximate match, direction also matters when duplicates exist. If two rows both qualify as the next smaller value, the scan direction decides which of them XLOOKUP returns. In a clean tier table with unique bounds this never comes up, which is another reason to keep band thresholds unique.
Performance
For a plain linear scan, direction has no meaningful speed difference in typical workbooks. The cost is proportional to how far XLOOKUP has to read before it hits a match. This leads to a practical point.
- If you usually want the newest record and the newest records sit at the bottom,
-1often finds the match sooner than a top-down scan. - On very large ranges, binary search (
2or-2) is much faster, but only when the data is sorted. The binary search chapter explains how.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Formula returns the oldest record instead of the newest | `search_mode` left at the default 1 while the newest row is lower in the sheet | Add `-1` as the sixth argument |
| Formula returns the wrong "latest" row after the table is re-sorted | `-1` relies on row order, not on dates | Keep the table in date order, or use a date-testing formula such as the MAXIFS version above |
| `#VALUE!` or an unexpected result when adding the sixth argument | The fourth or fifth argument was left out, so the `-1` landed in the wrong slot | Fill or leave empty the earlier slots so `-1` is the sixth argument |
| Wrong result with `2` or `-2` | The lookup array is not sorted the way binary search requires | Sort the array, or use `1` or `-1` |
| `#N/A` for a value you can see in the data | Data-type or stray-space mismatch, not search direction | See the earlier chapter on common errors and data-type pitfalls |
Worked example: latest order status
Suppose an order log lists one row per status update, oldest first:
| Row | A (Order) | B (Status) |
|---|---|---|
| 2 | 1001 | Placed |
| 3 | 1002 | Placed |
| 4 | 1001 | Packed |
| 5 | 1001 | Shipped |
| 6 | 1002 | Packed |
To show the current status of the order whose number is in D2, search from the bottom:
=XLOOKUP(D2, A2:A6, B2:B6, "Unknown order", 0, -1)For order 1001 the scan starts at row 6 (order 1002, skip), then row 5 (order 1001, match). The result is "Shipped". For 1002 it matches row 6 immediately and returns "Packed". For an order number that is not in the log, the result is "Unknown order".
If the log grows by appending rows at the bottom, the formula keeps returning the newest status without any change, provided you extend the ranges. Converting the data to an Excel Table, or using whole-column references, keeps the ranges current.
Exercise
A table in A2:A100 lists customer names, with the oldest purchase first. Column C holds the purchase amount. The name to look up is in F2. Write a formula that returns the amount of that customer's most recent purchase, shows "No purchases" if the name is missing, and explain why the default formula returns the wrong amount.
Show answer
=XLOOKUP(F2, A2:A100, C2:C100, "No purchases", 0, -1). The default searchmode of 1 scans from the top and stops at the first match, which is the customer's oldest purchase. Setting searchmode to -1 scans from the bottom, so the first match it reaches is the newest row. This depends on the table being in date order, oldest at the top.
Exercise
Why does the formula =XLOOKUP(E2, A2:A6, C2:C6, -1) not search from last to first, and what does it do instead?
Show answer
The -1 is in the fourth position, which is ifnotfound, not the sixth. If E2 is not found, the formula returns -1 as the fallback value. When E2 is found, it still returns the first match. To search from last to first, write =XLOOKUP(E2, A2:A6, C2:C6, "None", 0, -1).