Reduce an array
=REDUCE([initial_value], array, function)
| Parameter | Description |
|---|---|
initial_value |
[optional] The initial value of the accumulator. |
array |
The array to be reduced. |
function |
The function or custom LAMBDA to apply. |
The REDUCE function takes three arguments: initial_value, array, and function. Initial_value is an optional initial seed value to use for the accumula
LAMBDA(a,v,calculation)
The first argument, a, is the accumulator. The accumulator begins as the initial_value provided to REDUCE and changes as the REDUCE function iterates
=REDUCE(0,{1,2,3,4,5},LAMBDA(a,v,a+v)) // returns 15
One way to use the REDUCE function is to create a conditional sum that uses custom logic that would be difficult with a built-in function like SUMIFS.
=REDUCE(0,B5:B16,LAMBDA(a,v,IF(ISEVEN(v),a+v,a)))
Notice that we have provided an initial_value of zero (0) and the array is given as the range B5:B16. The LAMBDA calculation looks like this:
LAMBDA(a,v,IF(ISEVEN(v),a+v,a))
To calculate a conditional sum of odd numbers, we can simply swap ISEVEN for ISODD:
=REDUCE(0,B5:B16,LAMBDA(a,v,IF(ISODD(v),a+v,a)))
Finally, to illustrate what the same formula looks like without any conditional logic, the formula in cell D7 sums all numbers in the range B5:B16 lik
=REDUCE(0,B5:B16,LAMBDA(a,v,a+v))