Better Conditional Sum in Google Spreadsheet
googledocsIn the original
article I
described using
SUM()
and FILTER()
in a Google
Docs spreadsheet to calculate the sum over a
subset of cells within a column. Turns out, there’s a better way:
=SUMIF('Worksheet name'!E:E, "food", 'Worksheet name'!D:D)
In this version, the SUMIF()
function combines the behavior of
the SUM()
and FILTER()
functions I was using before.
The first argument is the column to be compared to the filter value, the second
argument is the filter value itself (or the cell address containing the filter
value) and the third argument is the column over which the sum is to be
calculated. So in the above example, in column D of the worksheet named
“Worksheet name” all cells for which the corresponding cell in column E
contains the value “food” are selected, and the sum of those select cells is
returned.
In addition to being more succinct and easier to read, the use of
SUMIF()
has the added benefit of returning 0
if there
are no rows matching the filter. Using SUM()
and
FILTER()
instead returns #N/A
.