Guides
/

Data analysis

UNIQUE / SORT

How to use UNIQUE and SORT in Excel: distinct values and sorting

UNIQUE returns the distinct values from a list, and SORT returns a sorted copy of your data. The source stays untouched and the result updates itself.

Syntax

=UNIQUE(array, [by_col], [exactly_once])
  • array — The range to take distinct values from: one column or several (then whole-row combinations are compared).
  • by_col — Optional. FALSE (default) compares rows, TRUE compares columns (for horizontal lists).
  • exactly_once — Optional. FALSE (default) returns every value once; TRUE returns only values that appear exactly once in the list.

Example

AB
1OrderRegion
2#101Tashkent
3#102Samarkand
4#103Tashkent
5#104Bukhara
6#105Samarkand

Formula:

=COUNTA(UNIQUE(B2:B6))

→ result: 3

How UNIQUE works

=UNIQUE(B2:B6) keeps each region from the list once: Tashkent, Samarkand, Bukhara — in order of first appearance. The result spills into the cells below the formula, and COUNTA counts them: 3 different regions. UNIQUE is not case-sensitive: “tashkent” and “Tashkent” are one value. But “Tashkent ” (with a trailing space) stays separate — clean the data with TRIM first.

Distinct vs “appears only once”

The third argument changes the result completely: • =UNIQUE(B2:B6) → Tashkent, Samarkand, Bukhara — every value once. • =UNIQUE(B2:B6,,TRUE) → Bukhara — only the value that occurs exactly once. The second form is handy for finding one-time customers, regions with a single order or unmatched records. Note the two commas when the second argument is skipped.

Sorting with SORT and SORTBY

=SORT(array, [sort_index], [sort_order], [by_col]) • =SORT(A2:C100) — by column 1, ascending. • =SORT(A2:C100,3,-1) — by column 3 (sales), descending: 1 is ascending, -1 is descending. SORTBY sorts by another range and accepts several keys: =SORTBY(A2:A100,B2:B100,1,C2:C100,-1) — names sorted by region A→Z, then by sales from largest to smallest. The key column doesn’t have to appear in the result.

The most useful combinations

• Alphabetical distinct list: =SORT(UNIQUE(B2:B100)) • Count of distinct values: =COUNTA(UNIQUE(B2:B100)) • Distinct values with a condition (e.g. customers in Tashkent): =UNIQUE(FILTER(A2:A100,B2:B100="Tashkent")) • Distinct combinations of several columns (region + product): =UNIQUE(B2:C100) • A report in two formulas: F2 =SORT(UNIQUE(B2:B100)), G2 =SUMIFS(C2:C100,B2:B100,F2#) — a total for every region.

A source for drop-down lists

UNIQUE makes a drop-down list that updates itself: 1. In a helper column, say F2, enter =SORT(UNIQUE(B2:B1000)). 2. Select the target cell → Data → Data Validation (Alt → A → V → V) → Allow: List. 3. In Source, type =$F$2#. The # sign is a spill reference — the whole result starting at F2. When a new region appears in the data, the list grows automatically.

Common errors

• A 0 appears in the result — the range contains blank cells (the tail of A2:A1000). Exclude them: =UNIQUE(FILTER(A2:A1000,A2:A1000<>"")). • #SPILL! — the cells where the result should spill already contain something, or the formula is inside an Excel table (Ctrl+T). • Identical-looking values appear twice — one has a hidden space, or one is a number and the other a number stored as text. Clean the data with TRIM and VALUE. • #NAME? — your Excel version doesn’t have these functions.

Versions and alternatives to UNIQUE

UNIQUE, SORT and SORTBY are available in Microsoft 365, Excel 2021, Excel 2024 and Excel for the web. In Excel 2016/2019: • To drop repeats — Data → Remove Duplicates (it changes the source, so work on a copy). • To sort — Data → Sort or the filter menu. • To count distinct values with a formula: =SUMPRODUCT(1/COUNTIF(B2:B6,B2:B6)) — the range must not contain blanks. The advantage of UNIQUE: the source table stays intact and the result recalculates with the data.

Has ExcelLab been useful?

Support the project and help us release new lessons and exercises.

Support us

Frequently asked questions

In Excel 365/2021: =COUNTA(UNIQUE(B2:B100)). If there are blanks: =COUNTA(UNIQUE(FILTER(B2:B100,B2:B100<>""))). In older versions: =SUMPRODUCT(1/COUNTIF(B2:B100,B2:B100)) (for a range without blank cells).
Wrap UNIQUE in SORT: =SORT(UNIQUE(B2:B100)). For reverse order: =SORT(UNIQUE(B2:B100),1,-1).
Remove Duplicates deletes repeated rows from the source table once and for good. UNIQUE leaves the source alone, returns the result elsewhere and recalculates whenever the data changes.
Yes, by passing arrays to the second and third arguments, but SORTBY is simpler: =SORTBY(A2:C100,B2:B100,1,C2:C100,-1) — first by B ascending, then by C descending.
UNIQUE is УНИК, SORT is СОРТ and SORTBY is СОРТПО. The arguments are the same; only the separator changes to a semicolon.