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.

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

Filter Top N per Category in Excel (Using FILTER & COUNTIFS)

You can use a combination of FILTER and COUNTIFS to filter the top N...

Rank per Group in Excel

You have two groups of 20 students each. How do you determine the rank...

More like this

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

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.