SCAN is REDUCE with the working shown. It makes the same walk through an array, combining the running result with each value, but instead of returning only the final answer, it returns the result after every step, in an array the same size as the input.
The arguments
- initial_value is where the running result starts:
0 for a total, or a real opening balance from a cell. - array is the data to walk through.
- lambda has two parameters, the accumulator and the current value, and returns the new accumulator.
=SCAN(0,B2:B7,LAMBDA(acc,v,acc+v)) returns a running total: each cell of the result is the total of that month and all the months before it.
Running totals in one formula
The usual way to build a running total is to copy a formula such as =SUM($B$2:B2) down a column, with careful dollar signs. SCAN does it in one formula in one cell. The result spills, grows with the data, and can't be broken by a stray overwrite.
Balances
Give SCAN an opening balance from a cell and it becomes a ledger. =SCAN(E1,B2:B7,LAMBDA(bal,x,bal+x)) starts at the opening balance in E1 and applies each month's net cash flow to it.
Other running calculations
The LAMBDA can do anything with the accumulator:
- Running count.
=SCAN(0,B2:B7,LAMBDA(n,v,n+(v<0))) counts the negative months so far. - Running maximum.
=SCAN(0,B2:B7,LAMBDA(m,v,MAX(m,v))) returns the highest value seen so far. - Compounding.
=SCAN(1000,SEQUENCE(4),LAMBDA(a,i,a*1.5)) grows 1,000 by 50% four times. The values 1 to 4 only set the number of steps; the LAMBDA ignores them.
SCAN and REDUCE
REDUCE returns only the last value, and SCAN returns them all. If you ever write a REDUCE and want to see what it is doing, change it to SCAN and look at the intermediate numbers. The final cell of the SCAN result is what REDUCE would have returned.
Rules
The accumulator must be a single value at each step. If the LAMBDA returns an array, SCAN returns #CALC!. The result is a spilled array, so #SPILL! means the cells it needs aren't empty. A range is read across each row and then down, and the result has the same shape as the range.
See LAMBDA for how to write the function, and MAP when each item can be calculated on its own, without a running result.
SCAN needs Microsoft 365, Excel 2024 or later, or Excel for the web.