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.

Intermediate INDEX FILTER CHOOSEROWS AGGREGATE ROW COUNTIF XLOOKUP VLOOKUP

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:

F3fx=INDEX(FILTER(C2:C7,B2:B7=F1),F2)
ABCDEF
1DateCustomerOrderCustomerAlpha Ltd
22026-01-05Alpha LtdSO-501n2
32026-01-09BrightwaySO-502OrderSO-503
42026-01-14Alpha LtdSO-503
52026-01-20Cedar CoSO-504
62026-01-26Alpha LtdSO-505
72026-02-02BrightwaySO-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:

F3fx=INDEX(C2:C7,AGGREGATE(15,6,(ROW(B2:B7)-ROW(B2)+1)/(B2:B7=F1),F2))
ABCDEF
1DateCustomerOrderCustomerAlpha Ltd
22026-01-05Alpha LtdSO-501n2
32026-01-09BrightwaySO-502OrderSO-503
42026-01-14Alpha LtdSO-503
52026-01-20Cedar CoSO-504
62026-01-26Alpha LtdSO-505
72026-02-02BrightwaySO-506

Works without Ctrl+Shift+Enter in Excel 2010 and later.

Result: SO-503

  1. ROW(B2:B7)-ROW(B2)+1 creates the positions 1 to 6.
  2. Dividing by (B2:B7=F1) leaves the positions of Alpha Ltd rows (1, 3, 5) and turns the other rows into #DIV/0! errors.
  3. AGGREGATE(15,6,...,F2) means SMALL (function 15), ignoring errors (option 6), k = F2. With F2 = 2 the result is 3.
  4. 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:

F2fx=VLOOKUP("Alpha Ltd-2",A2:D7,4,FALSE)
ABCDEF
1KeyDateCustomerOrder
2Alpha Ltd-12026-01-05Alpha LtdSO-501SO-503
3Brightway-12026-01-09BrightwaySO-502
4Alpha Ltd-22026-01-14Alpha LtdSO-503
5Cedar Co-12026-01-20Cedar CoSO-504
6Alpha Ltd-32026-01-26Alpha LtdSO-505
7Brightway-22026-02-02BrightwaySO-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.

Tip. To list all matches at once instead of one at a time, see How to return multiple matches in Excel. For the last entry of a column regardless of criteria, see How to find the last value in a column in Excel. The lookup functions themselves are explained in Lookup guide: VLOOKUP vs INDEX/MATCH vs XLOOKUP.
⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 17 KB

More how-to recipes