Sub topic 8
Search modes: finding the last match
Assume you can enter formulas in Google Sheets and use cell and range references. This chapter focuses on XLOOKUP search modes. A search mode controls the direction in which Sheets scans the lookup range. It is different from a match mode, which controls whether the match must be exact or can be approximate.
Why the search direction matters
Suppose a product appears several times in a sales log. A normal lookup returns the first matching row. If the log is ordered by date, the first row may contain an old price or status. You may need the most recent matching row instead.
XLOOKUP searches from the top down by default. To search from the bottom up, set its search_mode argument to -1.
The full syntax is:
=XLOOKUP(search_key, lookup_range, result_range, missing_value, match_mode, search_mode)The final argument controls the direction:
| Search mode | Meaning | Typical use |
|---|---|---|
| `1` | Search from first row to last row | Return the first match |
| `-1` | Search from last row to first row | Return the last match |
| `2` | Binary search in ascending data | Faster search in sorted data |
| `-2` | Binary search in descending data | Faster search in reverse-sorted data |
For ordinary unsorted data, use 1 or -1. Binary search modes require correctly sorted data. If the data is not sorted as required, the result can be incorrect.
Returning the last matching row
Imagine this data in columns A through C:
| A | B | C |
|---|---|---|
| Product ID | Date | Status |
| P-102 | 2026-01-04 | Ordered |
| P-205 | 2026-01-05 | Shipped |
| P-102 | 2026-01-09 | Delivered |
| P-102 | 2026-01-12 | Returned |
If cell E2 contains P-102, this formula returns the first status, Ordered:
=XLOOKUP(E2, A2:A100, C2:C100, "Not found")The formula uses the default search mode, 1. To return the last status, use -1 as the sixth argument:
=XLOOKUP(E2, A2:A100, C2:C100, "Not found", 0, -1)Here, 0 explicitly requests an exact match. The final -1 tells Sheets to search upward from the bottom. The result is Returned.
The formula does not automatically know that the last row is the newest record. It simply returns the last matching row in the current range. Therefore, this method works for “latest” data only when the rows are arranged in the required order.
How the search works
Search mode versus match mode
These arguments answer different questions:
match_modeasks, “What counts as a match?”search_modeasks, “Where should the search begin and in which direction?”
For example, this formula requires an exact match and searches from the bottom:
=XLOOKUP(E2, A2:A100, C2:C100, "Not found", 0, -1)If you omit the 0, XLOOKUP still uses exact matching by default. Including it makes the formula's intent easier to read.
VLOOKUP has no separate bottom-up search argument. It normally returns the first matching row. To return the last match with VLOOKUP, you would need a more complex formula or reorganize the data. XLOOKUP states the requirement directly.
Exercise
In a log where the newest record is last, which formula returns the latest status for the key in E2?
Show answer
=XLOOKUP(E2, A2:A100, C2:C100, "Not found", 0, -1)