UNIQUE, SORT and SORTBY: Lists That Update Themselves
Shahrukh needs a list of cities from a 20-row sales table, sorted A to Z, for a drop-down. Last month he copied the column, removed duplicates and sorted it by hand. Then two new rows came in and the list was out of date. One formula, =SORT(UNIQUE(Sales[City])), now keeps it current.
UNIQUE returns each value once. SORT returns a range sorted by one of its own columns. SORTBY sorts a range by another range, such as reps by amount. All three are dynamic array functions: you type the formula in one cell and the answer spills into the cells below.
Download the practice file (unique-sort-sortby.xlsx)
Excel file, 7 KB. It has the sample data used below. All numbers in this workbook are fictional teaching data, not real company figures.
Quick answer
- Click an empty cell with free space below it.
- Type
=UNIQUE(Sales[City])and press Enter. Six cities spill down. - Wrap it:
=SORT(UNIQUE(Sales[City]))for A to Z. - For reps from highest to lowest amount:
=SORTBY(Sales[Rep],Sales[Amount],-1). - If you see #SPILL!, clear the cells below the formula.
=SORT(UNIQUE(Sales[City]))
=SORTBY(Sales[Rep],Sales[Amount],-1)
UNIQUE
=UNIQUE(array, [by_col], [exactly_once])
=UNIQUE(Sales[City])gives the 6 cities, each once.=UNIQUE(Sales[[Rep]:[City]])gives each Rep + City pair once.=UNIQUE(Sales[City],,TRUE)gives only cities that appear exactly once.
Count distinct values with =ROWS(UNIQUE(Sales[City])). In the practice file it returns 6.
SORT
=SORT(array, [sort_index], [sort_order], [by_col])
=SORT(UNIQUE(Sales[City]))sorts the city list A to Z. The first one is Kolhapur.=SORT(Sales,3,-1)sorts the whole table by its 3rd column (Amount), largest first.=INDEX(SORT(Sales[Amount],1,-1),1)returns the biggest amount: 9,100.
SORTBY
=SORTBY(array, by_array1, [sort_order1], ...)
=SORTBY(Sales[Rep],Sales[Amount],-1) lists reps in order of their amounts, highest first. The first name is Rani, who has the biggest single sale. SORTBY can sort by a column that you do not show, and by several columns: =SORTBY(Sales,Sales[City],1,Sales[Amount],-1).
Fixing #SPILL! and other surprises
- #SPILL!: something is in the way. Select the formula cell; Excel draws a dashed border around the spill area. Clear those cells.
- Spilled formulas do not work inside an Excel Table. Put them on a normal range next to the table.
- Refer to the whole spilled list with the
#sign: if the list starts in H2, use=H2#in a drop-down source or in another formula. - Excel 2019 and older do not have these functions. Use Remove Duplicates and Data > Sort instead.
Ravindra Bagale's Tip
Use =SORT(UNIQUE(...)) as the source for drop-down lists: in Data Validation type =$H$2#. New cities in the data appear in the drop-down without anyone editing it.
Ravindra Bagale's Tip – मराठी
Drop-down list चा source म्हणून =SORT(UNIQUE(...)) वापरा: Data Validation मध्ये =$H$2# लिहा. Data मध्ये नवीन city आली की कोणी edit न करता drop-down मध्ये दिसते.
Ravindra Bagale's Tip – हिंदी
Drop-down list के source के लिए =SORT(UNIQUE(...)) इस्तेमाल कीजिए: Data Validation में =$H$2# लिखिए। Data में नई city आते ही बिना edit किए drop-down में दिख जाती है।
Practice task
- Open the Sales sheet (table name Sales).
- In H2 type
=SORT(UNIQUE(Sales[City]))and check six cities spill, starting with Kolhapur. - In J2 type
=SORTBY(Sales[Rep],Sales[Amount],-1). - Type something in H5 and look at the #SPILL! error, then delete it.
- Compare your answers with the check cells in E2:F5.
Learn it properly
Course lessons:
Related guides: FILTER Function · Remove Duplicates · Sort by Several Columns, Custom Sort Order and Advanced Filter · Dependent Drop-Down List in Excel (City, then Area)
Frequently asked questions
Which Excel versions have UNIQUE and SORT?
Microsoft 365, Excel 2021, Excel 2024 and Excel for the web. Excel 2019 and older do not.
How do I count unique values?
=ROWS(UNIQUE(range)), or =COUNTA(UNIQUE(range)).
Can UNIQUE ignore blanks?
Combine it with FILTER: =UNIQUE(FILTER(Sales[City],Sales[City]<>"")).
Why does my formula show only one value in old Excel?
Older versions do not spill. The file shows the first value only, or an error.