How to Get the Cumulative Count of Distinct Values in Google Sheets

Knowing how to calculate the cumulative count of distinct values can be helpful in many ways. For example, you can use it to track how many unique customers have made a purchase over time or count how many different products have been sold cumulatively.

In Google Sheets, you can use the SCAN function to get the cumulative count of distinct values.

Generic Formula

=ArrayFormula(IF(range="",,SCAN(0, range, LAMBDA(acc, values, IF(COUNTIF(A2:values, values)=1, acc+1, acc)))))

When using this formula, replace range with the column range that you want to analyze for the running count of distinct values.

As a side note, the last value in the cumulative count of distinct values will be equal to the total count of unique values in the range.

Example: Cumulative Count of Distinct Values in a Column

Assume you have the following names in A2:A in Google Sheets:

Jeri
Valerie
Jeri
Charlie
Bob
Charlie
Stacey
Stacey

In cell B2, enter the following formula to get the cumulative count of distinct names:

=ArrayFormula(IF(A2:A="",,SCAN(0, A2:A, LAMBDA(acc, values, IF(COUNTIF(A2:values, values)=1, acc+1, acc)))))

Result:

Example of a running count of distinct names in a column in Google Sheets

Formula Explanation

  • COUNTIF(A2:values, values) – Returns the running count of occurrences of the current row’s value. This is equivalent to entering =COUNTIF($A$2:A2, A2) in B2 and dragging it down.
  • In this COUNTIF, values represents the current value/reference.
  • IF(COUNTIF(A2:values, values) = 1, acc + 1, acc) – If the count of values is 1, the formula adds 1 to the accumulator (acc); otherwise, it retains the previous count.
  • The SCAN function iterates this logic for each value in the array, returning the running count of distinct values in Google Sheets.
  • The IF statement at the beginning ensures that empty rows do not display any result.

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.

Top N Products and Top N Sellers in Google Sheets (Top N of Top N)

If you’ve ever wanted to compare which sellers contribute the most to your top-selling...

Use XLOOKUP in a Structured Table in Google Sheets (Single and Multiple Conditions)

This tutorial is for users who want to apply XLOOKUP inside a structured table...

Reset SCAN by Another Column in Google Sheets and Excel

Resetting SCAN function results based on values in another column is a topic of...

How to Get the Fastest Time for Each Person in Google Sheets

Whether you’re tracking race results, event times, or any other timed activities, finding the...

More like this

Top N Products and Top N Sellers in Google Sheets (Top N of Top N)

If you’ve ever wanted to compare which sellers contribute the most to your top-selling...

Use XLOOKUP in a Structured Table in Google Sheets (Single and Multiple Conditions)

This tutorial is for users who want to apply XLOOKUP inside a structured table...

How to Get the Fastest Time for Each Person in Google Sheets

Whether you’re tracking race results, event times, or any other timed activities, finding the...

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.