OzGrid

Excel: Summing/Counting Based on Multiple Criteria

< Back to Search results

 Category: [Excel]  Demo Available 

Excel: Summing/Counting Based on Multiple Criteria

Got any Excel Questions? Free Excel Help

Conditional Summing / Counting
 
A Forum Member needed to count based on conditions as follows

Column A Column B
01 0.5
01 0.7
01 1.0
02 0.3
02 2.0

The result needed was as follows: 

If Column = 01 then count how many cells in Column B are less than 1
 
XL-Dennis provided a solution using SUMPRODUCT
 
=SUMPRODUCT((A1:A5="01")*(B1:B5<1)) 

Another poster required further explanation of how SUMPRODUCT worked, and this was explained in great detail by Chris Davison.

Let’s look at the first part of the SUMPRODUCT solution: (A1:A5="01")

(A1:A5="01") is a Boolean statement – i.e. the statement is either TRUE or FALSE for each value – in the above example… 

TRUE 
TRUE 
TRUE 
FALSE 
FALSE 

or, since TRUE=1 and FALSE=0 : 

1 
1 
1 
0 
0 

looking at the second part : 

(B1:B5<1)

Another Boolean test - the statement is either TRUE or FALSE: 

TRUE 
TRUE 
FALSE 
TRUE 
FALSE 

and again, since TRUE=1 and FALSE=0 : 

1 
1 
0 
1 
0 

since there are two conditions, SUMPRODUCT multiplies the products of both criteria in this array… 

1x1 
1x1 
1x0 
0x1 
0x0 

equals 

1 
1 
0 
0 
0 

The PRODUCT of the results : 

1+1+0+0+0 = 2 

So the net result is 2 (i.e. 2 conditions conform to the criteria we specified)

The formula can be used in many ways and once you discover SUMPRODUCT, it seems like a gift from heaven – a word of caution though… 

Imagine having 10 criteria (i.e. names that begin with "B", in the "North", where the value is less than $5000, with a date in January, for salesman "Bloggs", for company "ABC Corp" and you're interrogating 5,000 records....

SUMPRODUCT will interrogate each record (all 5,000 of them) ten separate times, to ascertain whether each criteria is in fact TRUE or FALSE.... then when it's got all these, it multiplies them all together 5,000 x 10 calculations, then adds up the results.... with such a query, I wouldn't be surprised if it took a minute or so to calculate, so while it's useful, if you begin to notice performance degradations on your worksheets as you use them more often, you may be better off looking at the range of Database formulae..( DSUM, DGET, DCOUNTA etc) which basically do the same thing but are much easier to set up and maintain, and, from experience, are a lot quicker.

 

See also: Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions and Index to new resources and reference sheets

 

See also:

Convert Excel Dates/Time to True Dates & Times
Copy, Cut & Paste Special in Excel
Count Between Date Ranges
Excel Count If With Multiple Criteria
Count Lines in a Cell
Count of Each Item in a List
Excel VBA Macro - Count Or Sum By Fill Color

 

Microsoft ® and Microsoft Excel ® are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft.

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


Gallery



stars (0 Reviews)