HomeExcel FormulaRunning Count of Occurrences in Excel (Includes Dynamic Array)

Running Count of Occurrences in Excel (Includes Dynamic Array)

Published on

To calculate the running count of values in a list, we can use COUNTIF with or without dynamic array functionality in Excel.

In a sorted or unsorted list, the first occurrence of a value is numbered 1, the second occurrence is numbered 2, and so on. This applies to all the values in the list.

The benefits of using a running count in Excel include:

  • Trend and Pattern Identification: Running counts help identify trends and patterns by showing how often specific values occur in a data.
  • Conditional Analysis: Running counts can be used in combination with other Excel functions such as FILTER and SUMIFS, enabling conditional analysis based on specific criteria.
  • Versatility in Applications: Running counts can be applied in various scenarios, from inventory management to sales tracking, offering flexibility in data analysis.

Running Count of Occurrences Using COUNTIF in Excel

Let’s consider t-shirt colors red, blue, and green in the range C3:C12. These colors occur multiple times in the list.

Insert the following Excel COUNTIF formula in cell D3 and drag it down until D12:

=COUNTIF($C$3:C3, C3)
Running Count of Occurrences in Excel using Drag-Down COUNTIF Formula

This will return the running count of occurrences of values, such as t-shirt colors, in the list.

How does this formula work?

When we drag the formula down, the range, i.e., $C$3:C3, becomes $C$3:C4, $C$3:C5, $C$3:C6, and so on. Similarly, the criterion becomes C3, C4, C5, C6, and so on.

So, the formula returns the count of occurrences of the criterion up to the current row, as the range is not the whole range but up to the current row only.

Running Count of Occurrences Using COUNTIF with Dynamic Arrays in Excel

In Excel for Microsoft 365 (or supported Excel versions), we can use the MAP function with COUNTIF to return the running count of occurrences of values. This will be a dynamic array formula that spills down from the formula cell.

In cell D3, enter the following formula:

=MAP(C3:C12, LAMBDA(row, COUNTIF(C3:row, row)))

Where ‘row’ represents the current element in the array C3:C12. In D3, C3:row will be C3:C3, in D4, C3:row will be C3:C4, and so forth.

This formula follows a similar non-array (drag-down) formula logic.

The MAP function in Excel iterates over each element (defined by the name ‘row’) in the array C3:C12 and applies a custom function, i.e., LAMBDA(row, COUNTIF(C3:row, row)).

It calculates the count of occurrences of the value in that row within the range from C3 to the current row (row). Essentially, it’s a dynamic COUNTIF formula that updates based on the current row.

Limiting the Running Count Formula Output to a Specific Value in a List

In certain cases, you may only want the running count of occurrences of a particular value in Excel. In the above Excel formula example, let’s consider the color “red.”

The solution in that case is to employ a logical test, something like:

If the running count of the current row value is “red,” return the running count of occurrences of values; otherwise, return blank.

For such tests, we can use the IF function in Excel. If you prefer the drag-down formula:

=IF(C3="red", COUNTIFS($C$3:C3, C3), "")

If you prefer the dynamic array formula:

=MAP(C3:C12, LAMBDA(row, IF(row="red", COUNTIF(C3:row,row), "")))
Running Count of Occurrences of Specific Values in Excel (Dynamic Array Using MAP)

Additional Tip

For the running count of occurrences in Excel, we can also use COUNTIFS. In the above formula, whether it’s a drag-down or dynamic array, feel free to replace COUNTIF with COUNTIFS.

COUNTIFS supports multiple criteria. However, since we only want to specify one criterion (the current row value), we don’t require this function.

That’s why I have opted for COUNTIF, though both functions work.

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.

Excel: Month Name to Number & Number to Name

This tutorial showcases the most efficient formulas for converting a month name to a...

Get the First or Last Row/Column in a New Google Sheets Table

If you've recently started using the new Google Sheets TABLE functionality, you may find...

UNIQUE Function in Visible Rows in Google Sheets

The UNIQUE function doesn't inherently include only visible rows when it returns values, discarding...

Customizing Alternating Colors of a Table in Google Sheets

Google Sheets' new table feature offers limited options for customizing colors. While you can...

More like this

Excel: Month Name to Number & Number to Name

This tutorial showcases the most efficient formulas for converting a month name to a...

EXPAND + Stacking: Expand an Array in Excel

We can expand an array by adding values at the end using the EXPAND...

Creating Custom Descending Sequence Lists in Excel

You can utilize functions like ROW or SEQUENCE in Excel to create a custom...

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.