Retrieve the Earliest or Latest Entry Per Category in Google Sheets

To retrieve the earliest or latest entry per category in Google Sheets, there should be a date, datetime, or time column. This allows us to sort the data by the category column and then by the date/datetime/time column in either ascending or descending order to extract the oldest or newest entries per category.

This technique is useful in many scenarios and is easy to implement in Google Sheets using just the SORT and SORTN functions—no filtering is required.

Example: Train Schedule Data

Consider a sample train schedule with destination, departure time, and train name or number. We will extract the earliest train to each location and the latest train to each location.

Sample Data

Sample data to retrieve the earliest or latest entry per category in Google Sheets

1. Retrieve the Earliest Entry Per Category in Google Sheets

Assume the above train schedule is in A1:C.

  • The category is the destination in column A.
  • To extract the earliest train for each destination, use the following formula:
=SORTN(SORT(A2:C, 1, TRUE, 2, TRUE), 9^9, 2, 1, TRUE)

Earliest Train to Each Destination:

DestinationDeparture TimeTrain Name
Cochin7:00 amRajdhani Express
Delhi5:00 amMidwest Express
Mumbai6:30 amGolden Chariot

Formula Explanation

This formula combines SORT and SORTN:

SORT:

SORT(A2:C, 1, TRUE, 2, TRUE)
  • Sorts the train schedule first by destination (column 1, ascending) and then by departure time (column 2, ascending).
  • This arranges the table so that the earliest train appears first for each destination.

SORTN:

=SORTN(..., 9^9, 2, 1, TRUE)
  • Extracts the first occurrence of each category (destination) from the sorted list.
  • Ensures that only the earliest train per category is selected.

Tip: Modify the column numbers in the formula (highlighted) to match your dataset if your category or time column is in a different position.

2. Retrieve the Latest Entry Per Category in Google Sheets

To extract the latest train for each destination, we only need to change one thing:

Modify the SORT function to sort the time column in descending order (replace TRUE with FALSE for the second sorting criteria).

Updated Formula:

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

Latest Train to Each Destination:

DestinationDeparture TimeTrain Name
Cochin11:30 pmA/C Express
Delhi10:50 pmLate Night Express
Mumbai8:15 pmAjanta Express

Does This Formula Work with Timestamps or Dates?

Yes! This method works for extracting the earliest or latest entry per category based on time, date, or datetime values.

If you need:

  • To find the last row in each group without sorting by date, check this post.
  • To extract the last entry by date, check this post.

Conclusion

We have seen one of the simplest formulas to extract the earliest or latest record per category in Google Sheets.

Key Takeaways:

  • We used the SORTN function’s duplicate removal feature to achieve this.
  • The SORT function is optional if you manually sort your data (Data > Sort range), but using SORT ensures your source data remains unaltered.
  • This method works for time, date, and datetime columns in Google Sheets.

Now you can apply this to your own datasets and automate earliest/latest data retrieval in Google Sheets!

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.

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

Find Common Items Across Multiple Columns in Google Sheets

This tutorial explains how to find common items across multiple columns in Google Sheets....

Sort Column by Length of Text in Google Sheets

To sort a column by length of text, you can either use the QUERY...

More like this

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

Find Common Items Across Multiple Columns in Google Sheets

This tutorial explains how to find common items across multiple columns in Google Sheets....

8 COMMENTS

    • Hi, Marlon Rafael Orpilla,

      It’s not clear whether you have data spread across several months.

      If you want me to look into this problem, please make a sample sheet with enough sample data and share that link/URL via ‘reply’ below.

      I won’t publish that link.

  1. This is super helpful! How can I do that, remove the duplicates from one column, leaving only the most recent date, but also show the data sorted by 2 sort ranges?

    • Hi, Jessica Gonzalez,

      Use SORTN as explained. Then wrap the output with SORT to sort by two sort ranges.

      If you want more clarification, please do leave your sample sheet URL in the reply.

  2. This is very helpful. Thank you. How can we find the second-highest value based on the timestamp? Is it possible with the Sortn function?

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.