Excel: XLOOKUP for First and Last Non-Blank Value in Row

In Excel for Microsoft 365 or versions 2021 and above, we can use the XLOOKUP function to retrieve the first and last non-blank values in a row.

This is particularly useful for horizontally aligned data such as daily, weekly, or monthly sales across rows, attendance records, test scores, etc.

For example, if we have weekly sales data across rows in B2:H2 and want to find the first and last sales values, we can use the following XLOOKUP formulas.

Sample Data in B1:H7:

SunMonTueWedThuFriSat
1567
52
510
0
105
203016

XLOOKUP in Excel for Finding the First Non-Blank Value in a Row

In the data above, you can use the following XLOOKUP formula in cell J2 to get the first non-blank value in that row.

=XLOOKUP(FALSE,ISBLANK(B2:H2),B2:H2,"")

Then drag the J2 fill handle down to J7 to apply the formula to each row in the range.

Finding the first non-blank value in a row in Excel

Formula Explanation

The formula follows the XLOOKUP syntax:

=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode]) 

In the formula, we have used the following arguments corresponding to each parameter:

  • lookup_value: FALSE (The value to search in the lookup_array)
  • lookup_array: ISBLANK(B2:H2) (returns FALSE wherever values present in B2:H2 and TRUE otherwise)
  • return_array: B2:H2 (the value to return from the column where lookup_value is first found in the lookup_array)
  • if_not_found: "" (return null if no match is found)
  • match_mode: omitted, 0 by default (exact match of the lookup_value)
  • search_mode: omitted, 1 by default (search from left to right)

In short, XLOOKUP searches for FALSE in the following array:

{TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE}

and returns the corresponding value from the second column in the range B2:H2.

This helps us find the first non-blank value in a row in Excel using XLOOKUP.

XLOOKUP in Excel for Finding the Last Non-Blank Value in a Row

XLOOKUP can look up a value from right to left in a horizontal range in Excel.

With minimal changes to the above formula, we can find the last non-blank value in a row in Excel.

Formula:

=XLOOKUP(FALSE,ISBLANK(B2:H2),B2:H2, "",0, -1)

Enter this formula in cell L2 and drag it down through the desired range.

Finding the last non-blank value in a row in Excel

The logic of the formula is exactly the same as the previous one. Here, we additionally specified the last two parameters:

  • match_mode: 0 (exact match)
  • search_mode: -1 (search from right to left)

Applying Lambda to Spill Formulas Down

Above, we dragged the fill handle of the formulas in cells J2 and L2 down to apply them across the entire range.

If you prefer the formulas to automatically fill down from the first row below the header, using them within an Excel table will achieve this automatically.

However, if you use a range as shown above, you may want to apply the BYROW lambda function to spill the formulas down.

To get the first non-blank value of each row, use the following BYROW formula in cell J2, provided J2:J7 is empty:

=BYROW(B2:H7, LAMBDA(val, XLOOKUP(FALSE,ISBLANK(val),val,"")))

Syntax of the BYROW function:

BYROW(array, function)

Here, the array is the range B2:H7.

The BYROW function applies the custom lambda function to each row of the array. The lambda function is defined as:

LAMBDA(val, XLOOKUP(FALSE,ISBLANK(val),val,""))

This lambda function uses XLOOKUP to find the first non-blank value in each row.

To return the last non-blank value in each row, use the following formula in cell L2, provided L2:L7 is blank:

=BYROW(B2:H7, LAMBDA(val, XLOOKUP(FALSE,ISBLANK(val),val,"",0, -1)))

Resources

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.

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

How to Retrieve the Last Record in Each Group in Excel

As part of data analysis, you may need to retrieve the last record from...

More like this

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

How to Retrieve the Last Record in Each Group in Excel

As part of data analysis, you may need to retrieve the last record from...

How to Perform Case-Sensitive XLOOKUP in Excel with EXACT

We may not always need to perform a case-sensitive XLOOKUP for product names in...

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.