
Remove Excel Duplicates and Keep Only the Latest Value — Extract the Last Row by ID (UNIQUE, MAP, XMATCH, Power Query)
Summary: The core requirement is to keep only the most recent row for each customer, product, or ticket (ID). In Excel 365, create an ID list with UNIQUE, then use MAP+FILTER+SORT or XMATCH(search_mode -1) to find the last position and return the entire row. For large datasets, Power Query’s Group By → Max → Merge is the most robust option.
1) MAP/FILTER/SORT method (most readable)
Assume the table is named Tbl and has the columns ID, Date, Value. Sort each ID group by date in descending order and return only the first row.
=LET(
ids, UNIQUE(Tbl[ID]),
hdr, Tbl[#Headers],
result, MAP(ids, LAMBDA(i, TAKE(SORT(FILTER(Tbl, Tbl[ID]=i), XMATCH("Date", Tbl[#Headers]), -1), 1))),
VSTACK(hdr, result)
)
The key is to create a group with FILTER(Tbl, Tbl[ID]=i), sort by date in descending order with SORT(...,-1), and keep the first row with TAKE(...,1). Finding the Date column number with XMATCH also keeps the formula safe if the column order changes.
2) Find the “last position” with XMATCH and use INDEX
This method finds the last position for each ID by searching from the bottom up with XMATCH(search_mode,-1), then returns the entire row with INDEX.
=LET(
ids, UNIQUE(Tbl[ID]),
hdr, Tbl[#Headers],
rows, BYROW(ids, LAMBDA(r, INDEX(Tbl, XMATCH(INDEX(ids,r), Tbl[ID], 0, -1), ))),
VSTACK(hdr, rows)
)
search_mode = -1 means searching from bottom to top. When an ID appears multiple times, the last row—usually the row with the latest date—is selected. If date order is not guaranteed, it is safer to sort the source first with SORTBY(Tbl, Tbl[Date], 1).
3) Power Query — Group By + Max
- Data → From Table/Range → open the query
- Group By ID → add a Max of Date column
- Merge with the source (ID=ID, Date=Max Date)
- Expand only one row from the merged column → Close & Load
It is resilient to refreshes and stable even with hundreds of thousands of rows.
4) PivotTable alternative (summary + source connection)
In a PivotTable, summarize with Rows: ID and Values: Date (Max), then join the result to the source with XLOOKUP to retrieve other columns.
=XLOOKUP(1, (Tbl[ID]=A2)*(Tbl[Date]=B2), Tbl[Value])
5) Validation and maintenance checklist
- If dates are stored as text, sorting will fail. Normalize them with
NUMBERVALUEorDATEVALUE. - Set a policy in advance for duplicate rows with the same ID and date: first row, last row, or aggregation.
- Convert the range to a table (Ctrl+T) and use structured references so ranges expand automatically.