Sub topic 13
Get the top N rows and unique values
This chapter covers two functions that shrink a result to the rows you care about: SORTN keeps the first N rows of a sorted result, and UNIQUE removes repeated values. It assumes you can write SORT and FILTER formulas, know that array formulas spill into neighboring cells, and can read ranges such as A2:D. Both functions produce live results that update when the source data changes.
The sample data
The examples use this table in A1:D9. Row 1 is the header and the data is in A2:D9.
| Region | Rep | Amount | Date |
|---|---|---|---|
| West | Ana | 1200 | 2026-01-05 |
| East | Ben | 800 | 2026-01-07 |
| West | Cho | 1500 | 2026-01-09 |
| North | Dev | 1500 | 2026-01-12 |
| East | Eli | 2000 | 2026-01-14 |
| West | Fay | 900 | 2026-01-15 |
| North | Gus | 1500 | 2026-01-19 |
| East | Hal | 700 | 2026-01-21 |
In the formulas below, A2:D means "everything from row 2 down", so new rows are picked up automatically.
Top N rows with SORTN
SORTN sorts a range like SORT does, then returns only the first N rows. Its syntax is:
=SORTN(range, [n], [display_ties_mode], [sort_column, is_ascending], ...)rangeis the data to sort. Do not include the header row.nis how many rows to return. The default is 1.display_ties_modecontrols how rows that tie at the cutoff are handled.sort_columnandis_ascendingwork exactly as inSORT. You can repeat the pair for a multi-level sort.
To get the three largest sales:
=SORTN(A2:D, 3, 0, 3, FALSE)Column 3 is Amount, and FALSE sorts it from largest to smallest. The formula returns these rows:
| Region | Rep | Amount | Date |
|---|---|---|---|
| East | Eli | 2000 | 2026-01-14 |
| West | Cho | 1500 | 2026-01-09 |
| North | Dev | 1500 | 2026-01-12 |
For the bottom three, change the sort direction to TRUE:
=SORTN(A2:D, 3, 0, 3, TRUE)Ties and display_ties_mode
Three rows have an Amount of 1500: Cho, Dev and Gus. Only two of them fit in the top three. With display_ties_mode set to 0, the formula cuts the list at exactly N rows. Gus is dropped, and the rows that appear earlier in the source are kept first.
The other modes change two things: whether rows tied with the Nth row are kept, and whether fully identical rows are removed. Check the built-in help for SORTN (type =SORTN( and open the hint) for the exact meaning of each number in your version. Choose a mode on purpose when ties matter. A "top 3 salespeople" report that silently drops a tied person is a common source of disputes.
Making N a cell value
Hard-coding 3 works, but a cell makes the report adjustable. If F1 holds the number of rows you want:
=SORTN(A2:D, F1, 0, 3, FALSE)If F1 is larger than the number of data rows, SORTN returns every row. It does not raise an error. If F1 is blank, Sheets treats n as the default of 1, which can look like a bug.
Top N after filtering
SORTN accepts any array as its first argument, so you can filter first. This returns the top two West sales:
=SORTN(FILTER(A2:D, A2:A="West"), 2, 0, 3, FALSE)The result is Cho (1500) and Ana (1200). The sort_column number refers to the column's position within the filtered array, not its letter on the sheet. Because FILTER here keeps all four columns, the Amount column is still column 3.
If the region has no rows, FILTER returns #N/A and SORTN passes it along. Wrap the formula as you did in the previous chapter:
=IFERROR(SORTN(FILTER(A2:D, A2:A="West"), 2, 0, 3, FALSE), "No matches")Keeping a header row
SORTN does not know about headers. If you include row 1 in the range, the header text is sorted with the data. To show a header above the result, stack it with an array literal. A semicolon places the second block below the first, and both blocks need the same number of columns:
={A1:D1; SORTN(A2:D, 3, 0, 3, FALSE)}Unique values with UNIQUE
UNIQUE returns each distinct row once. The syntax is:
=UNIQUE(range, [by_col], [exactly_once])by_colisFALSEby default, so rows are compared. Set it toTRUEto compare columns instead.exactly_onceisFALSEby default. Set it toTRUEto return only values that appear a single time.
To list each region once, in the order it first appears:
=UNIQUE(A2:A)For the sample data this returns West, East and North. To sort the list alphabetically, wrap it in SORT:
=SORT(UNIQUE(A2:A))The result is East, North, West.
Unique rows versus unique values
If the range has more than one column, UNIQUE compares whole rows. A row counts as a duplicate only if every column matches. =UNIQUE(A2:B) returns each Region and Rep pair once, so a rep who appears under two regions is listed twice. To get distinct values of one column, pass only that column.
Values that appear exactly once
Set the third argument to TRUE to drop anything that repeats:
=UNIQUE(C2:C, FALSE, TRUE)The Amount 1500 appears three times, so it is excluded. The result is 1200, 800, 2000, 900 and 700. This is a quick way to find non-repeating entries, such as an invoice number that was entered only once. Note that this is different from a normal UNIQUE, which would keep one copy of 1500.
Gotchas with UNIQUE
- Case is ignored. "west" and "West" count as the same value. The first one encountered is the one displayed.
- Spaces count. "West" and "West " (with a trailing space) are different values. If your list shows apparent duplicates, check for stray spaces with
=LEN(A2)or clean the column withTRIM. - Blanks appear in open-ended ranges. A range like
A2:Aincludes empty cells below your data, which can show up as a blank entry. Filter them out first:
=SORT(UNIQUE(FILTER(A2:A, A2:A<>"")))- Text and numbers differ. The number 1500 and the text "1500" are not the same value. This is the same data-type problem that disrupts sorting.
Counting distinct values
UNIQUE combines well with counting functions. This counts the distinct regions:
=COUNTA(UNIQUE(FILTER(A2:A, A2:A<>"")))For the sample data the answer is 3. The FILTER step matters here: without it, COUNTA could count a blank entry.
Choosing the right tool
The diagram shows how the functions chain together. Each step takes an array and returns an array, so you can add or remove steps as needed.
| Goal | Formula pattern |
|---|---|
| Top N rows overall | `=SORTN(range, N, 0, col, FALSE)` |
| Bottom N rows | `=SORTN(range, N, 0, col, TRUE)` |
| Top N within a group | `=SORTN(FILTER(range, condition), N, 0, col, FALSE)` |
| Distinct values, alphabetical | `=SORT(UNIQUE(FILTER(col, col<>"")))` |
| Values that occur once | `=UNIQUE(col, FALSE, TRUE)` |
| Number of distinct values | `=COUNTA(UNIQUE(FILTER(col, col<>"")))` |
Common errors
#REF!with "Array result was not expanded". Something sits in the cells where the result needs to spill. Clear those cells, or move the formula to an empty area. This applies toSORTNandUNIQUEjust as it does toSORTandFILTER.- Header in the middle of the results. The header row was included in the range. Start the range at row 2, or stack the header separately as shown above.
- Wrong top rows. If amounts were pasted as text, they sort as text, so "900" ranks above "2000". Convert them to numbers first.
- Too few rows returned. With mode 0, ties beyond N are cut. Raise N, choose a different tie mode, or add a tie-breaker column.
- Only one row returned.
nwas left blank or omitted, so the default of 1 applied.
Exercises
Exercise
Using the sample data in A2:D9, write one formula that returns the two largest sales made in the East region, showing the whole row. Then say which rows it returns.
Show answer
=SORTN(FILTER(A2:D9, A2:A9="East"), 2, 0, 3, FALSE). FILTER keeps the three East rows (Ben 800, Eli 2000, Hal 700), SORTN sorts them by column 3 in descending order and keeps two, so the result is Eli (2000) and Ben (800).
Exercise
A colleague's formula =UNIQUE(A2:A) shows a blank entry at the bottom of the region list, and =COUNTA(UNIQUE(A2:A)) gives 4 instead of 3. Explain the cause and rewrite the count so it returns 3.
Show answer
The open-ended range A2:A includes empty cells below the data, and UNIQUE returns one blank for them, which COUNTA then counts. Remove blanks first: =COUNTA(UNIQUE(FILTER(A2:A, A2:A<>""))).