Google Sheets: Get First and Last Entry Rows for Each Item

Published on

With the help of either the QUERY function or a combination of UNIQUE and XLOOKUP, you can get the first and last entry rows for each item in Google Sheets. This method returns a summary with three columns: a list of unique items (e.g., SKUs), the row number of their first appearance, and the row number of their last appearance.

Manually scanning large datasets to find when each item first or last appeared can be tedious and error-prone. Automating this process makes your analysis faster, cleaner, and more accurate.

Real-Life Use Cases: When to Get First and Last Entry Rows for Each Item

Here are a few practical scenarios where this method comes in handy:

Inventory & Stock Movement

Track when each product SKU first appeared and when it was last updated. Useful for identifying inactive products or analyzing stock movement timelines.

CRM & Sales Tracking

Summarize each customer’s first and last interaction to measure engagement duration, retention, or account inactivity.

Project & Task Tracking

Get the start and end rows for each task or project entry to monitor timelines and spot bottlenecks.

Financial Transactions

List the earliest and latest row of each recurring transaction (e.g., salaries, bills) for auditing or trend analysis.

Data Logging & Experiments

Capture the full entry range for each experiment run or subject to isolate complete datasets for deeper review.

Sample Inventory Data

Use the following example to try out the formulas:

DateSKUAction
2025-05-05SKU-001In
2025-05-07SKU-002In
2025-05-10SKU-001Out
2025-05-12SKU-003In
2025-05-13SKU-002Out
2025-05-14SKU-001In
2025-05-16SKU-003Out
2025-05-17SKU-002In
2025-05-18SKU-003In
2025-05-20SKU-001Out

In this dataset, each SKU may appear multiple times. Your goal is to get the first and last entry rows for each item (SKU).

Get First and Last Entry Rows for Each Item Using QUERY

To find the first and last row numbers for each SKU using a single formula, enter the following into cell E1:

=ArrayFormula(
   QUERY(
      HSTACK(ROW(B2:B), B2:B), 
      "select Col2, min(Col1), max(Col1) 
      where Col2 is not null 
      group by Col2 
      order by min(Col1) 
      label Col2 'SKU', min(Col1) 'First Row', max(Col1) 'Last Row'"
   )
)

Example Output: First and Last Entry Rows for Each SKU

Summary table of SKUs with first and last row numbers in Google Sheets

This tells us that SKU-001 first appeared on row 2 and last appeared on row 11, indicating a full activity range across the sheet.

How It Works:

  • HSTACK(ROW(B2:B), B2:B) pairs each SKU with its row number.
  • QUERY groups the SKUs and returns:
    • The first row (min) each SKU appears on.
    • The last row (max) each SKU appears on.
  • The results are sorted by first appearance and labeled clearly.

This gives you a quick summary table:
Get First and Last Entry Rows for Each Item — fast and formula-driven.

Get First and Last Entry Rows for Each Item Using UNIQUE + XLOOKUP

If you’re more comfortable with XLOOKUP than QUERY, follow these three simple steps:

Step 1: Get Unique Items

In E2, enter:

=UNIQUE(B2:B)

This lists each unique SKU from your data.

Step 2: Find the First Entry Row

In F2, enter:

=ArrayFormula(XLOOKUP(E2:E, B2:B, ROW(B2:B), "", 0, 1))
  • This searches for each SKU from top to bottom and returns the first matching row number.

Step 3: Find the Last Entry Row

In G2, enter:

=ArrayFormula(XLOOKUP(E2:E, B2:B, ROW(B2:B), "", 0, -1))
  • This searches from bottom to top and returns the last matching row number.

That’s it — you now have a clear view of when each SKU was first and last recorded.

Final Thoughts

Whether you use QUERY or XLOOKUP, both methods help you get the first and last entry rows for each item in Google Sheets with ease. This is a powerful technique for reporting, tracking, and analyzing anything that involves repeated entries over time.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Top Discussions

More like this

Pivot Table Formatting, Output & Special Behavior in Google Sheets

Pivot Tables in Google Sheets are powerful—but they can get tricky once you move...

Pivot Table Calculations & Advanced Metrics in Google Sheets

When it comes to built-in tools for data analysis and visualization in Google Sheets,...

Google Sheets Pivot Table Tutorial: Basics, Setup, and Date Grouping

The easiest way to summarize, analyze, and report data in Google Sheets is by...

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.