In Excel, using double minus signs (--) converts "TRUE" to 1 and "FALSE" to 0.
Example : Let's take a simple logical condition : 3 > 2
.
Apply this in Excel = 3 > 2
returns "TRUE". Using "--" (two minus signs together) with this condition i.e. = --(3 > 2)
returns 1.
This method is commonly used in the SUMPRODUCT formula to convert conditional arrays which give TRUE/FALSE into 1s and 0s.
Suppose you have the table below and you are asked to calculate the sum of the products of values in the range B2:B8 with the values in the range A2:A8 that are less than 12.
A | B | |
---|---|---|
1 | X | Y |
2 | 20 | 2 |
3 | 10 | 5 |
4 | 5 | 8 |
5 | 15 | 11 |
6 | 7 | 14 |
7 | 8 | 17 |
8 | 13 | 20 |
Incorrect : =SUMPRODUCT((A2:A8<12),B2:B8)
returns 0.
Correct (with double minus sign) : =SUMPRODUCT(--(A2:A8<12),B2:B8)
returns 44.
- (A2:A8<12): This part checks if each value in the range A2:A8 is less than 12 and creates an array of TRUE/FALSE values based on the condition.
- The first minus sign converts the array of TRUE/FALSE values to
(0,-1,-1,0,-1,-1,0)
and the second minus sign multiplies each value by the negative sign. It results to(0,1,1,0,1,1,0)
. This is basic math: (-) * (-) = +. - SUMPRODUCT function multiplies elements of the two arrays : (0,1,1,0,1,1,0) , (2,5,8,11,14,17,20) and returns a value of 44.
Maths behind the formula:
- 0 * 2 = 0
- 1 * 5 = 5
- 1 * 8 = 8
- 0 * 11 = 0
- 1 * 14 = 14
- 1 * 17 = 17
- 0 * 20 = 0
Then the SUMPRODUCT function adds up these products:
0 + 5 + 8 + 0 + 14 + 17 + 0 = 44
Alternatively, you can use the multiply sign (*) in the SUMPRODUCT formula which is used to perform multiplication between arrays.
=SUMPRODUCT((A2:A8<12)*B2:B8)
Superb. Thanks for sharing your knowledge.
ReplyDeleteCan you please also add one of the most power full excel tool "Turning Data into Decisions" i.e. PivotTable, PowerPivot and PivotCharts?
Really good
ReplyDeleteSo in the first example we are wanting to add the values in column B where the value in column A is less than 12.
ReplyDeleteYour example looks confusing to me. Could you elaborate what you are this formula exactly is doing please? Formula With minus in first array and without in the second formula?
ReplyDelete