Sub topic 9
Deduplicate with a formula: UNIQUE
This chapter covers the UNIQUE function as a formula-based alternative to Data cleanup > Remove duplicates. It assumes you can enter formulas, select ranges, and use the menu bar. It also assumes you have already met TRIM, CLEAN, and the case functions (LOWER, UPPER, PROPER) in earlier chapters. Those steps matter here because UNIQUE compares cells exactly as they are stored.
Why a formula instead of the menu tool
The Remove duplicates tool changes your data in place. It deletes rows, and you can only undo it right away. UNIQUE works differently. It reads a range and writes the distinct rows into a new location. Your source data stays as it was.
That gives you three advantages:
- The original data is never modified, so it is a safe companion to the "work on a copy" habit.
- The result updates automatically when the source changes.
- You can feed the result into other formulas, such as
COUNTA,SORT, or a data validation list.
The cost is that the output is a live formula, not static values. If you later delete the source, the output disappears with it. Copy the result and use Paste special > Values only when you need a fixed snapshot.
Syntax
=UNIQUE(range, [by_column], [exactly_once])| Argument | Default | Meaning |
|---|---|---|
| `range` | required | The cells to deduplicate. Rows are compared as a whole. |
| `by_column` | `FALSE` | If `TRUE`, compares columns instead of rows. Rarely needed for cleanup. |
| `exactly_once` | `FALSE` | If `TRUE`, returns only rows that appear exactly one time in the source. |
The most common form takes only the range:
=UNIQUE(A2:C)This returns the distinct rows of A2:C in a new location and leaves the original data unchanged. UNIQUE keeps the first occurrence of each row and drops later repeats. The output keeps the original order of those first occurrences.
Where to put the formula
UNIQUE returns an array, which means it fills several cells from one formula. Google Sheets calls this a spill. Place the formula in a single cell, and the results fill down and to the right of it.
Two rules follow:
- The cells the result needs must be empty. If any cell in the spill area contains data, the formula shows
#REF!and the message "Array result was not expanded because it would overwrite data." - Put the formula on a separate sheet or in columns beside the data, never inside the range it reads.
A tidy layout is to keep the raw data on a sheet named Raw and the formula on a sheet named Clean:
=UNIQUE(Raw!A2:C)How rows are compared
UNIQUE treats two rows as duplicates only when every cell in the row matches. With a three-column range, the rows Jane, Doe, NY and Jane, Doe, CA are different, so both are kept. If you want to deduplicate on one column only, select only that column in the range.
The comparison has two properties that cause most of the surprises:
- It ignores case.
[email protected]and[email protected]are treated as the same value, and the first one seen is kept. - It is sensitive to spaces and invisible characters.
"Jane"and"Jane "(with a trailing space) are different values. So are a normal space and a non-breaking space.
The second point is why cleaning order matters. If you run UNIQUE on untrimmed data, the stray spaces keep apparent duplicates apart, and you get a result that looks deduplicated but is not.
Worked example
Suppose Raw!A1:C holds this data, with a header in row 1:
| Name | City | |
|---|---|---|
| Jane Doe | [email protected] | Austin |
| Jane Doe | [email protected] | Austin |
| JANE DOE | [email protected] | Austin |
| Sam Lee | [email protected] | Denver |
| Jane Doe | [email protected] | Austin |
The second row has a trailing space after the name. The third row differs only in case. The fifth row is an exact repeat of the first.
Running UNIQUE directly on the raw range gives:
=UNIQUE(Raw!A2:C)The result has three rows: the first Jane Doe row, the trailing-space row, and Sam Lee. The all-caps row is dropped because the comparison ignores case. The exact repeat is dropped. The trailing-space row survives because the extra space makes it a different value.
Now trim first. TRIM works on a single value, so wrap it in ARRAYFORMULA to apply it across the whole range:
=UNIQUE(ARRAYFORMULA(TRIM(Raw!A2:C)))The result is now two rows: Jane Doe and Sam Lee. The trimmed rows match, so only the first is kept.
Feeding cleaned values into UNIQUE
A full pre-cleaning pass for text data combines the functions from earlier chapters:
=UNIQUE(ARRAYFORMULA(TRIM(CLEAN(SUBSTITUTE(Raw!A2:C, CHAR(160), " ")))))Read it from the inside out:
SUBSTITUTEreplaces non-breaking spaces (character code 160) with normal spaces.CLEANremoves non-printing characters.TRIMremoves leading and trailing spaces and collapses runs of spaces.ARRAYFORMULAapplies the steps to every cell in the range.UNIQUEremoves the repeated rows.
Because UNIQUE ignores case, you do not need LOWER for the comparison. You do need a case function if you want the output itself to look consistent. UNIQUE returns the first occurrence as it was written, so a row such as JANE DOE may be the one kept. To fix that, apply PROPER before deduplicating:
=UNIQUE(ARRAYFORMULA(PROPER(TRIM(Raw!A2:A))))Keeping rows that appear only once
The third argument changes the question from "give me one of each" to "give me the rows that have no repeats at all."
=UNIQUE(A2:A, FALSE, TRUE)With the list a, b, a, c, the default result is a, b, c. With exactly_once set to TRUE, the result is b, c, because a appears twice and is excluded entirely.
This is useful for finding values that occur a single time, such as an ID that was entered once and may be a typo of an ID that appears many times elsewhere. It is not a general deduplication option, because it throws away every row that had a repeat, including the first copy.
Choosing between UNIQUE, the menu tool, and a flag column
| Situation | Better choice |
|---|---|
| You want a one-time cleanup and the sheet is the final destination | Remove duplicates tool |
| You want the source to stay untouched and the result to update | `UNIQUE` |
| You need to review repeats before deleting anything | `COUNTIF` flag column |
| You need a distinct list to drive a dropdown or summary | `UNIQUE` |
| You need to decide which copy to keep (for example, the newest) | Sort first, then use either method |
UNIQUE always keeps the first occurrence. If "first" should mean "most recent," sort the source before you deduplicate. You can do that inside the formula:
=UNIQUE(SORT(Raw!A2:C, 3, FALSE))This sorts by the third column in descending order and then keeps the first row for each distinct combination. Note that sorting changes which row counts as first, but because UNIQUE compares whole rows, two rows that differ in the sort column are still different rows. To deduplicate on a key such as email while choosing the newest row, you need to compare on the key only, which takes a different technique than plain UNIQUE on all columns.
Common errors and gotchas
#REF!with "Array result was not expanded." Something is in the way. Clear the cells below and to the right of the formula, or move the formula.- Duplicates still appear. The values differ by a trailing space, a non-breaking space, or a hidden character. Clean with
TRIM,CLEAN, andSUBSTITUTEbeforeUNIQUE. - Numbers and text look alike but do not match. The number
1001and the text"1001"are different values. Check alignment: numbers align right by default, and text aligns left. - Blank rows appear in the output. Open-ended ranges such as
A2:Cinclude empty rows.UNIQUEreturns one blank row if blanks exist. Wrap the range inFILTERto remove them, for example=UNIQUE(FILTER(A2:C, A2:A<>"")). - Header row is mixed into the result. Start the range at the first data row, or the header is treated as data.
- The result changes unexpectedly. The output is live. If someone edits or sorts the source, the output follows. Paste as values if you need a stable list.
Verifying the output
After you run UNIQUE, compare counts before you trust it. In two spare cells:
=COUNTA(Raw!A2:A)
=COUNTA(Clean!A1:A)The second number must be smaller than or equal to the first. If they are equal, either there were no duplicates or something kept them apart. Spot-check by picking one value you know was repeated and searching for it in the output with Ctrl+F (Cmd+F on Mac). It should appear once.
Summary of the working model
UNIQUE reads a range, compares whole rows case-insensitively but space-sensitively, and returns the first copy of each distinct row into empty cells. Clean the text first, put the formula where its output has room, and verify the counts.
Exercise
Column A holds the values "Red", "red ", "RED", "Blue", and "Red" (the second value has a trailing space). What does =UNIQUE(A1:A5) return, and what single change to the formula would reduce the result to "Red" and "Blue"?
Show answer
It returns three values: "Red", "red ", and "Blue". The all-caps "RED" and the last "Red" are dropped because the comparison ignores case, but "red " survives because of its trailing space. Wrapping the range with trimming fixes it: =UNIQUE(ARRAYFORMULA(TRIM(A1:A5))) returns "Red" and "Blue".
Exercise
A list in A2:A contains the IDs 101, 102, 101, 103. Which formula returns only 102 and 103, and why is it not a good way to remove duplicates?
Show answer
=UNIQUE(A2:A, FALSE, TRUE). The third argument returns only values that appear exactly once. It is not a deduplication method because it also removes 101 completely, including its first copy, instead of keeping one.