To use the REDUCE
function in Google Sheets, you either type the formula directly into a cell or insert it via the function menu. The REDUCE
function applies a LAMBDA
function to each value in an array along with an accumulator, reducing the array to a single result.
Here's how:
1. Typing the Formula:
- In any cell, start typing
=REDUCE(
. Google Sheets will offer autocomplete suggestions.
2. Inserting via the Function Menu:
- Click on the cell where you want the formula.
- Go to Insert → Function → Array → REDUCE. This will insert the basic
REDUCE
formula structure into the cell.
3. Understanding the REDUCE Syntax:
The REDUCE
function has the following syntax:
REDUCE(initial_value, array_or_range, LAMBDA(accumulator, current_value, formula_expression))
initial_value
: The starting value for the accumulator. This is the value that theaccumulator
will hold before processing the first element of the array.array_or_range
: The array or range of cells to be reduced. This is the data you want to iterate over.LAMBDA(accumulator, current_value, formula_expression)
: ALAMBDA
function that is applied to each element in thearray_or_range
.accumulator
: A name to refer to the accumulated value. This starts with theinitial_value
and gets updated in each iteration.current_value
: A name to refer to the current element being processed in the array.formula_expression
: The formula that calculates the new accumulated value based on theaccumulator
and thecurrent_value
.
Example:
Let's say you have a range of numbers in cells A1:A5 (e.g., 1, 2, 3, 4, 5) and you want to calculate their sum using REDUCE
. You would use the following formula:
=REDUCE(0, A1:A5, LAMBDA(accumulator, current_value, accumulator + current_value))
0
: Theinitial_value
is 0 because we're starting the sum at zero.A1:A5
: Thearray_or_range
is the range containing the numbers.LAMBDA(accumulator, current_value, accumulator + current_value)
: ThisLAMBDA
function takes theaccumulator
(initially 0) and adds thecurrent_value
(each number from A1:A5). The result becomes the newaccumulator
for the next iteration.
The result of this formula would be 15 (1+2+3+4+5).
Explanation:
The REDUCE
function iterates through the range A1:A5.
- Iteration 1:
accumulator
= 0,current_value
= 1,formula_expression
evaluates to 0 + 1 = 1. Theaccumulator
is updated to 1. - Iteration 2:
accumulator
= 1,current_value
= 2,formula_expression
evaluates to 1 + 2 = 3. Theaccumulator
is updated to 3. - Iteration 3:
accumulator
= 3,current_value
= 3,formula_expression
evaluates to 3 + 3 = 6. Theaccumulator
is updated to 6. - Iteration 4:
accumulator
= 6,current_value
= 4,formula_expression
evaluates to 6 + 4 = 10. Theaccumulator
is updated to 10. - Iteration 5:
accumulator
= 10,current_value
= 5,formula_expression
evaluates to 10 + 5 = 15. Theaccumulator
is updated to 15.
The REDUCE
function then returns the final value of the accumulator, which is 15.
The REDUCE
function, in conjunction with LAMBDA
, provides a powerful way to perform cumulative calculations and aggregations on arrays in Google Sheets. It allows you to apply custom logic to each element in an array and accumulate the results into a single value.