Count Unique Values in Visible Rows in Google Sheets

Published on

We should use the COUNTUNIQUEIFS function, not COUNTUNIQUE, to count unique values in visible rows only in Google Sheets.

When we filter data to focus on specific records, most functions include hidden rows in their calculations. However, there is a technique to exclude hidden rows.

You should replace your ‘intended’ function with an alternative function that supports criteria usage.

Because we need to include a virtual helper column that contains 1 (visible) and 0 (hidden) to identify hidden rows, we should be able to specify helper_column>=1 as a criterion.

That’s why we use COUNTUNIQUEIFS instead of COUNTUNIQUE.

First, let’s see how COUNTUNIQUE behaves with filtered data.

My sample data in D1:D10 consists of 6 unique values, excluding the header, before filtering.

=COUNTUNIQUE(D2:D10)
COUNTUNIQUE in Filtered Data

Now, let’s apply a filter to this range and see what happens.

COUNTUNIQUE issue in counting unique values in visible rows

The formula still returns 6, even though I have filtered out a few rows. Below, you can see the solution to count unique values in visible rows in Google Sheets.

Counting Unique Values in Visible Rows

We will start with data in a single column, and then proceed to more than one column.

Syntax of the COUNTUNIQUEIFS Function in Google Sheets:

COUNTUNIQUEIFS(range, criteria_range1, criterion1, [criteria_range2, …], [criterion2, …])

Assume D2:D is the range where you want to count unique values in visible rows. You can use the following formula, where D2:D is provided as an example that you should replace with your actual range.

=COUNTUNIQUEIFS(D2:D, MAP(D2:D, LAMBDA(r, SUBTOTAL(103, r))), 1)

Where:

  • range: D2:D
  • criteria_range: MAP(D2:D, LAMBDA(r, SUBTOTAL(103, r))), which returns 1 for visible rows and 0 for hidden rows
  • criterion: 1

Note: You can repeat the criteria_range and criterion for additional filtering conditions.

Explanation of the criteria_range

We can use SUBTOTAL(103, D2) to check if the value in cell D2 is counted. The function number 103 in SUBTOTAL represents COUNTA. When you hide row #2, D2 becomes invisible. Thus, the output of the SUBTOTAL formula will be 0.

We can use the LAMBDA function to create a custom function that checks visibility: LAMBDA(r, SUBTOTAL(103, r)). Here, ‘r’ represents the current element being processed by the function.

LAMBDA(r, SUBTOTAL(103, r))

To apply this to the range D2:D, we use the MAP lambda helper function.

Syntax:

MAP(array1, [array2, …], lambda)

By specifying the array D2:D, MAP will map each value within the custom function and return an array output.

Counting Unique Values in Visible Rows in Multiple Columns

Sometimes you may want to count unique values based on two or more columns.

For example, you might want to get the count of unique employees in visible rows when the employees’ first and last names are in two columns.

Here, the COUNTUNIQUEIFS function won’t work because combining first and last names as a criteria range will cause an “Argument must be a range” error.

Instead, we will extract visible rows using the FILTER function and apply UNIQUE to it. This will return the unique records. Then, we count the values in one of the columns using COUNTA.

In the following example, the first names are in column D and the last names are in column E. Here is the formula to count unique names in visible rows:

=COUNTA(
   CHOOSECOLS(
      UNIQUE(
         FILTER(D2:E, MAP(D2:D, LAMBDA(r, SUBTOTAL(103, r)))=1)
       ), 1
   )
)
Count Unique Values in Visible Rows in Two Columns

Formula Explanation

  • FILTER(D2:E, MAP(D2:D, LAMBDA(r, SUBTOTAL(103, r)))=1): Filters the range D2:E where the virtual helper column returns 1 (indicating visible rows).
  • UNIQUE(…): Returns the unique rows from the above filter formula result.
  • CHOOSECOLS(…, 1): Returns the first column from the unique rows.
  • COUNTA(…): Returns the count of the unique values.

In this formula, you can replace D2:E with your actual table range.

That’s all about counting unique rows in a multiple-column range in Google Sheets.

Resources

Here are some Google Sheets tutorials that help handle hidden rows in calculations:

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 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...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

Time Sequences in Excel by Minute, Hour, or Second Increments

Creating time sequences, whether by hour, minute, or second increments in Excel, can be...

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...

2 COMMENTS

  1. This is perfect for what I needed, but can you explain to me the formula for counting VISIBLE cells in a column that have a specific text? The current formula I am using is =COUNTIF(O8:O, "Text"), but this does not adjust when I filter out data. I appreciate any help I can get! Thank you!

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.