Sub topic 8
UNIQUE options: by_column and exactly_once
This chapter covers the second and third arguments of UNIQUE. It assumes you can enter formulas, use open-ended ranges such as B2:B, and read comparison operators. It also assumes you know how a formula result spills into neighboring cells and how UNIQUE removes duplicates in its default form.
The full signature
UNIQUE takes one required argument and two optional ones:
UNIQUE(range, [by_column], [exactly_once])| Argument | Default | Meaning |
|---|---|---|
| `range` | none | The cells to examine. |
| `by_column` | `FALSE` | `FALSE` compares rows with each other. `TRUE` compares columns with each other. |
| `exactly_once` | `FALSE` | `FALSE` keeps one copy of each distinct item. `TRUE` keeps only items that occur a single time. |
The two options are independent. You can set either one, or both. To set only exactly_once, you still have to supply by_column, because arguments are positional. Pass FALSE as a placeholder.
What "distinct" means for a multi-column range
UNIQUE compares whole rows, not individual cells. Take this range in A2:C:
| A (Name) | B (Region) | C (Sales) |
|---|---|---|
| Ana | West | 1200 |
| Ben | East | 800 |
| Ana | West | 1200 |
| Ana | West | 900 |
=UNIQUE(A2:C) returns three rows. The first and third rows match in every column, so one of them is dropped. The fourth row has the same Name and Region but a different Sales value, so it counts as a different row and stays.
To list distinct values of a single field, pass a single-column range such as B2:B.
The comparison is case-insensitive. West and west are treated as the same value, and the first one encountered is the one returned. The comparison also ignores formatting. A cell that shows 1,200 and a cell that holds the number 1200 are equal, but the text "1200" and the number 1200 are different values.
The by_column option
By default, UNIQUE looks down the rows. Each row is one item, and rows are compared with each other. Setting by_column to TRUE rotates this. Each column is one item, and columns are compared with each other.
Suppose you have a horizontal layout where each column is an observation, and you want to drop repeated observations:
| Row | D | E | F | G |
|---|---|---|---|---|
| 1 | Mon | Tue | Mon | Wed |
| 2 | 10 | 20 | 10 | 30 |
=UNIQUE(D1:G2, TRUE)Columns D and F are identical (Mon, 10), so the formula returns three columns: Mon/10, Tue/20, and Wed/30. The result spills to the right, not downward.
The default behavior on the same range is different:
=UNIQUE(D1:G2)This compares row 1 with row 2. They differ, so both rows come back and nothing is removed. This is the most common surprise with by_column. If you have wide data and UNIQUE appears to do nothing, check whether you need TRUE.
A single row of values is a special case. For D1:G1 with by_column left at FALSE, there is only one row, so there is nothing to deduplicate. Use =UNIQUE(D1:G1, TRUE) to get distinct values across the row.
The exactly_once option
The default UNIQUE is a "deduplicate" operation. It keeps one copy of everything. Setting exactly_once to TRUE changes the question. It keeps only the items that occur a single time in the range. Any item that appears twice or more is removed entirely, with no copy kept.
Consider a column of order IDs in A2:A:
| A |
|---|
| 101 |
| 102 |
| 101 |
| 103 |
| 102 |
| 104 |
=UNIQUE(A2:A)returns 101, 102, 103, 104.=UNIQUE(A2:A, FALSE, TRUE)returns 103, 104.
The values 101 and 102 each appear twice, so they disappear from the second result. This makes exactly_once a quick way to find items that have no repeat. Typical uses are customers who ordered only once, IDs that appear in a single system, and typos that occur a single time in a free-text column.
The result keeps the order of first appearance, as it does with the default setting.
If every item in the range repeats, the result is empty, and UNIQUE returns #N/A. This follows the same pattern as FILTER. Wrap the formula when that outcome is possible:
=IFERROR(UNIQUE(A2:A, FALSE, TRUE), "Every value repeats")Finding the repeated values instead
UNIQUE has no option for "values that appear more than once". You can build this from UNIQUE and COUNTIF with FILTER:
=UNIQUE(FILTER(A2:A, COUNTIF(A2:A, A2:A) > 1))Reading from the inside out:
COUNTIF(A2:A, A2:A)counts, for each cell, how often its value occurs in the range.FILTERkeeps the cells whose count is above 1.UNIQUEcollapses the remaining copies to one each.
For the order IDs above this returns 101 and 102.
If A2:A contains blank cells, COUNTIF treats each blank as a match for other blanks and the criterion behaves oddly. Filter blanks out first, as described in the common mistakes below.
Using both options together
When by_column is TRUE and exactly_once is TRUE, UNIQUE looks for columns that occur only once. For the earlier horizontal data:
=UNIQUE(D1:G2, TRUE, TRUE)Columns D and F match, so both are removed. The result is two columns: Tue/20 and Wed/30.
exactly_once on multi-column ranges
The same row-level logic applies. With exactly_once set, a row is kept only if no other row matches it in every column. In the Ana/Ben table above, =UNIQUE(A2:C, FALSE, TRUE) returns Ben/East/800 and Ana/West/900. The two identical Ana/West/1200 rows cancel each other out.
Use this to reveal unmatched records. For example, to compare two lists, stack them and ask for rows that appear only once:
=UNIQUE({A2:A; D2:D}, FALSE, TRUE)Names that exist in both lists occur twice in the stacked array and are removed. The names that remain exist in only one list. Be aware that a name repeated within a single list is also removed, so this works correctly only when each list is already free of duplicates. If it might not be, wrap each list in its own UNIQUE first:
=UNIQUE({UNIQUE(A2:A); UNIQUE(D2:D)}, FALSE, TRUE)Combining the options with other functions
Because UNIQUE returns an array, it nests like the other functions in this tutorial. Two patterns come up often.
Sort the distinct values for a dropdown source:
=SORT(UNIQUE(FILTER(B2:B, B2:B<>"")))Count how many values are distinct:
=COUNTA(UNIQUE(FILTER(B2:B, B2:B<>"")))Count how many values occur only once:
=COUNTA(UNIQUE(FILTER(B2:B, B2:B<>""), FALSE, TRUE))The last formula will show #N/A if nothing occurs exactly once. Wrap it in IFERROR(..., 0) to get a count of zero.
Common mistakes
- Leaving blanks in an open-ended range.
UNIQUE(B2:B)returns one empty-looking row for the blank cells below your data. Filter them out first withFILTER(B2:B, B2:B<>""). Withexactly_onceset toTRUE, the blank row is removed only if more than one blank cell exists, so a range with exactly one blank cell would return it. - Forgetting the placeholder.
UNIQUE(A2:A, TRUE)setsby_column, notexactly_once. To set only the third argument, writeUNIQUE(A2:A, FALSE, TRUE). - Using the default on wide data. If your items are laid out across columns, use
by_columnset toTRUE. - Treating
exactly_onceas "remove duplicates". It removes every copy of a repeated item. The result can be much shorter than the default result. - Hidden differences. Trailing spaces, such as
"West "against"West", make values distinct. Clean the data withTRIMinside the formula:=UNIQUE(ARRAYFORMULA(TRIM(B2:B))). - Blocked spill. The result still needs empty cells to spill into. Any value in the way produces a
#REF!error until the obstruction is cleared.
Summary of behavior
| Formula | Compares | Keeps |
|---|---|---|
| `UNIQUE(range)` | Rows | One copy of each distinct row |
| `UNIQUE(range, TRUE)` | Columns | One copy of each distinct column |
| `UNIQUE(range, FALSE, TRUE)` | Rows | Rows that occur once |
| `UNIQUE(range, TRUE, TRUE)` | Columns | Columns that occur once |
Exercise
Column A2:A holds these values: red, blue, red, green, blue, yellow. (1) What does =UNIQUE(A2:A, FALSE, TRUE) return? (2) Write a formula that returns the values that appear more than once, each listed a single time.
Show answer
(1) It returns green and yellow, because red and blue each appear twice and are removed entirely. (2) =UNIQUE(FILTER(A2:A, COUNTIF(A2:A, A2:A) > 1)) returns red and blue. To also ignore blank cells below the data, use =UNIQUE(FILTER(A2:A, A2:A<>"", COUNTIF(A2:A, A2:A) > 1)).
Exercise
Row 1 contains the values D1:H1 = north, south, north, east, south. You write =UNIQUE(D1:H1) and get the same five values back. Why, and what formula gives the distinct values across the row?
Show answer
With by_column at its default of FALSE, UNIQUE compares rows. The range has only one row, so nothing is a duplicate. Use =UNIQUE(D1:H1, TRUE), which compares columns and returns north, south, east across the row.