13-Week Cash Flow Forecasting in SQL — 40 Worked Answers on Real Treasury Data (DuckDB)
Every treasury forecasts its cash, and almost none of them goes back to check. This notebook does both in SQL. It builds the bank balance at any date from the movements, tests the loan covenant at the close of every Sunday the way the bank does, and then grades 432 weekly forecast versions against what the bank actually did in each of their thirteen weeks — by bias, which says which way a company leans, and by MAPE, which says how wrong it is either way. Every one of the forty answers was checked against the dataset's own answer book, on two versions of DuckDB, before the file was published.
By findi360 · Updated November 2026
- Format
- Jupyter notebook (.ipynb), SQL throughout, outputs saved
- Data
- DATA DARE DD-CFP-002 · 59,499 bank rows · 432 forecast versions · CSV, Parquet, JSON and Excel
- Covers
- 40 questions, easy to very hard
- Needs
- Python 3.9+, duckdb, pandas · checked on DuckDB 1.3.2 and 2.0.0
Bias and MAPE: the two numbers that grade a forecast
Bias is the net error: total forecast less total actual, over the actual. A company with a positive bias habitually expects more cash than arrives, and you can correct for it. MAPE — the mean absolute percentage error — is the average size of the error whichever way it went. A forecast can have almost no bias and still be useless, because the misses cancel out. You need both, and this notebook works out both in one line each from a single view.
The interesting part is how they move as the horizon lengthens. A forecast of next week is worth more than a forecast of week thirteen, and one of the questions finds the exact week where the error passes five per cent — which is, in practice, the week the forecast stops being worth acting on.
Grading a forecast means defining the actual
This is the part people skip. A forecast line says "week 3 will bring 4.1 million of customer receipts". To grade it you have to decide what the actual was, and that means taking every bank row of that company in the seven days from that week's start whose category maps to that forecast line — which is why the category map matters more than it looks. Four different bank categories all mean "customer receipts" here: an ordinary receipt, an unidentified one, a card settlement and a store cash deposit.
The notebook works the actual out once, for every company, week-start and line in the file, and keeps it in a single view beside the forecast. After that every bias and MAPE question is one line of SQL, and the hard part — which was always the definition, not the arithmetic — is written down where you can see it.
The covenant, tested the way the bank tests it
There is no balance column anywhere in this dataset. Each company's opening balance is a row in the bank file like any other, so the balance at a date is the sum of everything dated on or before it — and the notebook defines that once as a macro rather than writing the arithmetic out again.
The loan then requires a minimum cash balance at the close of every Sunday. With no calendar table in the set, the Sundays are generated, each company's balance is worked out on every one of them, and the headroom sits beside it. One table, 143 weeks, three companies — and one Sunday where the covenant was broken.
What the forecast implied, and what happened
A version forecasts movements, not balances, so the balance it implied at the end of each week is a running total — the opening balance plus every week up to that one. In SQL that is a window function, and it replaces the cumulative column that drifts the first time someone inserts a row in a spreadsheet.
With that, two of the hardest questions become comparisons rather than projects: how far the forecast balance at week thirteen drifted from the balance that Sunday actually closed on, and in how many versions the treasury's own forecast already showed the covenant being breached. The second one is the question a board asks after the fact, and the answer here is: in plenty of them.
SQL, Python or Excel?
A 13-week forecast is usually built in Excel, and for the forecast itself that is reasonable — it is a judgement exercise with a spreadsheet's flexibility. The grading is not. Comparing 432 versions against the bank, by company and by week out, is a grouping problem over tens of thousands of rows, and in SQL it is a handful of queries that run in under a second and can be re-run every week.
That is the useful split: forecast where judgement lives, grade where the data lives. The same group's receivables and payables ledgers are published with their own worked solutions in SQL, so one group's cash can be followed from the invoice, through the payment run, to the bank line that lands in a forecast week.
Inside the notebook
- Libraries — A cell that tells you which of the three libraries is missing before anything else runs.
- Point DuckDB at the files — Views over the CSV, the Parquet file and two JSON files — plus the two Excel files, registered from pandas.
- First look — The category map, the three companies and their loans, and SUMMARIZE over the bank file.
- Fix the data — Append the new quarter, map every bank row to its forecast line, and unnest 432 forecast versions.
- Four working views — The balance at any date, every Sunday with its covenant headroom, the actual of each forecast week, and the forecast balance week by week.
- The answers, Q01 to Q40 — One query per question, each printing its answer, and a final list of all forty.
Excel + DuckDB — the Power of Plus
Excel does this model well when all five of these are true. When two or more are not, you add DuckDB — not instead of Excel, beside it. That is the Power of Plus.
- One-off — you build it once, not again every month.
- Fixed structure — the same accounts, entities and layout each time.
- Small enough to check by eye — thousands of rows, not millions.
- One source — one ledger or one plan, not data scattered across systems and files.
- No live link needed — nobody expects it to update from the ledger by itself.
What grading last month's forecast really costs in Excel
- Find the four versions published that month and line their thirteen weeks up by hand.
- Pull the bank data for each of those weeks and map every category to a forecast line again.
- Rebuild the bias and error columns, for every company and every week out.
- Do it again next month, and accept that nobody will ever check more than one month at a time.
The same work, once it repeats
In DuckDB the grading is a handful of queries over every version at once, so "how good is our forecast, by week out, by company, this year" is one answer rather than a project.
DuckDB still needs your judgement where the data is new — but everything after that repeats by itself.
Learn it on findi360: AR Full-Scale Model · Level 2
Notify me when it opensWant to see what scattered data looks like? Three ERPs, three currencies and one consolidated group: DD-CON-004 — Diversified Group Close: Acquisition, Goodwill & Multi-ERP Consolidation
Who this model is for
- Treasury and FP&A teams who publish a rolling forecast and have never graded it
- Finance teams with a cash covenant to monitor between reporting dates
- Controllers who want actuals against the treasury calendar without rebuilding a workbook each month
- Candidates preparing for the DATA DARE cash flow challenges and the Silver Seal test
Frequently asked questions
Do I need to know SQL already?
Only the shape of it. Every block has a short explanation in plain words before it, and the two features that do the heavy lifting — window functions and aggregate filters — each get a paragraph where they first appear.
What do I have to install?
One line: pip install duckdb pandas openpyxl. There is no server and no database file — DuckDB runs inside the notebook and reads the dataset files where they already are.
Which version of DuckDB does it need?
It was run and checked on DuckDB 1.3.2 and on DuckDB 2.0.0 and gives identical answers on both.
What is the difference between bias and MAPE?
Bias is the net error and tells you which way a forecaster leans, so you can correct for it. MAPE is the average absolute error and tells you how wrong they are regardless of direction. A forecast with no bias can still have a large MAPE, which is why the notebook reports both.
Can I grade my own forecast with this?
Yes, and it is the main reason to read it. You need two things: your forecast versions with the week each line covers, and your bank data with a category you can map to the same lines. The mapping is the work; the grading is then the queries in section 6.3 almost unchanged.
Are the answers in the file correct?
Every one of the forty was compared with the Expected_Result_Value in the dataset's own Question Bank, within its stated tolerance, on two versions of DuckDB — and then the notebook was executed from top to bottom and its printed answers checked again.
Keep going on findi360
- DATA DARE Mizan Manufacturing: The 13-Week Cash Forecast — Cash Flow Planning DD-CFP-001 · L1 — XLSX / ~10,000 rows Mizan Group: How Good Is the Treasury's Forecast? — Cash Flow Planning DD-CFP-002 · L2 — CSV / ~30,000 rows
- DATA DRILL Test yourself — Power BI practice questions One question at a time, or a 50-question timed simulation.
- GUIDE 13-week cash flow forecast in Python with pandas The same work in Python, with tested code and its real output.
- COURSE Courses and free recordings Power BI, Python and finance courses for finance professionals.
Related models
-
13-Week Cash Flow Forecast — 3 Scenarios + Revolver
A rolling 13-week cash flow forecast built the way treasurers and lenders read one: open customer and supplier invoices, a sales and purchases plan and dated items such as payroll, tax and loan instalments become weekly receipts, payments and closing cash, under three scenarios, with a revolver that draws and repays by itself.
-
13-Week Cash Flow Forecast in Python — Jupyter Notebook
The thirteen-week cash forecast built the way a treasury team builds it — from the invoices open on the day it is made, the delay each customer really takes, the Monday payment run and the dates treasury already knows — and then the same forecast run for last year and marked against the bank.
-
AR Analysis in SQL — DuckDB Notebook
The whole receivables question bank answered in SQL. DuckDB reads the CSV, the Parquet file and the nested JSON where they sit, four views and one macro carry the thinking, and forty queries of two or three lines each answer everything from the ageing bucket to the overdue book corrected for cash nobody could place.
Need a model we have not built yet?
Tell us what you need. Members vote, and the most-requested models are built first — free to download.
Educational model on illustrative data. Not investment, accounting or tax advice. © findi360 — share freely with attribution.
