Features of !SEMTools
↑ Навигация по задачам в Excel и возможностям надстройки !SEMTools ↑

SUMPRODUCT

SUMPRODUCT function in Excel - simple usage example
Simplest example of using the SUMPRODUCT function
Function categoryMath
VolatilityNon-volatile
Similar functionsSUM, SUMIF, PRODUCT
Similar proceduresPivot tables

What does the SUMPRODUCT function do?

This function uses at least two ranges or arrays, multiplying and summing their elements.

Values in the first range are multiplied by corresponding values in the second range, and so on.

The sum of all such products is the calculation result.

Most often, the function is used as an alternative to the SUMIF function with multiple conditions.

SUMPRODUCT with conditions: AND, OR, count
F3=SUMPRODUCT((A2:A6="London")*(C2:C6>100)*C2:C6)
ABCDEF
1CityChannelSalesConditionResult
2LondonSearch150London330=SUMPRODUCT((A2:A6="London")*C2:C6)
3ParisDisplay90London and > 100260=SUMPRODUCT((A2:A6="London")*(C2:C6>100)*C2:C6)
4LondonDisplay70London OR Paris420=SUMPRODUCT(((A2:A6="London")+(A2:A6="Paris"))*C2:C6)
5RomeSearch60Rows for London3=SUMPRODUCT(--(A2:A6="London"))
6LondonSearch110

The comparison (A2:A6="London") gives an array of TRUE/FALSE; when multiplied, it becomes 1 and 0 and zeroes out the rows that don’t fit. Multiplying conditions is AND, adding is OR. The double minus turns TRUE/FALSE into 1/0 to simply count rows.

  1. The advantage of SUMPRODUCT over pivot tables is that in pivot tables, the filter for creating a slice is always an exact match (as in example 1). And "filter by label," if you output filtered values to rows, allows filtering by only one condition. SUMPRODUCT allows using all variants of string comparison (contains, starts with, ends with, matches) as well as numeric comparison (greater than, less than, equal to, not equal to, greater than or equal to, less than or equal to).
  2. The second advantage of SUMPRODUCT over pivot tables is that one filtering criterion in pivot tables cannot be used twice. SUMPRODUCT allows referencing the same column multiple times.
  3. The advantage of SUMPRODUCT over the SUMIFS function is that the conditional logic of SUMIFS is strictly "AND" for all conditions. The ability to use any logical operations on arrays of boolean values provides complete freedom to combine conditions in any logical combinations - AND, OR, IF, NOT.

SUMPRODUCT: result and input format

Ranges must be unidirectional, contain the same number of elements, and have the same vertical or horizontal dimensions. Specific range addresses don't matter.

Range sizes and text
F3=SUMPRODUCT(A2:A4,B2:B3)
ABCDEF
1ABTextFormulaResult
225pcssame size26=SUMPRODUCT(A2:A4,B2:B4)
3443different size#VALUE!=SUMPRODUCT(A2:A4,B2:B3)
410text counts as zero12=SUMPRODUCT(A2:A4,C2:C4)

Ranges must be the same size, otherwise #VALUE!. Text and blanks multiplied via commas count as zeros: 2×0 + 4×3 + 1×0 = 12.

Ranges can have multiple columns and rows simultaneously. Typically, vertical ranges with a width of 1 column are used.

SUMPRODUCT function syntax

=SUMPRODUCT(range_or_array1, range_or_array2, …)
Sum of products
E2=SUMPRODUCT(B2:B5,C2:C5)
ABCDE
1ProductQtyPriceRevenue
2Armchair34503120=SUMPRODUCT(B2:B5,C2:C5)
3Table26203120=SUM(B2:B5*C2:C5)
4Chair1045
5Lamp180

3×450 + 2×620 + 10×45 + 1×80 = 3,120. SUM over multiplied arrays gives the same, but in Excel 2019 and earlier it needs Ctrl+Shift+Enter, and SUMPRODUCT doesn’t.

Weighted average and case-sensitive count

SUMPRODUCT is the standard way to calculate a weighted average: the sum of values times weights divided by the sum of weights. Combined with EXACT, it counts cells case-sensitively, which COUNTIF can’t do.

Weighted price and exact match
E2=SUMPRODUCT(B2:B4,C2:C4)/SUM(C2:C4)
ABCDEFGH
1BatchPriceQtyWeighted priceCode“ABC” exactly
2110010112.5=SUMPRODUCT(B2:B4,C2:C4)/SUM(C2:C4)ABC2=SUMPRODUCT(--EXACT(G2:G5,"ABC"))
321205abc
431305ABC
5Abc

(100×10 + 120×5 + 130×5) / 20 = 112.5 — a plain AVERAGE would give 116.67. EXACT compares case-sensitively, so 2 of the 4 codes are “ABC”.

SUMPRODUCT formula example

Let's examine a complex example with 4 conditions in detail.

The marketer's task is to calculate the sum based on four conditions:

  1. Week start date - April 13 or later
  2. Campaign name contains the word "Brand"
  3. Campaign ends with "BY" (Belarus)
  4. Or ends with "UA" (Ukraine)

Rows meeting all four conditions are highlighted in green. How to create a formula that would account for only them?

The formula can be seen in the screenshot in cell H9.

Complex example with SUMPRODUCT function in Excel

Essentially, it's the sum of the product of two ranges - the array of numbers D3:D34 and an array of zeros and ones. Let's examine it closer:

Explanation of compound formula logic with SUMPRODUCT

The array of zeros and ones is created by multiplying three array formulas, the third of which is obtained by adding two other array formulas and applying the SIGN function.

Each row shows one of the four conditions on which these formulas are based:

(VALUE(LEFT($A$3:$A$34, 8)) >= $H$2)
Condition 1: week start ≥ 04/13
D2=VALUE(LEFT(A2,8))>=$G$2
ABCDEFG
1WeekCampaignClicksCheckParameterValue
204/06/20 - 04/12/20Brand_Search_BY120FALSE=VALUE(LEFT(A2,8))>=$G$2From date04/13/20
304/13/20 - 04/19/20Brand_Search_BY150TRUE=VALUE(LEFT(A3,8))>=$G$2WordBrand
404/13/20 - 04/19/20Generic_Search_UA90TRUE=VALUE(LEFT(A4,8))>=$G$2End 1BY
504/20/20 - 04/26/20Brand_Display_UA70TRUE=VALUE(LEFT(A5,8))>=$G$2End 2UA
604/20/20 - 04/26/20Brand_Search_US60TRUE=VALUE(LEFT(A6,8))>=$G$2
704/27/20 - 05/03/20brand_search_ua110TRUE=VALUE(LEFT(A7,8))>=$G$2

LEFT takes “04/13/20”, VALUE turns the text into a date (with US date settings), and then it can be compared with G2.

This expression creates an array of boolean values TRUE and FALSE. The LEFT($A$3:$A$34,8) function extracts the first 8 characters from the date range in column A, and the VALUE function converts them to numeric format; otherwise, comparison with cell H2 won't work correctly because the LEFT function is a text function and returns a string.

NOT(ISERROR(SEARCH($H$4, $B$3:$B$34)))
Condition 2: the name contains “Brand”
D4=NOT(ISERROR(SEARCH($G$3,B4)))
ABCDEFG
1WeekCampaignClicksCheckParameterValue
204/06/20 - 04/12/20Brand_Search_BY120TRUE=NOT(ISERROR(SEARCH($G$3,B2)))From date04/13/20
304/13/20 - 04/19/20Brand_Search_BY150TRUE=NOT(ISERROR(SEARCH($G$3,B3)))WordBrand
404/13/20 - 04/19/20Generic_Search_UA90FALSE=NOT(ISERROR(SEARCH($G$3,B4)))End 1BY
504/20/20 - 04/26/20Brand_Display_UA70TRUE=NOT(ISERROR(SEARCH($G$3,B5)))End 2UA
604/20/20 - 04/26/20Brand_Search_US60TRUE=NOT(ISERROR(SEARCH($G$3,B6)))
704/27/20 - 05/03/20brand_search_ua110TRUE=NOT(ISERROR(SEARCH($G$3,B7)))

SEARCH ignores case, so “brand_search_ua” matches too. A shorter version: ISNUMBER(SEARCH($G$3,B2)).

This expression also creates an array of boolean values. The SEARCH function searches for the word "BRAND" inside each cell of column B; if not found, it returns an error, and if found, it returns the position of its first character in the string. With the ISERROR function we turn errors into ones and their absence into zeros. And then with the NOT function we get the array we need.

There's no "Ends with" function in Excel, but you can do a simple check - if the last characters in the string match the word, isn't that an occurrence? This operation is performed by the expression:

(RIGHT($B$3:$B$34, LEN($J$2)) = $J$2)

Here we're helped by the LEN and RIGHT functions. By checking whether the last 2 characters match cell J2, we also return an array of boolean values.

The fourth condition is similar to the third, as is its expression:

(RIGHT($B$3:$B$34, LEN($J$4)) = $J$4)
Conditions 3–4: ends with BY or UA
D6=SIGN((RIGHT(B6,LEN($G$4))=$G$4)+(RIGHT(B6,LEN($G$5))=$G$5))=1
ABCDEFG
1WeekCampaignClicksCheckParameterValue
204/06/20 - 04/12/20Brand_Search_BY120TRUE=SIGN((RIGHT(B2,LEN($G$4))=$G$4)+(RIGHT(B2,LEN($G$5))=$G$5))=1From date04/13/20
304/13/20 - 04/19/20Brand_Search_BY150TRUE=SIGN((RIGHT(B3,LEN($G$4))=$G$4)+(RIGHT(B3,LEN($G$5))=$G$5))=1WordBrand
404/13/20 - 04/19/20Generic_Search_UA90TRUE=SIGN((RIGHT(B4,LEN($G$4))=$G$4)+(RIGHT(B4,LEN($G$5))=$G$5))=1End 1BY
504/20/20 - 04/26/20Brand_Display_UA70TRUE=SIGN((RIGHT(B5,LEN($G$4))=$G$4)+(RIGHT(B5,LEN($G$5))=$G$5))=1End 2UA
604/20/20 - 04/26/20Brand_Search_US60FALSE=SIGN((RIGHT(B6,LEN($G$4))=$G$4)+(RIGHT(B6,LEN($G$5))=$G$5))=1
704/27/20 - 05/03/20brand_search_ua110TRUE=SIGN((RIGHT(B7,LEN($G$4))=$G$4)+(RIGHT(B7,LEN($G$5))=$G$5))=1

Adding the two checks is OR; SIGN turns 2 into 1. The “=” comparison also ignores case, so “ua” = “UA”.

The OR conditional logic is reproduced by adding two boolean arrays. In doing so, they become numbers. TRUE+FALSE (and vice versa) gives 1, FALSE+FALSE gives 0, TRUE+TRUE gives 2. To avoid distorting the data, we use the SIGN function - it returns 1 for positive numbers and 0 for 0. Thus, we get an array of zeros and ones based on two boolean arrays.

Multiplying the three arrays gives us a single array of zeros and ones. Multiplying this by the values of impressions, clicks, and downloads, we get the sums of values filtered this way. Numbers where conditions aren't met are "destroyed" by zeros, and numbers where conditions are met are multiplied by 1 and summed.

An observant reader might wonder why we don't use the OR function to combine the last two boolean arrays. The answer: unfortunately, it doesn't combine arrays pairwise (just like the AND function), but considers each cell of the array as an equal element.

This is why arithmetic functions are used.

  • Instead of AND - multiplication
  • And instead of OR - addition + SIGN function.
The final formula: sum by 4 conditions
H2=SUMPRODUCT(C2:C7*(VALUE(LEFT(A2:A7,8))>=G2)*NOT(ISERROR(SEARCH(G3,B2:B7)))*SIGN((RIGHT(B2:B7,LEN(G4))=G4)+(RIGHT(B2:B7,LEN(G5))=G5)))
ABCDEFGH
1WeekCampaignClicksAll conditionsParameterValueTotal clicks
204/06/20 - 04/12/20Brand_Search_BY1200=(VALUE(LEFT(A2,8))>=$G$2)*NOT(ISERROR(SEARCH($G$3,B2)))*SIGN((RIGHT(B2,LEN($G$4))=$G$4)+(RIGHT(B2,LEN($G$5))=$G$5))From date04/13/20330=SUMPRODUCT(C2:C7*(VALUE(LEFT(A2:A7,8))>=G2)*NOT(ISERROR(SEARCH(G3,B2:B7)))*SIGN((RIGHT(B2:B7,LEN(G4))=G4)+(RIGHT(B2:B7,LEN(G5))=G5)))
304/13/20 - 04/19/20Brand_Search_BY1501=(VALUE(LEFT(A3,8))>=$G$2)*NOT(ISERROR(SEARCH($G$3,B3)))*SIGN((RIGHT(B3,LEN($G$4))=$G$4)+(RIGHT(B3,LEN($G$5))=$G$5))WordBrand
404/13/20 - 04/19/20Generic_Search_UA900=(VALUE(LEFT(A4,8))>=$G$2)*NOT(ISERROR(SEARCH($G$3,B4)))*SIGN((RIGHT(B4,LEN($G$4))=$G$4)+(RIGHT(B4,LEN($G$5))=$G$5))End 1BY
504/20/20 - 04/26/20Brand_Display_UA701=(VALUE(LEFT(A5,8))>=$G$2)*NOT(ISERROR(SEARCH($G$3,B5)))*SIGN((RIGHT(B5,LEN($G$4))=$G$4)+(RIGHT(B5,LEN($G$5))=$G$5))End 2UA
604/20/20 - 04/26/20Brand_Search_US600=(VALUE(LEFT(A6,8))>=$G$2)*NOT(ISERROR(SEARCH($G$3,B6)))*SIGN((RIGHT(B6,LEN($G$4))=$G$4)+(RIGHT(B6,LEN($G$5))=$G$5))
704/27/20 - 05/03/20brand_search_ua1101=(VALUE(LEFT(A7,8))>=$G$2)*NOT(ISERROR(SEARCH($G$3,B7)))*SIGN((RIGHT(B7,LEN($G$4))=$G$4)+(RIGHT(B7,LEN($G$5))=$G$5))

Column D is the product of the three conditions: 1 only where all of them are met. SUMPRODUCT multiplies the clicks by these ones and zeros and adds them up: 150 + 70 + 110 = 330. Column D isn’t needed — it’s shown for clarity.


Other examples with SUMPRODUCT:

Find duplicate values in Excel

Like the article? Help its author! Buy !SEMTools, it has lots of useful instruments to process text data.