UNIQUE Function in Visible Rows in Google Sheets

The UNIQUE function doesn’t inherently include only visible rows when it returns values, discarding duplicates in Google Sheets.

However, we can utilize a workaround method to achieve the desired result involving the MAP lambda function.

The logic is as follows: We will first identify visible rows and use the FILTER function to extract them. Then we apply the UNIQUE function to the extracted range.

Below, you will find step-by-step instructions for applying the UNIQUE function to visible rows in Google Sheets.

Sample Data and Testing Default Behavior of UNIQUE in a Filtered Range

The sample data is in A1:D16, where A1:D1 contains the field labels.

There are two unique categories in A2:A16, i.e., Electronics and Clothing, which we can extract using the following UNIQUE formula:

=UNIQUE(A2:A16)

Let’s filter out one of the categories, which is Electronics, and see what the formula returns.

For that, select A1:D16 and click Data > Create a Filter.

Sample Filtered Data in cells A1 to D16

Click the filter drop-down in cell A1, uncheck Electronics, and click OK.

Now, there is only one visible category, which is Clothing. However, the above UNIQUE formula will still return the same two unique categories.

The proper way to apply the UNIQUE function to visible rows in Google Sheets is here.

UNIQUE Function in Visible Rows in Google Sheets

Step-by-Step Instructions for Using the UNIQUE Function in Visible Rows

To determine whether cell A2 is visible in Google Sheets, you can use the following SUBTOTAL formula:

=SUBTOTAL(103, A2)

This formula outputs 0 if row #2 is hidden and 1 if it’s visible. Function #103 represents COUNTA in visible cells.

To apply this formula to each row in the range A2:A16, you need to convert it into a custom LAMBDA function.

The syntax for creating a custom lambda function is:

LAMBDA(name, formula_expression)

Here’s the custom lambda function:

LAMBDA(r, SUBTOTAL(103, r))

You can use this lambda function within the MAP function. The syntax for MAP is:

=MAP(array1, lambda)

Where array1 is A2:A16.

=MAP(A2:A16, LAMBDA(r, SUBTOTAL(103, r)))

In this lambda function, “r” represents the current element in the array. The MAP function iterates over each value in the array and returns 1 for visible rows and 0 for hidden rows.

Next, you can FILTER the range A2:A16 where the MAP output is 1. The syntax for FILTER is:

=FILTER(range, condition)

Here is that FILTER formula:

=FILTER(A2:A16, MAP(A2:A16, LAMBDA(r, SUBTOTAL(103, r))))

Here, the range is A2:A16, and the condition is the MAP formula.

Finally, wrap this formula with UNIQUE to get unique values in the visible rows:

=UNIQUE(FILTER(A2:A16, MAP(A2:A16, LAMBDA(r, SUBTOTAL(103, r)))))

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.

Lookup Previous Values Dynamically in Excel and Google Sheets

Sometimes, you may want to look up the previous values relative to the current...

Sort Data but Keep Blank Rows in Excel and Google Sheets

Using a dynamic array formula or the FILTER command, we can sort data and...

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

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.