Lookup First and Last Values in a Row and Return Headers in Google Sheets

Published on

This tutorial explains how to look up the first and last values in a row and return their corresponding headers in Google Sheets. We’ll use XLOOKUP as the core function, along with some optional enhancements using BYROW to apply the logic across multiple rows automatically.

If you’re working with horizontally structured data—where each row contains values across time, categories, or stages—this technique can be incredibly helpful. We’ll walk through how to find and return the header (e.g., date or label) of the first and last non-blank values in each row.

Sample Data (Payment Tracker)

In the following sample data, let’s find when the first and last payment was received from each customer.

CustomerJan 2024Feb 2024Mar 2024Apr 2024May 2024Jun 2024
Mike200150100
Ananya250
Carol180190

We want to look up the first and last values in each row and return their corresponding month (header) in columns H and I respectively:

Lookup First Value in a Row and Return Header

To look up the first value’s header in a row, use the following formula:

=ArrayFormula(XLOOKUP("?*", TO_TEXT(B2:G2), $B$1:$G$1, "",2, 1))

Place this formula in cell H2 and drag it down to apply it to each row.

Google Sheets: Using XLOOKUP to return the first non-blank value header in each row

Formula Breakdown:

  • "?*": A wildcard pattern that matches any cell with one or more characters.
  • TO_TEXT(B2:G2): Converts numbers to text so they can match the wildcard.
  • $B$1:$G$1: The header row (static), from which we return the result.
  • "": Returns blank if nothing is found.
  • 2: Enables wildcard match.
  • 1: Searches from left to right.

Lookup Last Value in a Row and Return Header

To look up the last value’s header in a row, use this similar formula in cell I2:

=ArrayFormula(XLOOKUP("?*", TO_TEXT(B2:G2), $B$1:$G$1, "", 2, -1))

This version uses -1 as the last argument to search from right to left instead.

Google Sheets: Using XLOOKUP to return the last non-blank value header in each row

Make the Formula Spill Using BYROW

To avoid dragging formulas down manually, you can use BYROW to create an array formula that applies the same logic to each row.

For First Value Header (in H2):

=BYROW(B2:G4, LAMBDA(r, ArrayFormula(XLOOKUP("?*", TO_TEXT(r), $B$1:$G$1, "", 2, 1))))

For Last Value Header (in I2):

=BYROW(B2:G4, LAMBDA(r, ArrayFormula(XLOOKUP("?*", TO_TEXT(r), $B$1:$G$1, "", 2, -1))))

Tip: Replace B2:G4 with B2:G if you want to apply this to an expanding dataset.

Formula Breakdown:

  • BYROW(range, LAMBDA(r, ...)): Applies the formula to each row of the range.
  • TO_TEXT(r): Converts all values in the current row to text.
  • XLOOKUP(...): Finds the first/last non-blank cell and returns the corresponding header.

Additional Tip: Return the Value Instead of Header

If you want to return the value itself (not the header), just change the return range:

  • In the drag-down formulas, replace $B$1:$G$1 with B2:G2.
  • In the BYROW formulas, replace $B$1:$G$1 with just r.

Real-Life Use Cases

Here are some practical examples where looking up the first and last values in a row comes in handy:

  • Tracking customer payments: When customer names are in the first column and their payment history is spread across columns, you can return the first and last payment dates using the header row (which contains the dates).
  • Project timelines: If each row shows a project’s status across weekly or monthly columns, you can pull the first and last non-blank status entry along with the corresponding week/month.
  • Attendance logs: For students or employees, you can find when someone first attended and most recently showed up—and return the corresponding date from the header.

Resources

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

Reservation and Booking Status Calendar Template in Google Sheets

My reservation and booking status calendar template in Google Sheets uses spreadsheet cells as...

Multi-Row Dynamic Dependent Drop-Down List in Google Sheets

We can create a multi-row dynamic dependent drop-down list in Google Sheets without using...

Date-Related Conditional Formatting Rules in Google Sheets

Based on my experience, much of the conditional formatting in Google Sheets centers around...

More like this

Rental Property Manager Template for Google Sheets (Free & Fully Automated)

Do you want to manage all aspects of your rental properties, such as tracking...

Content Calendar Template in Google Sheets (Free, Dynamic & Fully Automated)

Are you a content creator managing posts across multiple platforms — Blogs, YouTube, Twitter...

Funnel Chart in Google Sheets (Dynamic without Helper Columns)

Most tutorials online create a funnel chart in Google Sheets using a stacked bar...

LEAVE A REPLY

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