UNIQUE
Short answer
A list of accounts that updates itself
UNIQUE(array, [by_col], [exactly_once])
- Category
- Dynamic Arrays & Modern Excel
- Excel version
- 2021+
- Also searched as
- distinct values, remove duplicates formula, list of unique accounts, dimension list, new codes this month
What it returns
UNIQUE returns the distinct values from a range, in the order they first appear, as a spilled array. Point it at a ledger’s cost center column with 40,000 rows and you get the 85 cost centers it contains, one per row.
The result resizes itself. When next month’s extract brings a cost center nobody has seen, the list grows by one row on the next recalculation. When a code disappears, the list shrinks.
Feed it more than one column and it returns distinct combinations. UNIQUE(GL[[Entity]:[Dept]]) gives every entity and department pair that has activity, which is the row structure of a departmental P&L.
Arguments that matter
| Argument | Required | What it does |
|---|---|---|
array | Yes | The range to de-duplicate. GL[CostCenter]. One column, or several adjacent columns for distinct combinations. |
by_col | No | FALSE (default) compares rows. TRUE compares columns, for data laid out across. |
exactly_once | No | FALSE (default) returns every distinct value once. TRUE returns only the values that occur exactly once in the source. |
exactly_once is the one to learn. Set to TRUE, it drops anything that repeats and returns the singletons. Run it on a mapping table key and compare: if ROWS(UNIQUE(Map[Code], , TRUE)) is smaller than ROWS(Map[Code]), some key appears more than once. That one cell belongs in the check row.
by_col matters only when time runs across. A header row of month labels with repeats collapses to distinct months with TRUE. Most source data runs down, so leave it out. When the repeats sit in a grid, say cost centers typed across several columns, flatten it first with TOCOL: UNIQUE(TOCOL(range, 1)) skips the blanks and returns one distinct list.
UNIQUE is not case sensitive. "opex" and "OPEX" collapse to whichever appears first.
In a financial model
UNIQUE turns a dimension list from something you maintain into something the data maintains. The account list down column B of a summary tab, the cost center list feeding a dropdown, the vendor list on a spend analysis: each becomes one formula reading the source.
The pattern is UNIQUE for the rows and SUMIFS for the values. The row headers spill from one cell, and each value cell reads the spilled header by reference to the spill range, $B$12#. A new code adds a row and its SUMIFS fills with it, with no insert and no copy-down.
The second use is the close check. The question every month is “what appeared in the ledger that the mapping table has never seen?” UNIQUE answers it in one cell, and the play on building a check row and making it loud says where that answer belongs: at the top, counted, not buried on a working tab.
Where the list should drive a published statement, be careful. A statement has a fixed line order set by the mapping table in one mapping table per dimension. UNIQUE returns order of first appearance in the extract, which changes month to month. Use it for analysis tabs and checks, and let the mapping table own the statement layout.
Worked example
A general ledger extract on tab GL, formatted as a Table with columns Date, Code, CostCenter, Amount. A mapping table Map with columns Code and Line.
The list of codes with activity this period, sorted:
=SORT(UNIQUE(GL[Code]))
And the check: codes in the ledger that are missing from the mapping table.
=LET(
codes, UNIQUE(GL[Code]),
missing, FILTER(codes, ISNA(XMATCH(codes, Map[Code])), "None"),
missing)
With this extract and a mapping table that lacks 6450:
| GL[Code] (source) | Output |
|---|---|
| 4000 | 6450 |
| 6100 | |
| 4000 | |
| 6450 | |
| 6100 | |
| 6450 |
Six ledger rows, three distinct codes, one of them unmapped. The output is one row. On a month where everything maps, it reads None. Count it for the check row with =SUM(--ISNA(XMATCH(UNIQUE(GL[Code]), Map[Code]))), expected zero.
A spend summary uses the same idea with values attached. In B12, =SORT(UNIQUE(AP[Vendor])). In C12:
=SUMIFS(AP[Amount], AP[Vendor], B12#)
One formula in each column, and both spill to the same height.
Old way vs new way
The legacy route is Data, Remove Duplicates on a pasted copy of the column, or a PivotTable with the code in Rows. As a formula, the pre-2021 version was an array formula entered with Ctrl+Shift+Enter:
=IFERROR(INDEX(GL[Code], MATCH(0, COUNTIF($B$11:B11, GL[Code]), 0)), "")
The new version:
=UNIQUE(GL[Code])
The old one had to be filled down far enough to catch every code, recalculated slowly over a large ledger, and failed silently if it was not filled far enough. UNIQUE sizes itself and reads in one glance.
Where it breaks
A 0 appears at the bottom of the list. The source range includes blank cells, usually a whole-column reference like GL!B:B, and UNIQUE returns a blank as 0. Fix: point it at a Table column, or wrap it: UNIQUE(FILTER(GL[Code], GL[Code]<>"")).
The same code appears twice. One copy is stored as text and the other as a number, or one carries a trailing space. UNIQUE treats "4000" and 4000 as different values. Fix: correct the type where the data lands, in Power Query, rather than wrapping every formula in VALUE or TRIM.
UNIQUE returns #SPILL!. Something sits in the cells the result needs, often a hard-coded total typed under last month’s list. Fix: clear the range below and to the right, and put totals above the spill or reference it with SUM(B12#) elsewhere.
SUMIFS values no longer line up with their labels. The value column was typed as cell-by-cell formulas pointing at B12, B13, and so on, and the list grew or reordered. Fix: reference the spill range, B12#, so the values spill with the labels.
UNIQUE returns #NAME?. The file opened in Excel 2019 or earlier, which has no dynamic arrays. Fix: if the model must travel to legacy Excel, keep the static list and the array formula above.
The statement lines reorder month to month. UNIQUE returns order of first appearance, and the extract order changed. Fix: wrap it in SORT for analysis tabs, and drive published statements from the mapping table’s line order instead.
Related functions
- FILTER. When you want rows that meet a condition rather than distinct values. The two combine for “distinct codes in entity 200”.
- SORTBY. Order the distinct list by something other than its own value, such as spend, descending.
- GROUPBY. UNIQUE plus SUMIFS in one formula, with totals. Reach for it when the summary is the output and you do not need the labels for anything else.
- COUNTIFS. Count how often each distinct value occurs, to find which key is the duplicate once UNIQUE has told you one exists.
- XLOOKUP. Attach the statement line to each distinct code. The pattern behind SUMIFS over lookups for period math.
- SORT. Puts the distinct list in alphabetical or numeric order. Almost always the wrapper on a UNIQUE that feeds a dropdown.
