Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spreadsheet function to sum all values in one column based on criteria from another column

I want a spreadsheet function that will produce a sum of all values in column B for when column A is equal to X and when it is equal to Y.

A     B

X    10
Y     3
X     7
X    22
Y     4
Y     9

The output should look like the following (where 39 and 16 are the results of the formulas):

X    39           -> 10 + 7 + 22
Y    16           -> 3 + 4 + 9
like image 373
Jon Erickson Avatar asked Mar 31 '09 22:03

Jon Erickson


People also ask

How do you sum one column based on the criteria in another column?

If you want, you can apply the criteria to one range and sum the corresponding values in a different range. For example, the formula =SUMIF(B2:B5, "John", C2:C5) sums only the values in the range C2:C5, where the corresponding cells in the range B2:B5 equal "John."

How do you sum a column in Excel based on text in another column?

Sum if cell contains text If you are looking for an Excel formula to find cells containing specific text and sum the corresponding values in another column, use the SUMIF function. Where A2:A10 are the text values to check and B2:B10 are the numbers to sum. To sum with multiple criteria, use the SUMIFS function.

How do you sum values based on two criteria in another column in Excel?

If you need to sum numbers based on multiple criteria, you can use the SUMIFS function. The first range (D4:D11) are the cells to sum, called the "sum range". Criteria are supplied in pairs... (range / criteria).

How do I sum multiple columns in Excel based on multiple criteria?

To sum cells that match multiple criteria, you normally use the SUMIFS function. The problem is that, just like its single-criterion counterpart, SUMIFS doesn't support a multi-column sum range. To overcome this, we write a few SUMIFS, one per each column in the sum range: SUM(SUMIFS(…), SUMIFS(…), SUMIFS(…))


3 Answers

Something like this

X   10
Y   3
X   7
X   22
Y   4
Y   9

X   "=SUMIF(A1:A6;A8;B1:B6)"
Y   "=SUMIF(A1:A6;A9;B1:B6)"
like image 128
Sergej Andrejev Avatar answered Oct 21 '22 07:10

Sergej Andrejev


use SUMIF(range, criteria, sum_range): (sum values between B2-B8, using value in A2-A8 as criterea, using the specified condition)

=SUMIF(A2:A8,"=X",B2:B8)
=SUMIF(A2:A8,"=Y",B2:B8)
like image 26
Ilya Tchivilev Avatar answered Oct 21 '22 08:10

Ilya Tchivilev


You can use SUMPRODUCT to calculate the totals. For the "X" values:

=SUMPRODUCT((A1:A6="X")*(B1:B6))

for the "Y" values:

=SUMPRODUCT((A1:A6="Y")*(B1:B6))

Hope that helps,

Eric Melski

EDIT: Apparently you must use ARRAYFORMULA to use SUMPRODUCT in Google's spreadsheet. See for example http://www.google.com/support/forum/p/Google+Docs/thread?tid=13a3eb824446e891&hl=en

like image 40
Eric Melski Avatar answered Oct 21 '22 08:10

Eric Melski