Note: this is an array formula and must be entered with Control + Shift + Enter, except in Excel 365. They key to the formula is knowing that the FREQUENCY function gathers numbers into “bins” in a particular way. Each bin represents an upper limit, and contains a count of all numbers in the data set that are less than or equal to the upper limit, and greater than the previous bin number. The trick then is to create the data_array argument using the condition you want to test for (order count greater than zero in this case), and the bins_array using the opposite condition. To create the data_array bin we use the following code: Here, we use the IF function to test the order count in each month to see if it’s greater than zero. If so, IF returns the column number using the COLUMN function. The result from IF is an array like this: Notice that only columns where order count > 0 make it into this array. Those columns where the count is zero become FALSE. The bins_array is generated with this snippet: Here the IF function is used again to test the order count in each column, but this time the logic is reversed. Only column numbers where the count is zero make it into the array returned by IF, which looks like this: Per standard FREQUENCY behavior, the numbers in the bins_array become the functional bins that tally non-zero orders. Months where orders are greater than zero are translated to FALSE and don’t collect any numbers from the data array, since FALSE values are ignored. The result is that the surviving bins count the number of consecutive non-zero orders up to that point, but excluding those previously counted. This all works because of the incrementing nature of rows and columns – you can be certain that the “next” number is always greater than the previous number. With the data array and bin arrays as shown above, FREQUENCY returns a count per bin in an array like this: The FREQUENCY function always returns an array with one more item than bins in the bins_array. This is by design, to catch any values greater than the largest value in the bins_array. This array is returned directly to the MAX function, with returns the largest number in the array:
Other consecutive values
To count consecutive occurrences of other values, just adjust the logic as needed following the same pattern: the first condition tests for the thing you want to count, the second condition tests for the opposite.
Dave Bruns
Hi - I’m Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.