Sub topic 8
Review Duplicates Before Deleting Them with COUNTIF
Prerequisites and the review-first workflow
This chapter assumes you can select ranges, enter formulas, and use the Google Sheets menu bar. You should also understand that a row is a duplicate only when the columns that identify the record match. Two customers might share a name but have different email addresses, so deciding what counts as a duplicate is part of the cleanup.
Deleting duplicates immediately can remove valid records. A safer workflow is:
- Keep the original data unchanged.
- Add a temporary helper column.
- Flag repeated values with
COUNTIF. - Review the flagged rows.
- Delete only confirmed duplicates.
- Remove the helper column.
COUNTIF is a function that counts cells matching a condition. In this workflow, it checks how many times the current value has appeared from the first data row through the current row.
Flag repeated values with COUNTIF
Suppose column A contains customer email addresses, with a header in A1 and data starting in A2. Add Duplicate? to B1. Enter this formula in B2:
=COUNTIF($A$2:A2, A2) > 1Fill the formula down the column. The first occurrence of each email returns FALSE. Later occurrences return TRUE.
The dollar signs make the starting point, $A$2, fixed. The ending point, A2, changes as the formula moves down. This creates a growing range:
| Row | Formula checks | Duplicate? | |
|---|---|---|---|
| 2 | [email protected] | A2:A2 | FALSE |
| 3 | [email protected] | A2:A3 | FALSE |
| 4 | [email protected] | A2:A4 | TRUE |
The formula marks only repeats after the first occurrence. This lets you retain the first row and review the later rows before removing them.
To avoid marking blank cells as duplicates, use a blank check:
=IF(A2="", "", COUNTIF($A$2:A2, A2) > 1)This returns an empty cell when A2 is blank.
Decide which rows are truly duplicates
Filter the helper column to show TRUE, then compare the flagged rows with their original records. Check whether the matching value is enough to identify the same record. If the data has an order date, transaction ID, or status column, those fields may show that two similar rows are legitimate.
If a record is identified by several columns, create a helper key that combines those columns. For example, if an order is unique by customer email and order date, place this in a helper column:
=A2&"|"&TEXT(B2, "yyyy-mm-dd")Then apply the COUNTIF formula to the helper key column instead of checking only one field.
Common errors include:
- Inconsistent spaces:
[email protected]and[email protected]look identical but are different values. - Different capitalization: Depending on the comparison and workflow, capitalization can make review confusing. Normalize values before checking.
- Wrong starting row: If the data begins in row 3, use
$A$3:A3andA3. - Counting the wrong field: A shared name is usually a weaker duplicate key than an email address or record ID.
- Deleting every
TRUErow without review: Some repeats represent valid events rather than accidental copies.
Understand the result
The first matching value is preserved because the formula counts it as occurrence 1. Each later match is flagged. This is different from a formula that labels every value occurring more than once, including the first occurrence.
Exercise
In a list where the same email appears in rows 2, 5, and 8, which rows does =COUNTIF($A$2:A2, A2)>1 flag when filled down?
Show answer
Rows 5 and 8. Row 2 is the first occurrence, so it returns FALSE.