Lookup the Smallest Value in a 2D Array in Google Sheets

Published on

We can use a combination of BYROW and SMALL with FILTER or XLOOKUP to look up the smallest value in a 2D array in Google Sheets.

What does looking up the smallest value in a 2D array mean?

Assume you have sales dates in a column (range A2:A11) and sales quantities from different regions in the next few columns (B2:E11). The task is to find the first, second, and third smallest values from the range B2:E11 and return the corresponding dates from A2:A11.

Lookup the smallest value in a 2D array in Google Sheets
Lookup the smallest value in a 2D array

We will discuss how to achieve this in this Google Sheets tutorial.

Step 1: Find the Smallest Value in a 2D Array

To find the smallest value in the range B2:E11, use the following SMALL formula in Google Sheets:

=SMALL(B2:E11, 1)

Enter this formula in cell G2.

Finding the nth smallest value in a 2D array

To find the second smallest value, replace 1 with 2. For the third smallest value, replace 1 with 3, and so on.

For this example, we will look up the first smallest value in the 2D array. The formula will return 50. If you manually check the range, you’ll see this value is present in cells D3 and C9.

How do you find the corresponding dates?

Step 2: Match the Smallest Value in Each Row

Next, compare the smallest value in the 2D array with the smallest value in each row. Use the following formula in cell H2 and drag it down to H11:

=$G$2=SMALL(B2:E2, 1)
Matching the smallest value in each row

If the formula returns TRUE, it indicates a match. Therefore, you should look up TRUE to identify the corresponding dates.

Step 3: Lookup the Smallest Value in a 2D Array (Using TRUE for Match)

To look up the smallest value, use FILTER or XLOOKUP.

XLOOKUP will return the date corresponding to the first occurrence of the smallest value, while FILTER can return the dates of all occurrences.

In cell I2, enter the following XLOOKUP formula to get the date corresponding to the smallest value:

=XLOOKUP(TRUE, H2:H11, A2:A11) // returns 15/09/2024

XLOOKUP searches for the key TRUE in H2:H11 and returns the corresponding date from A2:A11.

Alternatively, you can use FILTER:

=FILTER(A2:A11, H2:H11) // returns 15/09/2024 and 21/09/2024

FILTER returns the dates from A2:A11 where the condition in H2:H11 is TRUE.

Lookup the Smallest Value in a 2D Array with an Array Formula

You can use the following approach to avoid using helper ranges and prefer a single formula to look up the nth smallest value in a 2D array.

This method eliminates the need for the helper range H2:H11 in both XLOOKUP and FILTER.

You can use the BYROW and SMALL combination to replace H2:H11 in the formulas:

BYROW(B2:E11, LAMBDA(r_range, SMALL(B2:E11, 1)=SMALL(r_range, 1)))

The BYROW function applies a custom lambda function to each row in the range.

  • SMALL(B2:E11, 1)=SMALL(r_range, 1) checks if the smallest value in B2:E11 (the 2D array) is equal to the smallest value in each row.

Here, SMALL(B2:E11, 1) is the smallest value in the entire 2D array, and SMALL(r_range, 1) is the smallest value in the current row. r_range represents the current row within B2:E11, and BYROW iterates through each row, adjusting r_range accordingly.

To lookup the smallest value in a 2D array, use the following formulas:

XLOOKUP:

=XLOOKUP(
   TRUE, 
   BYROW(B2:E11, LAMBDA(r_range, SMALL(B2:E11, 1)=SMALL(r_range, 1))), 
   A2:A11
)

FILTER:

=FILTER(
   A2:A11, 
   BYROW(B2:E11, LAMBDA(r_range, SMALL(B2:E11, 1)=SMALL(r_range, 1)))
)

Replace 1 with 2, 3, or the nth position you want to find.

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.

Case-Sensitive Running Count in Google Sheets

This tutorial provides a solution for incorporating case sensitivity into the running count of...

Fix Fractional Percentage Formatting Issues in Google Sheets

Applying data validation is the only effective solution to resolve issues related to fractional...

How to Duplicate Rows Dynamically Based on Cell Values in Excel

This tutorial explains how to duplicate rows based on cell values in a column...

Unique List by Section in Excel

If you have a list in a column separated by categories, you might want...

More like this

Case-Sensitive Running Count in Google Sheets

This tutorial provides a solution for incorporating case sensitivity into the running count of...

Fix Fractional Percentage Formatting Issues in Google Sheets

Applying data validation is the only effective solution to resolve issues related to fractional...

Google Sheets: Adaptive Study Planner with Auto-Reschedule

Below is a free download link to an adaptive study planner template for use...

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.