How to Count Unique Values in a Filtered Range in Excel

📅 Aug 06, 2026 📝 Sarah Miller

Filtering a dataset in Excel only to realize your summary formulas still count hidden rows is a common, frustrating hurdle. Standard functions like COUNTIF or basic UNIQUE fail to recognize active filters, leaving you with inaccurate reporting metrics. Fortunately, combining advanced functions grants you seamless, real-time data precision that dynamically adapts to user inputs. Note this key stipulation: this technique requires modern Excel versions (like Excel 365) to process complex array calculations. Utilizing a SUMPRODUCT, SUBTOTAL, and OFFSET combination provides the robust solution you need. Below, we detail how to implement this formula step-by-step.

How to Count Unique Values in a Filtered Range in Excel

Excel Formula to Count Distinct Values in a Filtered Range

Data analysis in Excel frequently requires us to find the number of unique or distinct entries within a dataset. While functions like UNIQUE and COUNTA make this straightforward under normal circumstances, the task becomes significantly more challenging when you apply a filter.

Standard count functions do not distinguish between visible and hidden rows. When you filter a table, a simple count formula will continue to calculate values in the hidden rows, leading to inaccurate metrics. To count distinct values in a filtered range, we must combine visibility-detecting functions like SUBTOTAL with array-processing functions.

This comprehensive guide explores the best formulas to achieve this across different versions of Excel, ranging from modern dynamic array formulas to classic compatibility solutions and high-performance helper columns.

The Core Challenge: Why Standard Formulas Fail

To understand why counting distinct visible values is tricky, we need to look at how Excel handles filtered rows. When you apply a filter, Excel does not delete the data; it merely sets the row height of the excluded rows to zero, hiding them from view.

  • COUNTIF and COUNTIFS: These functions scan the entire specified range, completely ignoring whether a row is visible or hidden.
  • COUNTA: This function counts all non-empty cells in a range, regardless of their visibility state.
  • SUBTOTAL and AGGREGATE: These are the only built-in formulas capable of ignoring hidden rows (using function codes 101–111). However, they lack native "distinct" or "unique" counting capabilities.

To solve this, we must build a formula that evaluates both criteria simultaneously: Is the row visible? and Is the value unique within the visible subset?

Method 1: The Modern Excel 365 Solution (Best & Most Robust)

If you are using Excel 365 or Excel 2021, you have access to dynamic arrays. By combining LET, MAP, LAMBDA, FILTER, and UNIQUE, we can create an elegant, non-volatile formula that scales beautifully with large datasets.

The Formula

=LET(
    range, A2:A20,
    visible, MAP(range, LAMBDA(row, SUBTOTAL(103, row))),
    clean_list, FILTER(range, (visible=1) * (range<>""), ""),
    IF(INDEX(clean_list, 1)="", 0, ROWS(UNIQUE(clean_list)))
)

How It Works Step-by-Step

  1. LET(range, A2:A20, ...): We define our target data range as a variable named range. This makes the formula easier to read and maintain.
  2. MAP(range, LAMBDA(row, SUBTOTAL(103, row))): Since SUBTOTAL normally expects a range and returns a single value, we cannot pass it an array directly to get row-by-row visibility. The MAP function solves this by forcing Excel to evaluate SUBTOTAL(103, ...) on each individual row one by one. The function code 103 behaves like COUNTA but ignores filtered-out rows. This step generates an array of 1s (for visible, non-empty cells) and 0s (for hidden or empty cells).
  3. FILTER(range, (visible=1) * (range<>""), ""): This filters our original data range, keeping only the items where the corresponding visibility array equals 1 and the cell is not blank (range<>"").
  4. IF(INDEX(clean_list, 1)="", 0, ROWS(UNIQUE(clean_list))): The UNIQUE function extracts distinct values from our filtered list. We wrap this in ROWS to count how many distinct items exist. The IF statement acts as an error-handling safety net: if no visible records match our criteria, it returns 0 instead of a #CALC! error.

Method 2: The Classic SUMPRODUCT & SUBTOTAL Formula (Pre-2021 Excel)

If you are working on older versions of Excel (such as Excel 2016 or 2019) that do not support dynamic array functions like MAP or UNIQUE, you must rely on a classic array calculation powered by SUMPRODUCT, SUBTOTAL, and OFFSET.

The Formula

=SUMPRODUCT((SUBTOTAL(3, OFFSET(A2, ROW(A2:A20)-MIN(ROW(A2:A20)), 0, 1))) / COUNTIF(A2:A20, A2:A20 & ""))

Note: Since this is an array formula in older Excel versions, you may need to commit it by pressing Ctrl + Shift + Enter instead of just Enter.

How It Works Step-by-Step

  1. ROW(A2:A20)-MIN(ROW(A2:A20)): This creates a sequential array of relative offsets starting from 0 (e.g., {0; 1; 2; 3; ...}).
  2. OFFSET(A2, offset_array, 0, 1): This is a clever trick to bypass SUBTOTAL's limitation. It breaks down the continuous range A2:A20 into an array of individual, single-cell references.
  3. SUBTOTAL(3, OFFSET(...)): Evaluates each single-cell reference individually. Function code 3 (COUNTA) returns a 1 if the cell is visible and populated, and 0 if it is hidden. This generates an array of visibilities like {1; 0; 1; 1; 0...}.
  4. COUNTIF(A2:A20, A2:A20 & ""): This calculates the total frequency of each value within the entire range. Concatenating an empty string (& "") prevents division-by-zero errors if there are empty cells in your range.
  5. The Division: We divide the visibility array by the frequency array. For example, if the value "Apples" appears 3 times in the dataset and all 3 are visible, each occurrence evaluates to 1 / 3. When SUMPRODUCT sums these up (1/3 + 1/3 + 1/3), the result is exactly 1. If only 2 occurrences of "Apples" are visible, the calculation becomes 1/3 + 1/3 = 0.67. While this classic approach is highly creative, it is important to note that it can yield minor inaccuracies if duplicates are partially filtered out.

Warning on Performance: The OFFSET function is volatile. This means Excel will recalculate this formula every time you make a change to any cell in your workbook. On large datasets (thousands of rows), this can severely slow down your spreadsheet's calculation speeds.

Method 3: The Helper Column & Pivot Table Approach (Fastest Performance)

If you have a massive dataset or want a clean, simple formula that doesn't drag down your computer's performance, the helper column method is the industry best practice.

Step 1: Create the Visibility Helper Column

Insert a new column next to your data (let's assume Column B) and name it "Is Visible". In cell B2, enter the following formula and copy it down your column:

=SUBTOTAL(103, A2)

When you filter your data, the visible rows will display a 1 in this helper column, while hidden rows will evaluate to 0.

Step 2: Apply the Count Distinct Formula

With your helper column established, counting distinct values is simple and computationally lightweight. You can use standard Excel 365 functions without complex lambda expressions:

=ROWS(UNIQUE(FILTER(A2:A20, B2:B20=1)))

Alternative: Using a Pivot Table with the Data Model

If you prefer a no-formula solution, you can leverage Excel's built-in Data Model engine to calculate distinct counts on filtered data:

  1. Select your data range (including the helper column).
  2. Go to the Insert tab on the ribbon and click PivotTable.
  3. In the dialog box, make sure to check the box at the bottom: "Add this data to the Data Model". Click OK.
  4. In the PivotTable Fields pane, drag your main data field to the Rows area.
  5. Drag your "Is Visible" helper column to the Filters area and set it to 1.
  6. To get a single distinct summary count, drag your main data field to the Values area. Click the dropdown on the value field, select Value Field Settings, scroll to the bottom of the list, and select Distinct Count.

Performance and Compatibility Comparison

Method Excel Version Compatibility Calculation Speed / Impact Handling of Blank Cells
Method 1 (LET + MAP) Excel 365, Excel 2021+ Fast / Native Array Performance Excellent (Filters out automatically)
Method 2 (SUMPRODUCT + OFFSET) All Versions (Excel 2007+) Slow (Volatile due to OFFSET) Moderate (Requires & "" trick)
Method 3 (Helper Column) All Versions Extremely Fast (Optimal for large sheets) Excellent

Summary and Best Recommendations

When choosing the right formula to count distinct values in a filtered range, prioritize your Excel version and dataset size:

  • Use Method 1 (Excel 365 LET/MAP) for modern worksheets. It keeps your workbook clean, avoids performance-heavy volatile functions, and handles empty rows gracefully without helper columns.
  • Use Method 3 (Helper Column) if your sheet contains tens of thousands of rows. This keeps calculations instantaneous and avoids the calculation lag associated with older array structures.
  • Avoid Method 2 unless you are forced to work in legacy environments with strict restrictions against adding helper columns.

Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.