In this example, the goal is to count the maximum number of consecutive monthly orders. That is, we want to count consecutive monthly orders greater than zero. This is a tricky formula to understand, so buckle up!
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.
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:
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.
This is a tricky formula to understand, and it requires that you have a numeric id for each match, and that all matches are sorted by id. They key is in understanding how FREQUENCY gathers numbers into "bins". Each bin represents an upper limit, and...
The Excel FREQUENCY function returns a frequency distribution, which is a summary table that shows the frequency of each value in a range. FREQUENCY returns multiple values and must be entered as an array formula with control-shift-enter, except...
The IF function runs a logical test and returns one value for a TRUE result, and another for a FALSE result. For example, to "pass" scores above 70: =IF(A1>70,"Pass","Fail"). More than one condition can be tested by nesting IF functions. The IF...
Formulas are the key to getting things done in Excel. In this accelerated training, you'll learn how to use formulas to manipulate text, work with dates and times, lookup values with VLOOKUP and INDEX & MATCH, count and sum with criteria, dynamically rank values, and create dynamic ranges. You'll also learn how to troubleshoot, trace errors, and fix problems. Instant access. See details here.