Locating the first match in Excel is straightforward, but retrieving the second, third, or Nth occurrence of a data point often leads to frustrating manual workarounds. While standard VLOOKUP or basic INDEX MATCH functions are the industry-standard tools for single lookups, they fall short when handling duplicate records.
Mastering the Nth occurrence lookup grants you absolute control over complex datasets without requiring you to restructure your source tables. As a stipulation, keep in mind that these advanced formulas leverage array processing (utilizing SMALL and ROW), which can slow down performance in massive workbooks.
For example, isolating the second transaction for "Client A" in a ledger requires this exact multi-layered approach. Below, we outline the precise formula syntax to deploy this solution.
In the world of data analysis, Microsoft Excel is an indispensable tool. Among its most powerful lookup features are the INDEX and MATCH functions. When combined, they offer a highly flexible alternative to the traditional VLOOKUP. However, standard lookup functions share a common limitation: they are designed to return only the first occurrence of a matching value.
But what happens when your dataset contains duplicate entries, and you need to retrieve the second, third, or Nth occurrence? For instance, if you have a sales log and want to find the amount of John's 3rd transaction, a standard INDEX MATCH will fail, consistently returning his 1st transaction instead.
To overcome this limitation, you need to construct a formula that can identify and isolate multiple matching entries. In this comprehensive guide, we will explore three highly effective methods to reference the Nth occurrence using Excel: the classic INDEX-SMALL-IF Array Formula, the non-array AGGREGATE alternative, and the modern Excel 365 FILTER function.
To illustrate these techniques, let us assume we have a sales table in the range A2:C7, representing transactional data. Our goal is to look up the 2nd transaction amount for the employee named "John".
| Row # | A (Employee) | B (Date) | C (Sales Amount) |
|---|---|---|---|
| 2 | Sarah | Jan 1 | $150 |
| 3 | John (1st) | Jan 2 | $200 |
| 4 | Sarah | Jan 3 | $350 |
| 5 | Bob | Jan 4 | $100 |
| 6 | John (2nd) | Jan 5 | $450 |
| 7 | John (3rd) | Jan 6 | $300 |
If we search for "John", a standard =INDEX(C2:C7, MATCH("John", A2:A7, 0)) will return $200 (the 1st occurrence). To extract the 2nd occurrence ($450), we need a more advanced approach.
For Excel versions older than Office 365 (such as Excel 2013, 2016, or 2019), the standard solution is an array formula that combines INDEX, SMALL, IF, and ROW.
{=INDEX(Return_Range, SMALL(IF(Lookup_Range = Lookup_Value, ROW(Lookup_Range) - ROW(First_Cell_in_Lookup_Range) + 1), N))}
Note: This is an array formula. You must press Ctrl + Shift + Enter (instead of just Enter) to execute it in older Excel versions. This wraps the formula in curly brackets {} automatically.
To find the 2nd sale amount for "John" (where N = 2), enter the following formula in your cell:
=INDEX(C2:C7, SMALL(IF(A2:A7="John", ROW(A2:A7) - ROW(A2) + 1), 2))
IF: The statement A2:A7="John" checks each cell in the lookup range. It generates an array of Boolean values: {FALSE; TRUE; FALSE; FALSE; TRUE; TRUE}.ROW(A2:A7) - ROW(A2) + 1 creates a relative sequence of row numbers starting from 1. It evaluates to: {1; 2; 3; 4; 5; 6}. This ensures the formula works perfectly regardless of where your table resides on the worksheet.IF function combines the Boolean array and the relative row numbers. If a value is TRUE, it retains the relative row number; if FALSE, it returns FALSE. The output array is: {FALSE; 2; FALSE; FALSE; 5; 6}. Note that "John" matches rows 2, 5, and 6.SMALL: The SMALL(..., N) function extracts the Nth smallest number from our filtered array, completely ignoring logical FALSE values. Since we requested the 2nd occurrence (N = 2), SMALL({FALSE; 2; FALSE; FALSE; 5; 6}, 2) returns 5.INDEX: Finally, INDEX(C2:C7, 5) references the 5th element in the sales column, which returns $450.If you are using Excel 2010 or newer but want to avoid the complexity of pressing Ctrl + Shift + Enter, you can use the incredibly versatile AGGREGATE function. This function can perform array-like operations natively without requiring array entry.
=INDEX(Return_Range, AGGREGATE(15, 6, (ROW(Lookup_Range) - ROW(First_Cell) + 1) / (Lookup_Range = Lookup_Value), N))
Using our sample dataset to find the 2nd sale for "John", the formula is:
=INDEX(C2:C7, AGGREGATE(15, 6, (ROW(A2:A7) - ROW(A2) + 1) / (A2:A7="John"), 2))
SMALL function.(ROW(A2:A7) - ROW(A2) + 1) / (A2:A7="John").TRUE (which equals 1) returns the row number. Dividing by FALSE (which equals 0) yields a #DIV/0! error.{1/FALSE; 2/TRUE; 3/FALSE; 4/FALSE; 5/TRUE; 6/TRUE}, which simplifies to {#DIV/0!; 2; #DIV/0!; #DIV/0!; 5; 6}.#DIV/0! errors, leaving only {2; 5; 6}.INDEX(C2:C7, 5) retrieves $450.If you are using Microsoft 365 or Excel 2021/Web, you have access to Dynamic Arrays. This completely eliminates the need for complex mathematical workarounds. You can simply combine INDEX with the new FILTER function.
=INDEX(FILTER(Return_Range, Lookup_Range = Lookup_Value), N)
To find the 2nd sale for "John" using Excel 365:
=INDEX(FILTER(C2:C7, A2:A7="John"), 2)
The FILTER(C2:C7, A2:A7="John") function filters the sales column based on our criteria. It returns a dynamic array of matching entries: {$200; $450; $300}. From there, the INDEX function simply selects the 2nd element in this new, clean list, returning $450.
This is by far the cleanest, most readable, and fastest-calculating method available.
If you request an Nth occurrence that does not exist (for example, asking for the 4th occurrence of "John" when there are only 3), your formulas will return a #NUM! or #REF! error. To keep your dashboards clean, always wrap your lookup formulas in an IFERROR function.
Here is how to apply it to the Excel 365 formula:
=IFERROR(INDEX(FILTER(C2:C7, A2:A7="John"), 4), "No record found")
Similarly, for the AGGREGATE method:
=IFERROR(INDEX(C2:C7, AGGREGATE(15, 6, (ROW(A2:A7) - ROW(A2) + 1) / (A2:A7="John"), 4)), "No record found")
Choosing the right formula depends heavily on your Excel environment and structural requirements:
By mastering these dynamic methods, you can construct robust dashboards and advanced data models capable of handling duplicate keys and multi-tiered lookups with ease.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.