REDUCE collapses an array into one result by visiting each value in turn. At each step a LAMBDA combines the result so far, the accumulator, with the next value, and returns the new accumulator. When the values run out, REDUCE returns the last accumulator.
The arguments
- initial_value is what the accumulator holds before the first value. Use
0 for a sum or count, 1 for a product, "" to build text. - array is the data to walk through. A range is read across each row, left to right, and then down.
- lambda takes two parameters. Name them as you like:
LAMBDA(acc,v,acc+v) has acc for the accumulator and v for the current value.
=REDUCE(0,A2:A6,LAMBDA(acc,v,acc+v)) starts at 0 and adds each amount in turn. The result is 680, the same as SUM, which is the simplest way to see how it works.
Where REDUCE earns its place
SUM, MAX and COUNTIF cover the simple cases, so reach for REDUCE when the calculation has state that a single function can't hold:
- Counting with a rule.
=REDUCE(0,A2:A6,LAMBDA(n,v,n+(v>100))) counts amounts over 100. The comparison returns TRUE or FALSE, which add as 1 or 0. - Joining text with your own rules.
=REDUCE("",B2:B5,LAMBDA(a,v,IF(a="",v,a&", "&v))) builds a list with commas and no leading comma. - Tracking the best so far.
=REDUCE("",B2:B5,LAMBDA(a,v,IF(LEN(v)>LEN(a),v,a))) returns the longest item. - Compounding. A balance that grows by a different rate each period is a REDUCE, because each step depends on the last.
REDUCE or SCAN
REDUCE returns only the final value. SCAN uses the same arguments but returns the accumulator after every step, such as a running total. If you want to see the work, use SCAN. If you only want the answer, use REDUCE.
Notes
The LAMBDA is called once for each value, so a very large array makes many calls. If a built-in function does the job, such as SUMIFS or TEXTJOIN, it will be faster and easier to read. The accumulator can be a number, text or a logical value.
An error in the array passes through the LAMBDA like any other value, and usually turns the final result into an error. See LAMBDA for writing the function and MAP for applying one to each item without accumulating.
REDUCE needs Microsoft 365, Excel 2024 or later, or Excel for the web.