How to Remove Duplicates in Google Sheets but Keep the Latest, First or Highest Row per Key (2026)
The same customer, student or serial number shows up on several rows: a form submitted twice, an export appended every month, an order history with one row per purchase. You want one row per key, but not just any row: the most recent one, the first one, or the one with the largest amount. Google Sheets’ built-in Remove duplicates always keeps the first row it meets, so the trick is to control what “first” means, or to use a formula that picks the right row for you. This guide shows both, with a worked example, and how to count the duplicates per key before you remove anything.
The example
An Orders tab with a header row and one row per order. Ana has three orders, Bo two:
| A: Email | B: Name | C: Date | D: Total | |
|---|---|---|---|---|
| 2 | ana@x.com | Ana | 2026-03-02 | 40 |
| 3 | bo@x.com | Bo | 2026-03-05 | 15 |
| 4 | ana@x.com | Ana Lee | 2026-04-11 | 25 |
| 5 | cy@x.com | Cy | 2026-02-20 | 60 |
| 6 | bo@x.com | Bo | 2026-01-30 | 90 |
| 7 | ana@x.com | Ana Lee | 2026-03-28 | 70 |
| 8 | dee@x.com | Dee | 2026-04-01 | 10 |
The newest row per email is 4, 3, 5 and 8. The first row per email is 2, 3, 5 and 8. The highest total per email is 7, 6, 5 and 8. Notice that the rows are not in date order, which matters for the shortcuts further down.
Before you start: check that column C holds real dates. =ISNUMBER(C2) should return TRUE. A date imported as text sorts like text (4/11/2026 comes before 4/2/2026), so “newest first” silently picks the wrong row. If it returns FALSE, select the column and use Format › Number › Date, or re-enter the values, before you go on.
Method 1: sort, then Remove duplicates (deletes in place)
- Make a copy of the tab (right-click the tab › Duplicate).
- Select the whole table, including the header, and choose Data › Sort range › Advanced range sorting options. Tick Data has header row, sort by Date, Z → A. Sort the whole table, not just the date column, or rows will be scrambled.
- With the table still selected, choose Data › Data cleanup › Remove duplicates, tick Data has header row, and tick only the Email column under Columns to analyze.
- Click Remove duplicates. The tool keeps the first row of each email, which after the sort is the newest.
To keep the highest total instead, sort by Total, Z → A. To keep the first-ever row, sort by date A → Z (or do not sort at all if the sheet is already in the order the rows arrived). The catch: this deletes rows straight away with no preview, and you have to remember to sort every time new rows come in.
Method 2: one formula that keeps the latest row per key (SORTN)
On a new tab, in A2 (put your own headings in row 1):
=SORTN(SORT(FILTER(Orders!A2:D, Orders!A2:A<>""), 3, FALSE), 9^9, 2, 1, TRUE)
Read it from the inside out:
FILTER(Orders!A2:D, Orders!A2:A<>"")drops empty rows. Without it, the blank rows below your data come back as one empty “key”.SORT(…, 3, FALSE)sorts by the third column of the range (Date), newest first. Google’s help notes that SORT sorts “only by the specified columns”; the other columns stay with their row.SORTN(…, 9^9, 2, 1, TRUE)returns up to 9^9 rows (that is, all of them), sorted by column 1 (Email) A → Z, with tie mode2, which Google’s help describes as “Show at most the first n rows after removing duplicate rows”. Rows count as duplicates when their sort column matches, and the one that survives is the one that came first in its input, here the newest. Google’s own SORTN example shows the same: sorting by Test 1 score with mode 2, Alice and Devon both score 100, and only Alice, the earlier row, is returned.
Result for the example: ana@x.com (2026-04-11), bo@x.com (2026-03-05), cy@x.com, dee@x.com, one row each, with all four columns. The result updates as new orders arrive, and the original tab is never changed. When you need a fixed list, copy it and use Edit › Paste special › Values only.
Keep the highest (or lowest) value instead. Sort by the Total column:
=SORTN(SORT(FILTER(Orders!A2:D, Orders!A2:A<>""), 4, FALSE), 9^9, 2, 1, TRUE)
That returns rows 7, 6, 5 and 8 (ana 70, bo 90). Change FALSE to TRUE inside SORT to keep the lowest total per email, or the oldest date.
A key made of several columns. Add more sort columns to SORTN: =SORTN(SORT(FILTER(Orders!A2:D, Orders!A2:A<>""), 3, FALSE), 9^9, 2, 1, TRUE, 2, TRUE) keeps the newest row for each Email + Name pair. In the example that returns two rows for ana@x.com, one for “Ana” and one for “Ana Lee”, which is a sign the names need cleaning, not that the formula is wrong.
The mistake to avoid. It is tempting to write =SORTN(A2:D, 9^9, 2, 1, TRUE, 3, FALSE), sorting by email and then date inside SORTN. That removes nothing: with tie mode 2 every sort column is part of the duplicate check, and no two rows share the same email and date. Sort by the date with SORT first, and give SORTN only the key column(s).
Keep the first row per key
If you want the earliest row as the sheet lists it, the simplest formula leaves out the SORT:
=SORTN(FILTER(Orders!A2:D, Orders!A2:A<>""), 9^9, 2, 1, TRUE)
This returns rows 2, 3, 5 and 8, sorted by email. To keep the rows in their original order instead, filter on a running count:
=FILTER(Orders!A2:D, Orders!A2:A<>"", COUNTIFS(Orders!A2:A, Orders!A2:A, ROW(Orders!A2:A), "<="&ROW(Orders!A2:A))=1)
For each row, COUNTIFS counts the rows from the top down to this one that have the same email; the first copy counts 1. Change "<=" to ">=" to keep the last row in sheet order instead. On large sheets this recalculates slowly, because every row scans the column.
Method 3: flag the row to keep, without moving anything (MAXIFS)
When you want to see the decision next to each row, or delete by hand, add a helper column. In E2 on the Orders tab, fill down:
=IF(A2="", "", C2=MAXIFS($C$2:$C, $A$2:$A, A2))
MAXIFS finds the newest date for this row’s email; the cell says TRUE on the row to keep and FALSE on the older copies. In the example rows 3, 4, 5 and 8 say TRUE. Then either filter column E on FALSE (Data › Create a filter) and delete those rows, or list the keepers elsewhere with =FILTER(Orders!A2:D, Orders!E2:E=TRUE). Swap C for D to flag the highest total. Two things to know: if two rows for the same email share the newest date, both say TRUE, so add a time to the date or decide by hand; and MAXIFS returns 0 when nothing matches, as Google’s help notes.
Count the duplicates per key first
Before deleting, it helps to know how much you are about to remove. One formula lists every key once with its number of rows and newest date:
=QUERY(Orders!A1:D, "select A, count(A), max(C) where A is not null group by A order by count(A) desc label count(A) 'Rows', max(C) 'Latest'", 1)
For the example: ana@x.com 3 rows (latest 2026-04-11), bo@x.com 2, cy@x.com 1, dee@x.com 1. The final 1 tells QUERY there is one header row. Google’s help warns that QUERY gives each column a single data type and treats the minority type as empty, so a few text dates in a date column turn into blanks; one more reason to fix the dates first.
For a count on every row instead, put =COUNTIF($A$2:$A, A2) in a helper column: 3, 2, 3, 1, 2, 3, 1. The number of rows that a dedupe will remove is the total minus the distinct keys: =COUNTA(A2:A)-COUNTUNIQUE(A2:A), 3 in the example. More counting formulas, and why letter case can change this number, are in how to count duplicates.
A shortcut that only works for append-only sheets (XLOOKUP)
If you already have a list of keys and only need the latest values for each, XLOOKUP can search from the bottom up. Its search_mode argument -1 means “search from the last entry to the first”:
=XLOOKUP(F2, Orders!A2:A, Orders!B2:D, "", 0, -1)
That returns the last row for the email in F2, which is the newest only when rows are always added at the bottom in date order, like Google Form responses. In the example it is wrong: Ana’s last row is the March 28 order, not April 11. If you are not sure the sheet is in date order, use Method 2 or 3.
Optional: a script that deletes the older rows in place
For a sheet that keeps growing, such as form responses you want to trim regularly, this Apps Script keeps the newest row per key on the active tab and deletes the rest. It copies the tab to a backup first, compares keys ignoring case and surrounding spaces, keeps the lower row when two dates tie, and stops with an error, deleting nothing, if a key row has a date that is not a real date. Open Extensions › Apps Script, paste it, set the two column numbers, save, then select the tab and run keepLatestPerKey.
// Keep only the newest row per key; delete the older ones. Makes a backup tab first.
// Assumes one header row. KEY_COL and DATE_COL are column numbers (A = 1).
const KEY_COL = 1; // Email
const DATE_COL = 3; // Date
function keepLatestPerKey() {
const ss = SpreadsheetApp.getActive();
const sheet = ss.getActiveSheet();
const stamp = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), 'yyyy-MM-dd HHmm');
sheet.copyTo(ss).setName(sheet.getName() + ' backup ' + stamp);
const rows = sheet.getDataRange().getValues().slice(1);
const toDelete = rowsToDelete_(rows, KEY_COL - 1, DATE_COL - 1);
toDelete.reverse().forEach(i => sheet.deleteRow(i + 2)); // bottom-up so row numbers stay valid
ss.toast(toDelete.length + ' older rows deleted');
}
function rowsToDelete_(rows, k, d) {
const best = new Map(); // key -> index of the newest row so far
rows.forEach((r, i) => {
const key = String(r[k]).trim().toLowerCase();
if (!key) return;
if (Object.prototype.toString.call(r[d]) !== '[object Date]') throw new Error('Row ' + (i + 2) + ': the date column is not a real date');
const j = best.get(key);
if (j === undefined || r[d].getTime() >= rows[j][d].getTime()) best.set(key, i); // tie: the lower row wins
});
const keep = new Set(best.values());
return rows.map((r, i) => i).filter(i => String(rows[i][k]).trim() !== '' && !keep.has(i));
}
It deletes rows one at a time, which is fine for hundreds of rows but slow for many thousands; for big sheets, use the SORTN formula on a new tab. Deleting rows also shifts anything that refers to specific rows, so check formulas and charts that point at this tab afterwards.
Pitfalls
- Keys that look the same but aren’t.
Ana@X.comwith a trailing space andana@x.commay be treated as two keys. Clean the key column first (a helper column with=LOWER(TRIM(A2))is often enough); duplicates that don’t match exactly covers the harder cases. - Dates stored as text sort wrongly and become blanks in QUERY. Check with
ISNUMBER. - Ties. Two rows with the same key and the same newest date: the formulas keep one of them, and the MAXIFS flag marks both. Use a timestamp with a time, or decide by hand.
- Sorting one column only. In Method 1, sorting just the date column detaches dates from their rows. Always sort the whole table.
- Different copies hold different details. If the newest row is missing a phone number that an older row has, keeping only the newest loses it. That calls for a merge, not a delete.
FAQ
Which row does Remove duplicates keep in Google Sheets?
The first row of each group, counting from the top of the selected range. To keep the newest row instead, sort the whole table by the date column, newest first, before you run Data › Data cleanup › Remove duplicates with only the key column ticked.
How do I keep the most recent entry per person with a formula?
Use =SORTN(SORT(FILTER(A2:D, A2:A<>""), 3, FALSE), 9^9, 2, 1, TRUE) where column A holds the key (for example email) and column 3 of the range holds the date. SORT puts the newest row first and SORTN with tie mode 2 keeps one row per key.
Why does SORTN not remove anything when I add the date as a sort column?
With display_ties_mode 2, SORTN treats rows as duplicates only when all of its sort columns match. If the date is a sort column, every email-and-date pair is different, so nothing is removed. Sort by date with SORT first and give SORTN only the key column.
How do I keep the row with the highest value per key?
Same pattern, sorting by the value column instead: =SORTN(SORT(FILTER(A2:D, A2:A<>""), 4, FALSE), 9^9, 2, 1, TRUE). Use TRUE instead of FALSE inside SORT to keep the lowest value.
How do I count how many times each value appears?
=QUERY(A1:D, "select A, count(A) where A is not null group by A order by count(A) desc label count(A) 'Rows'", 1) lists each key once with its number of rows. For a count next to every row, use =COUNTIF($A$2:$A, A2) in a helper column.
Can XLOOKUP return the latest entry?
XLOOKUP with search_mode -1 returns the last match in sheet order, for example =XLOOKUP(F2, A2:A, C2:C, "", 0, -1). That is the newest entry only when rows are added in date order, as with Google Form responses. Otherwise use the SORTN or MAXIFS method.
Install free from Google Workspace MarketplaceSee SheetDupes
Want to test it first? Try it on sample data (makes a copy of a 12-row example sheet in your Drive).
Related guides
- How to Find and Remove Duplicates in Google Sheets: Highlight, Count, Delete and Keep the Original (2026)
- How to Highlight Duplicates in Google Sheets: Conditional Formatting Formulas for One Column, Whole Rows, Repeats Only and Two Sheets (2026)
- How to Compare Two Columns or Two Sheets in Google Sheets to Find Matches and Duplicates (2026)
- Duplicates That Don’t Match Exactly in Google Sheets: Case, Extra Spaces, Gmail Dots, Phone Formats and Typos (2026)
- How to Prevent Duplicate Entries in Google Sheets: Data Validation Rules, Warnings and a Small Script (2026)
- How to Count Duplicates in Google Sheets: per Value, Extra Rows, Unique Counts and Why the Numbers Disagree (2026)
- How to Combine Two Lists or Several Tabs in Google Sheets Without Duplicates: VSTACK, UNIQUE, One Row per Key and Other Files (2026)
- How to Merge Duplicate Rows in Google Sheets: Sum the Numbers, Join the Notes and Fill the Blanks (2026)
- UNIQUE vs Remove Duplicates in Google Sheets: Live or Static, Letter Case, Spaces, Columns and Headers (2026)
- How to Remove Duplicates Within a Cell in Google Sheets: Comma-Separated Lists, Tags, Line Breaks and Letter Case (2026)
- How to Show Only the Duplicates in Google Sheets: Filter Duplicate Rows, List Repeated Values and Find Rows That Appear Once (2026)
- How to Find Duplicates Across Multiple Columns in Google Sheets: Rows That Match on Two or More Columns, and Values Repeated Anywhere in a Range (2026)
- How to Remove Duplicates Automatically in Google Sheets: Apps Script on a Daily Timer, on Form Submit or From a Menu (2026)
- How to Find Duplicates in Google Sheets but Ignore Blank Cells: Highlight, Count, List and Remove Without Empty Rows Getting in the Way (2026)
- How to Find Duplicate Transactions in Google Sheets: Double-Imported Rows, Invoices Paid Twice and Same-Amount Charges Within a Few Days (2026)
- How to Format Phone Numbers in Google Sheets: Strip Dashes and Spaces, Show (555) 010-1234, Handle +1 and Extensions, Then Find Duplicate Contacts (2026)
- How to Change Text Case in Google Sheets: UPPERCASE, lowercase, Proper Case Names, Sentence Case and Duplicates That Differ Only in Case (2026)
Function behavior is taken from Google’s Docs Editors Help pages for SORTN, SORT, MAXIFS, QUERY and XLOOKUP. Instructions refer to Google Sheets as it worked on November 18, 2026; menu names can change. Google Sheets is a trademark of Google LLC and this site is not affiliated with Google. Questions or corrections: hello@loomwise.app.