SCAN
Short answer
A running balance in one formula
SCAN([initial_value], array, lambda(accumulator, value))
- Category
- Dynamic Arrays & Modern Excel
- Excel version
- 2024+
- Also searched as
- running balance excel formula, running total one formula, cash roll forward excel, cumulative sum spill, debt balance formula, scan lambda excel
What it returns
SCAN starts from an initial value, steps through an array one value at a time, and applies a LAMBDA at each step. The LAMBDA takes the running result so far and the next value, and returns the new running result. SCAN returns every one of those intermediate results, in an array the same shape as the input.
Give it an opening cash balance of 1,000 and twelve months of net cash flow, with a LAMBDA that adds the two, and it returns twelve closing balances. That is a roll-forward: opening plus flow equals closing, carried forward month by month.
The initial value itself is not in the output. The first result is the first period’s closing balance.
Arguments that matter
| Argument | Required | What it does |
|---|---|---|
initial_value | Yes, in practice | The starting balance. The opening cash, the opening debt, or 0 for a cumulative sum. |
array | Yes | The flows to step through. One row of months in a model that runs left to right. |
lambda | Yes | A LAMBDA with two parameters: the running balance, then the current value. Returns the new balance. |
The LAMBDA’s parameter order is fixed. The first parameter is always the accumulator, the second is always the current value. Name them bal and flow, or debt and draw, so the order is obvious.
The LAMBDA can do more than add. Anything that turns last period’s balance and this period’s input into this period’s balance works: interest accrual, a floor at zero, a cap at a facility limit.
In a financial model
Roll-forwards are everywhere: cash, debt, fixed assets, deferred revenue, retained earnings, headcount. The usual build is two rows per balance, opening linked to the prior closing and closing as opening plus flows, filled right across every period.
That works, and it gives you twelve or sixty separate formulas per row. Any of them can be overwritten. A pasted value in month 23 breaks every month after it, and the grid still looks smooth.
SCAN replaces the closing row with one formula. It spills across the periods, runs left to right like the rest of the model, per time runs left to right, always, and an overwrite turns the whole row into #SPILL!. It is the strictest form of one row, one formula.
Keep the opening row for the reader. Auditors and reviewers expect to see opening, movements and closing. Derive the opening row from the SCAN output so nothing is entered twice.
Worked example
A cash flow tab CF, months across row 8 from column F to Q. Net cash flow in F20:Q20. The opening cash balance for the first month sits in E22, linked from the opening balance sheet.
Closing cash in F22:
=SCAN(E22, F20:Q20, LAMBDA(bal, flow, bal + flow))
Opening cash in F21, the closing row shifted one month right:
=HSTACK(E22, DROP(F22#, , -1))
| Jan-26 | Feb-26 | Mar-26 | Apr-26 | |
|---|---|---|---|---|
| Opening | 1,000 | 1,250 | 850 | 970 |
| Net flow | 250 | (400) | 120 | (600) |
| Closing | 1,250 | 850 | 970 | 370 |
A debt balance with interest charged on the opening balance and added to principal, as on a PIK note. MonthlyRate is a defined name, net draws less repayments are in F30:Q30, and the opening balance is in E32:
=SCAN(E32, F30:Q30, LAMBDA(debt, draw, debt * (1 + MonthlyRate) + draw))
The interest is inside the LAMBDA, so the balance and the interest on it are calculated in the same step. That keeps the schedule free of circular references.
Old way vs new way
Two rows filled right across every month, opening in row 21 and closing in row 22:
F21: =E22
F22: =F21+F20
One formula in F22:
=SCAN(E22, F20:Q20, LAMBDA(bal, flow, bal + flow))
The old build is 24 formulas for a year and twice that again for each extra year. The new one is a single cell that follows the length of the flow row.
Where it breaks
The balances are one month out. You expected the opening value in the first column. SCAN leaves the initial value out and starts with the first closing balance. Fix: put SCAN in the closing row, and build the opening row from it with HSTACK and DROP as above.
Balances carry over from one account to the next. The array was a grid with several accounts, one per row. SCAN steps through a 2D array row by row, so the second account starts from the first account’s final balance. Fix: one SCAN per account row, each with its own opening balance.
#VALUE! from one month onward. A flow cell holds text, such as a - or n/a from an export. The error enters the running balance and every later month inherits it. Fix: clean the flow row at its source, in Power Query or in the formula that builds it, so it holds numbers only.
Excel warns of a circular reference. Interest was calculated in another row from the SCAN output, then added back into the flows SCAN reads. Fix: move the interest into the LAMBDA, as in the debt example, so each step calculates it from the balance it already has.
SCAN returns #CALC!. The LAMBDA returns more than one value at a step, often because a parameter was used where a whole range was intended. Fix: check that the LAMBDA works on one balance and one flow and returns one number.
Every cell shows #NAME?. SCAN needs Excel 2024 or Microsoft 365. Fix: keep the two-row fill-right build for files that go to Excel 2021 or earlier.
Related functions
- REDUCE. The same stepping, returning only the final value. Use it when you need the ending balance and not the path.
- LAMBDA. The logic at each step. Name a roll-forward rule, such as a revolver with a limit, and reuse it across schedules.
- SEQUENCE. Builds the period header that the flow row and the SCAN output line up under.
- TAKE. Pulls the last closing balance out of the spill for the balance sheet:
TAKE(F22#, , -1). - VSTACK. Its sibling HSTACK joins the opening value onto the shifted closing row to build the opening line.
- BYROW. For one result per row of a grid, not a running result along it.
- Read tie the balance sheet first. A SCAN-built cash row feeds the balance sheet, and the check proves it.
