views:

1792

answers:

5

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 & 16 are the results of the formulas):

X    39           -> 10 + 7 + 22
Y    16           -> 3 + 4 + 9
+1  A: 

One quick and dirty solution is to make two new columns. For each row x, Cx should be something like =Ax=='X'?Bx:0. Do the same for column D, but checking Ax=='Y'. Then sum C and D.

(Not sure if that matches Google Spreadsheet syntax exactly.)

endtime
one reason I can't add more columns is because I'm building a google form and if I add columns it gets really crazy. I have to put this formula on a separate spreadsheet without modifying the spreadsheet that the google form is using.
Jon Erickson
+3  A: 

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)"
Sergej Andrejev
worked like a charm. thanks.
Jon Erickson
+1  A: 

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

Eric Melski
As far as I ,now, it doesn't work in Googledocs
Martin
I believe it is supported, but you must use ARRAYFORMULA to use SUMPRODUCT. See my edit above.
Eric Melski
+1: In Excel 2003 I was able to do =SUMPRODUCT((A1:A6<>"")*(B1:B6)) to only count items in B1 through B6 where there was something in the corresponding A1 through A6. Thanks for the answer (to my slightly different question)!
Craig
A: 

The fact that google docs doesn't support the sumproduct and the -- operator as Excel does is a bit bothering. You could always replicate the functionnality using more columns, but as you responded in one of your comments, this seems impossible.

You could check if a hidden column is rendered in the form. If it is not, this is the way to go.

Martin
+1  A: 

use SUMIF: (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)

Ilya Tchivilev