How to Keep the Six Most Recent Records for Each ID in Excel
Question details
The user needs a method to identify and retain the six newest entries for every unique ID based on the collection date, without permanently deleting the older historical data.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Managing and cleaning a large dataset where only the most recent historical data per category or ID is required for reporting and analysis.
- Observed behavior
- The dataset currently displays all historical records, and the user requires a targeted way to isolate the top 6 newest entries per ID without manually scanning dates.
Ensure your dataset is organized in a clear tabular layout with distinct columns for 'ID' and 'Date'. It is highly recommended to create a backup copy of your worksheet before applying any sorting or filtering, especially if you plan to delete the older records later.
Sort and Filter with a COUNTIF Helper Column
A reliable, non-destructive method that uses basic sorting and a simple counting formula to rank records by date for each ID.
This approach works in all versions of Excel and ensures you don't accidentally delete data. By sorting the data first, you can use a running count to identify the top 6 newest records chronologically.
Select your entire data range. Go to the 'Data' tab on the ribbon and click 'Sort'. Add a level to sort by 'ID' (A to Z), then click 'Add Level' and sort by 'Date' (Newest to Oldest).
Insert a new column next to your data and name it 'Rank'. Assuming your IDs are in column A and your data starts in row 2, click cell C2 (or your new column's cell) and enter the formula: =COUNTIF($A$2:A2, A2).
Press Enter, then double-click the fill handle (the small square at the bottom-right of the cell) to copy the formula down to the rest of the rows. This will number the occurrences of each ID from 1 upwards based on the sorted dates.
Select your header row, go to the 'Data' tab, and click 'Filter'. Click the filter dropdown arrow on the 'Rank' column, choose 'Number Filters' > 'Less Than Or Equal To', type 6, and click 'OK'.

Extract Recent Records Using Power Query
Ideal for large datasets and automated workflows, Power Query can group data by ID and retain only the top rows externally.
Use Dynamic Array Formulas (Excel 365 / 2021)
Extract the six most recent records to a new location dynamically without touching the original dataset.
Easily Filter and Analyze Data with WPS Spreadsheet
WPS Spreadsheet offers powerful sorting, filtering, and formula capabilities to help you effortlessly manage large datasets and extract the most recent records per ID. It supports advanced functions and handles complex data manipulation with ease.
- 1. Open Your File: Launch WPS Spreadsheet and open your existing Excel workbook containing the dataset.
- 2. Sort the Data: Highlight your dataset, navigate to the 'Data' tab, and use the 'Sort' feature to order by ID (Ascending) and Date (Descending).
- 3. Apply Formula: Insert a new column and apply the =COUNTIF($A$2:A2, A2) formula to calculate the chronological rank of each record.
- 4. Filter Results: Click 'AutoFilter' under the Data tab and set the rank column to show values less than or equal to 6.

Frequently Asked Questions
Can I delete the older records instead of just hiding them?
Yes. After using the helper column method to filter your data for ranks greater than 6, you can highlight the visible rows, right-click the row numbers, and select 'Delete Row'. Once you clear the filter, only the 6 most recent records per ID will remain.
Why does my COUNTIF formula return the same number for every row?
This usually happens if you didn't use absolute and relative cell references correctly. Ensure the first part of your range is locked with dollar signs (e.g., $A$2:A2). This creates an expanding range that correctly increments the count as the formula is dragged down.
Is there a way to identify recent records without sorting the original data?
Yes. You can use Power Query, which processes the data externally without altering the layout of your original source table. Alternatively, you can use a more complex array formula like SUMPRODUCT to rank dates based on multiple conditions, though this can slow down large spreadsheets.
Does the FILTER function work in all versions of Excel?
No, the FILTER and TAKE functions are dynamic array functions available only in Excel for Microsoft 365, Excel 2021, and newer versions. For older versions, the COUNTIF helper column method is the most reliable approach.




