How-to recipes · Lookups
How to get the nth match in Excel
Return the 2nd, 3rd or any n-th matching value instead of the first, with INDEX and FILTER in Microsoft 365 or AGGREGATE in Excel 2016 and 2019.
Lookup functions return the first match. Sometimes you need a specific later one: the second order a customer placed (to measure how quickly they came back), the third payment of a loan, the n-th time a machine stopped. You could sort and count by hand, but a formula with the position in a cell lets you change n and see the answer immediately. This recipe shows three ways, from the simplest Microsoft 365 formula to a helper-key method that works in any version.
The example data
Orders in A1:C7, sorted by date. The customer is typed in F1 and the position we want in F2. Alpha Ltd placed three orders, so its 2nd order is SO-503.
Microsoft 365 and Excel 2021: INDEX around FILTER
FILTER returns all the orders of the customer as a list, and INDEX picks the n-th item of that list:
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Date | Customer | Order | Customer | Alpha Ltd | |
| 2 | 2026-01-05 | Alpha Ltd | SO-501 | n | 2 | |
| 3 | 2026-01-09 | Brightway | SO-502 | Order | SO-503 | |
| 4 | 2026-01-14 | Alpha Ltd | SO-503 | |||
| 5 | 2026-01-20 | Cedar Co | SO-504 | |||
| 6 | 2026-01-26 | Alpha Ltd | SO-505 | |||
| 7 | 2026-02-02 | Brightway | SO-506 |
FILTER produces SO-501, SO-503, SO-505; INDEX takes item 2.
Result: SO-503
If you ask for a position that does not exist (n = 4 for Alpha Ltd), INDEX returns #REF!. Wrap the formula in IFERROR to show a message: =IFERROR(INDEX(FILTER(C2:C7,B2:B7=F1),F2),"No such order").
CHOOSEROWS does the same job and can return several columns: =CHOOSEROWS(FILTER(A2:C7,B2:B7=F1),F2) spills the date, customer and order of the 2nd match in one row. Use a negative number to count from the end: CHOOSEROWS(...,-1) is the last order.
The last match
For the last match specifically, XLOOKUP has a built-in option. Set search_mode to -1 to search from the bottom: =XLOOKUP(F1,B2:B7,C2:C7,,0,-1) returns SO-505.
Excel 2016 and 2019: INDEX + AGGREGATE
Without FILTER, find the row position of the n-th match with AGGREGATE, which can behave like SMALL while ignoring errors:
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Date | Customer | Order | Customer | Alpha Ltd | |
| 2 | 2026-01-05 | Alpha Ltd | SO-501 | n | 2 | |
| 3 | 2026-01-09 | Brightway | SO-502 | Order | SO-503 | |
| 4 | 2026-01-14 | Alpha Ltd | SO-503 | |||
| 5 | 2026-01-20 | Cedar Co | SO-504 | |||
| 6 | 2026-01-26 | Alpha Ltd | SO-505 | |||
| 7 | 2026-02-02 | Brightway | SO-506 |
Works without Ctrl+Shift+Enter in Excel 2010 and later.
Result: SO-503
ROW(B2:B7)-ROW(B2)+1creates the positions 1 to 6.- Dividing by
(B2:B7=F1)leaves the positions of Alpha Ltd rows (1, 3, 5) and turns the other rows into#DIV/0!errors. AGGREGATE(15,6,...,F2)means SMALL (function 15), ignoring errors (option 6), k = F2. With F2 = 2 the result is 3.INDEX(C2:C7,3)returns SO-503.
Swap 15 for 14 (LARGE) to count from the most recent order backwards.
Any version: a helper key with COUNTIF
If you prefer simple formulas, number each occurrence in a helper column. In A2, =C2&"-"&COUNTIF($C$2:C2,C2) creates keys such as Alpha Ltd-1, Alpha Ltd-2. Then a normal VLOOKUP finds any occurrence:
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Key | Date | Customer | Order | ||
| 2 | Alpha Ltd-1 | 2026-01-05 | Alpha Ltd | SO-501 | SO-503 | |
| 3 | Brightway-1 | 2026-01-09 | Brightway | SO-502 | ||
| 4 | Alpha Ltd-2 | 2026-01-14 | Alpha Ltd | SO-503 | ||
| 5 | Cedar Co-1 | 2026-01-20 | Cedar Co | SO-504 | ||
| 6 | Alpha Ltd-3 | 2026-01-26 | Alpha Ltd | SO-505 | ||
| 7 | Brightway-2 | 2026-02-02 | Brightway | SO-506 |
Column A holds =C2&"-"&COUNTIF($C$2:C2,C2) copied down. In a real sheet, build the lookup text from cells: F1&"-"&F2.
Result: SO-503
The growing range $C$2:C2 counts how many times the customer has appeared up to the current row, which is exactly its occurrence number. Helper keys are fast on large data and easy for colleagues to understand.
Pitfalls
- The n-th match depends on the order of the rows. If "2nd order" means the 2nd by date, sort by date first, or in Microsoft 365 sort inside the formula:
=INDEX(SORTBY(FILTER(C2:C7,B2:B7=F1),FILTER(A2:A7,B2:B7=F1)),F2). ROW(B2)in the AGGREGATE formula must refer to the first row of the data, otherwise the positions are shifted and INDEX returns the wrong row.- Text comparison ignores case and is broken by trailing spaces, as in every lookup.
- If n is typed as text (
"2"), INDEX and AGGREGATE may return#VALUE!. Make sure F2 holds a number.
Which method should you pick? In Microsoft 365, INDEX around FILTER is the clearest and needs no maintenance. In a workbook shared with colleagues on Excel 2016 or 2019, the AGGREGATE formula keeps everything in one cell, while the helper key is the easiest to audit and the fastest on very large lists.