
Tips.Net > ExcelTips Home > Formulas > Counting Groupings Below a Threshold
Summary: When working with some data sets, you may need to know how many groups of values in the data set have elements that fall below a certain threshold. There are several ways you can determine this count in Excel, as described in this tip. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)
Ronald imports a number of signal-level measurements as a series of values into Excel. He needs to count how many consecutive groups of values exist in this series which fall below a certain threshold. For example, he may have the following measurements:
27, 22, 22, 30, 32, 18, 22, 23, 28, 39, 24, 27, 35, 25, 21
If he wants to know the number of groupings where the members of those groupings were under 26, the answer would be 4. Note that this is the groupings of consecutive values below 26, not the number of individual values below 26. Thus, in this case, the four groupings would be shown by the brackets in the following:
27, [22, 22], 30, 32, [18, 22, 23], 28, 39, [24], 27, 35, [25, 21]
Ronald is wondering what sort of formula he can use to figure out the number of groupings that fall below some arbitrary threshold he might specify.
There are actually several different ways you can approach this. The first is to use a "results column" that essentially notes changes in threshold and sequence grouping. For instance, if you had the above values in column A of a worksheet (starting at cell A2) and the threshold value in cell E1, then you could use the following formula in every cell to the right of a value in column A:
=IF(A2>=$E$1,B1,IF(A1<$E$1,B1,B1+1))
The formula keeps a running sum of the groups below the threshold. The max (or last value) of column B provides the total number of groups below the threshold. The formula checks to see whether the value immediately to the left, in column A, is above or below the threshold. If it's above, or if not and the previous value in column A was also below, the it doesn't increment the running sum. Otherwise, it does increment because a new grouping is starting.
A related way of doing the count is to use this formula in column B, instead:
=IF(A2>=$E$1,0,IF(A1<$E$1,0,1))
This results in column B containing a series of 0 or 1 values. The only time that a 1 value occurs is at the start of a series that is below the threshold. This makes it easy to sum all the values in column B, which provides the count of groupings.
If you don't want to use the results column, you can use an array formula to figure out the count. The following formula assumes, again, that the values to be analyzed are in column A, beginning at A2, and that the threshold value is in cell E1. Remember, as well, that array formulas are entered by pressing Ctrl+Shift+Enter.
=SUM(IF((A2:A16<$E$1)*((A2:A16<$E$1)*1<>((A1:A15<$E$1)*ISNUMBER(A1:A15))),1))
The formula basically does what the previous results-column formula did (determines a 0 or 1 based on whether a below-threshold grouping is starting) and then sums those values. You can find more about array values and how to use them in other WordTips or at the following Web pages:
http://www.emailoffice.com/excel/arrays-bobumlas.html http://www.cpearson.com/excel/array.htm
Of course, if you do these types of comparisons a lot, you may want to develop your own user-defined function (a macro) to figure the count of groupings for you. The following is an example of such a function.
Function CountGroups(ByVal MyRange As Range, Threshold As Single)
Dim Cell As Range
Dim bInGroup As Boolean
Dim iCount As Integer
Application.Volatile
iCount = 0
bInGroup = False
For Each Cell In MyRange
If Application.IsNumber(Cell) Then
If Cell < Threshold Then 'Less than the threshold?
If Not bInGroup Then 'Only count if starting new group
iCount = iCount + 1
bInGroup = True 'Mark as being in group
End If
Else
bInGroup = False 'No longer in a group
End If
End If
Next
CountGroups = iCount
End Function
The function looks through each cell in a range and calculates if it is the start of a new below-threshold group or not. You use the function by using a formula such as the following in your worksheet:
=CountGroups(A2:A16,E1)
Tip #3171 applies to Microsoft Excel versions: 97 2000 2002 2003 2007
PivotTables Got You Perplexed? ExcelTips: PivotTables for the Faint of Heart shows how you can start using Excel's PivotTable tool right away to spin your data into gold! You discover how easy it really is to crunch the numbers you need to crunch. Uncover the power of the PivotTable Wizard, how to edit PivotTables, how to format them, how to customize them, and much more.
Check out ExcelTips: PivotTables for the Faint of Heart today!
Thousands of ExcelTips, available for immediate download. Have all the Microsoft Excel info you need, right at your fingertips. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site