Extract the Last Entry of Each Item by Date in Google Sheets

You don’t need to use QUERY or FILTER to extract the last entry of each item by date in Google Sheets. Instead, you can use a simple combination of SORT and SORTN.

The SORT function is unnecessary if your data is already sorted by the item column and then by the date column. However, for flexibility, we will include it. This formula extracts the latest records from each group based on the date entries.

Where Is This Useful?

Extracting the last entry of each item by date in Google Sheets is useful in various real-life scenarios, such as:

  • Tracking the last recorded stock update for each product.
  • Finding the latest PO (purchase order) for each customer.
  • Extracting the most recent payment or invoice details.
  • Tracking the most recent leave request per employee/student.
  • Extracting the most recent status change for ongoing projects.

Generic Formula

=SORTN(SORT(range, key_col, TRUE, dt_col, FALSE), 9^9, 2, key_col, TRUE)

Where:

  • range – The data range containing the records.
  • key_col – The column number determining the grouping (e.g., product name column number, employee name column number).
  • dt_col – The column number containing the date entries.

Example 1: Extracting the Last Leave Request for Each Employee

Consider the following employee leave data:

Sample data for extracting the last leave request for each employee in Google Sheets

To extract the last leave request record for each employee:

  • key_col is column A (Employee Name).
  • dt_col is column E (Leave Request Date).

Formula:

=SORTN(SORT(A2:E, 1, TRUE, 5, FALSE), 9^9, 2, 1, TRUE)

Output:

EmployeeLeave TypeStart DateEnd DateRequest Date
AaronVacation1-Mar-20245-Mar-202420-Feb-2024
GayatriVacation11-Mar-202415-Mar-20249-Mar-2024

How This Works:

  • SORT(A2:E, 1, TRUE, 5, FALSE): Sorts data by employee name (ascending) and then by request date (descending).
  • This ensures that each employee’s latest leave request appears first in their group.
  • SORTN(..., 9^9, 2, 1, TRUE): Removes duplicate rows based on employee names, keeping only the latest record per employee.

Example 2: Extracting the Last Stock Update per Product

Here is sample data for tracking the last recorded stock update for each product:

Sample data for extracting the last stock update per product in Google Sheets

To extract the last entry of each Product by date:

=SORTN(SORT(A2:D, 2, TRUE, 4, FALSE), 9^9, 2, 2, TRUE)

Output:

CategoryProductStock QtyUpdate Date
ElectronicsLaptop1010-Feb-2025
AccessoriesMouse407-Feb-2025

To extract the last updated date by Category:

=SORTN(SORT(A2:D, 1, TRUE, 4, FALSE), 9^9, 2, 1, TRUE)

Output:

CategoryProductStock QtyUpdate Date
AccessoriesMouse407-Feb-2025
ElectronicsLaptop1010-Feb-2025
GamingLaptop125-Feb-2025

To extract the last updated date by Category and then by Product:

=SORTN(SORT(A2:D, 1, TRUE, 2, TRUE, 4, FALSE), 9^9, 2, 1, TRUE, 2, TRUE)

Output:

CategoryProductStock QtyUpdate Date
AccessoriesMouse407-Feb-2025
ElectronicsLaptop1010-Feb-2025
GamingLaptop125-Feb-2025
GamingMouse453-Feb-2025

Conclusion

This tutorial demonstrated how to extract the last entry of each item by date in Google Sheets using SORT and SORTN. These formulas help efficiently retrieve the most recent records in various scenarios.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Filter Top N per Category in Excel (Using FILTER & COUNTIFS)

You can use a combination of FILTER and COUNTIFS to filter the top N...

Rank per Group in Excel

You have two groups of 20 students each. How do you determine the rank...

Real-Time Excel Filtering Using Combo Box & FILTER Function

In this tutorial, you’ll learn how to set up real-time Excel filtering using a...

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

More like this

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

How to Extract Top N from Aggregated Query Results in Google Sheets

To summarize data in Google Sheets, you can use the QUERY function. However, if...

How to Use RANK IF in Google Sheets (Conditional Ranking)

You can use the RANK function to rank values in an entire dataset. But...

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.