SumProduct operates on arrays of numbers. When you want to do a
multiconditional counting, say: count the records where A-range "X" and
B-range "Y"...
=SUMPRODUCT((A2:A5="X"),(B2:B5="Y"))
would leave SumProduct with arrays of logical values, not numbers. Something
like this:
=SUMPRODUCT(({TRUE,TRUE,FALSE,TRUE}),({FALSE,TRUE,TRUE,TRUE}))
Excel numerical equivalents for TRUE and FALSE are 1 and 0, respectively.
Since SumProduct does not convert these arrays into their numerical
equivalents itself, we need to effect the conversion explicitly. Here how it
can be done:
[1] Replace comma with * (put otherwise: switch from the native comma syntax
to the star syntax)...
=SUMPRODUCT((A2:A5="X")*(B2:B5="Y"))
This works because arithmetic operators (+,-,*,/, etc) coerces/converts
their operands's data type, in this case from logical to either 1 or 0.
Keep SumProduct's syntax and force coercion with arithmetic operations
applied to conditionals that evaluate to logical arrays...
[2a]
=SUMPRODUCT((A2:A5="X")+0,(B2:B5="Y")+0)
[2b]
=SUMPRODUCT((A2:A5="X")*1,(B2:B5="Y")*1)
[2c]
=SUMPRODUCT(--(A2:A5="X"),--(B2:B5="Y"))
The foregoing has the merit that they allow us to use the native comma
syntax, which apparently avoids the overhead that the multiplication invoked
by * between conditional expressions. It has also become clear that the
temporal profiles associated with *1, +0, and -- differ: The pick order in
speed is:
-- GreaterThan/EqualTo +0 (almost equal in speed)
+0 GreaterThan *1
That's why you see a gradual change to the comma syntax, accompanied with
the fastest operating coercer.
Is this the first recorded case of two wrongs making a right?
No. As Norman suggested, check out the threads involving the -- coercer,
which explain how it works.
Vaughan said:
OK, it works, but why does it?
I mean, if you want to use sumproduct to return a conditional sum, you put
the mysterious double negative before the first term and it works, you don't
put it and it doesn't.