Calculating running totals or cumulative account balances in spreadsheets traditionally required dragging formulas down hundreds of rows or employing volatile array functions that bog down calculation engines. Google Sheets provides the native SCAN helper function alongside LAMBDA, which iterates through an array, accumulates values sequentially, and spills an entire column of rolling totals from a single dynamic formula.
Understanding the Mechanics of SCAN and LAMBDA
The SCAN function processes arrays in order, maintaining an accumulator variable across each element:
=SCAN(initial_value, array, LAMBDA(accumulator, current_value, formula_expression))- initial_value: The baseline starting value of the accumulator (typically
0for running sums or1for multiplicative compounding). - array: The range or matrix containing the numeric values to process.
- LAMBDA(accumulator, current_value): A custom anonymous function where
accumulatorholds the running total andcurrent_valuerepresents the current row value.
Creating a Basic Running Total Formula
To calculate a running cumulative total for sales transactions in column range B2:B50 starting from zero:
=SCAN(0, B2:B50, LAMBDA(acc, val, acc + N(val)))
Entering this single formula into cell C2 automatically spills calculated cumulative balances across C2:C50 without dragging handles. Wrapping val in N() gracefully converts empty cells or accidental text labels to zero, preventing calculation errors.
Handling Open-Ended Dynamic Ranges with Blank Row Suppression
In automated data pipelines where new transactions append dynamically to the bottom of a sheet, calculate running totals only on populated rows:
=LET(
data, B2:B,
SCAN(0, data, LAMBDA(acc, val, IF(ISBLANK(val), "", acc + val)))
)
By integrating IF(ISBLANK(val), "", ...) within the LAMBDA calculation, rows without entries remain clean and blank rather than repeating the final accumulated balance indefinitely.
Calculating Running Balances with Debits and Credits
For financial ledger sheets featuring separate debit (column B) and credit (column C) entries:
- Calculate net transactional values dynamically using
BYROWor array subtraction. - Feed the net differences into
SCAN:=LET( debits, B2:B100, credits, C2:C100, net_amounts, MAP(debits, credits, LAMBDA(d, c, N(c) - N(d))), SCAN(0, net_amounts, LAMBDA(acc, net, acc + net)) ) - The formula outputs accurate, real-time account balances, incrementing on credit receipts and decrementing on debit disbursements.
Rolling Compounding and Percentage Growth Calculations
Beyond simple addition, SCAN effortlessly handles geometric and compounding growth models:
# Calculate compounding monthly portfolio returns based on percentages in column B
=SCAN(10000, B2:B13, LAMBDA(acc, rate, acc * (1 + rate)))
This single formula tracks portfolio valuation starting at £10,000 and compounding through monthly growth rates, demonstrating the immense power of SCAN over legacy iterative spreadsheet models.