Investment Studio > Expressions > Functions > Math & Trig > SUMIF

float sumif(array test_array, string criteria, array sum_array = test_array)

Returns the sum of elements in sum_array corresponding to elements in test_array which satisfy the criteria. In other words, each element in test_array is tested for the specified criteria; if the criteria are satisfied, the corresponding element in sum_array is added to the function result.

criteria can be a value or a quoted expression: 2, "2", ">2", "apples" are all valid criteria. The absence of an explicit (quoted) operator implies equality.

If sum_array is omitted, test_array is used instead.

Examples

=sumif({4, 3, 2, 1}, ">2", {5, 10, 15, 20})

equals 15 (first element in test_array = 4 > 2 contributes first element in sum_array = 5; second element in test_array = 3 > 2 contributes second element in sum_array = 10)

=sumif({"apples", "oranges", "pears", "apples"}, "apples", {1, 1, 1, 1})

equals 2 (there are two occurrences of "apples" in test_array, both corresponding to a 1 in sum_array)

=sumif({1, 2, 3, 4}, "<2.5")

equals 3 (first and second elements in test_array satisfy criteria; in the absence of a separate sum_array they contribute their own values to the result).

See also average, count, counta, countif, product, sum, sumproduct, sumsq.