TL;DR: A billing report showed, per area, what was billed this year, what was collected, and what was still outstanding. Billed minus collected should have equalled outstanding. It was off by about 40%. A side-by-side report can’t explain a gap like that, because it has nowhere to put the explanation. So I built a movement statement next to it: billed + additions − collections − reductions − advance applied ± an explicit unexplained column = outstanding. Split that way, three quarters of the gap was one bug (advance payments were stored under a different receipt type than the collection query read), about a fifth was a definition (billed used the gross tax, outstanding the discounted amount), and about 5% was penalty and notice fees that never reached the balance. If a finance report has no column for the difference it can’t explain, it will hide its own bugs.

Key takeaways

  • “A − B should equal C” is a claim with hidden terms. Write every flow out as its own column, and the hidden ones show up as the rows that don’t balance.
  • Give the residual its own column. A report that can’t show the unexplained amount reports it silently, inside whichever number absorbed it.
  • One bug usually dominates a large gap. Decompose before you fix, or you’ll fix the small causes first and still be wrong.
  • Changing which table a report reads rarely moves the number. Changing what a word means (“billed”, “collected”) moves it a lot.
  • Bucket money by the year of the bill it pays, not the date it arrived. A payment in April against last year’s bill is arrears, not this year’s collection.

The report that didn’t add up

The system is a public-sector billing platform. Every account gets a yearly bill with many charge heads: a base charge, cesses, penalties, notice fees. Payments come in as receipts. A separate balance table holds what each account still owes. I own the reports and dashboards on top of it; the billing and payment code belongs to another team. The stored-total post is from the same system.

The report in question is the one people actually look at: one row per area (nineteen areas, a row for government-owned properties, and a total), with columns for billed this year, collected this year, and outstanding, plus the same for arrears.

The arithmetic anyone does on first sight is current billed minus current collected, compared with current outstanding. On a live year, those were off by about 40% of the billed-minus-collected figure. It wasn’t rounding or timing. Something big was missing.

A comparison report gives you three numbers and a feeling. It doesn’t say which of the three is wrong, or whether all three are right and the identity is missing terms. So the first job wasn’t fixing a query. It was building a version of the report in a shape that could explain itself.

A movement statement instead of a comparison

Accountants solved this a long time ago. A roll-forward (or movement statement) starts from an opening figure, lists every flow that changes it during the period, and lands on the closing figure. The international standard for fixed assets, for instance, requires exactly this: a reconciliation of the carrying amount at the start and end of the period, showing additions, disposals and the rest (IAS 16, paragraph 73). The same idea works on a billing report:

  billed this year                    (bills raised, principal heads)
+ additions                           (upward revisions to a bill after it was raised)
+ interest and penalty raised         (charged on the balance during the year)
− collected                           (normal payments)
− advance applied                     (money paid ahead, used against the bill)
− reductions                          (downward revisions, write-offs)
− rebates                             (early-payment discounts)
± unexplained                         (the column that keeps everyone honest)
= outstanding

  ▼  every row must balance; the only place a gap may live is "unexplained"

The last column is the point. The old report had a residual too. It just didn’t have a column, so the residual was hidden inside “collected” and “outstanding”, and both looked plausible on their own.

In SQL, the statement is a set of conditional aggregates. Postgres’s FILTER clause keeps each flow readable as its own line:

-- Illustrative.
WITH flows AS (
  SELECT b.area,
         sum(b.billed_principal)                                        AS billed,
         sum(b.revised_up)                                              AS additions,
         coalesce(sum(r.amount) FILTER (WHERE r.kind = 'payment'), 0)   AS collected,
         coalesce(sum(r.amount) FILTER (WHERE r.kind = 'advance'), 0)   AS advance_applied,
         sum(b.revised_down)                                            AS reductions,
         sum(b.rebate)                                                  AS rebates
  FROM bills b
  LEFT JOIN receipts r ON r.bill_id = b.id
  WHERE b.bill_year = 2026
  GROUP BY b.area
)
SELECT f.*,
       o.outstanding,
       o.outstanding
         - (f.billed + f.additions - f.collected - f.advance_applied
            - f.reductions - f.rebates)                                 AS unexplained
FROM flows f
JOIN outstanding_by_area o USING (area)
ORDER BY f.area;

(The real one is wider, and it pre-aggregates receipts per bill so the join can’t fan out. The shape is the same.)

Where the 40% went

With every flow in its own column, the gap stopped being one number and became three:

Share of the gapWhat it wasKind of causeFix
~75%Advance payments were stored under their own receipt type; the collection query read only the normal typeBugRead both types; show advance as its own column
~22%Billed summed the gross tax; outstanding was posted at the discounted amountDefinitionBill the same quantity the balance uses, show the rebate as a flow
~5%Penalty and notice fees on the bills never reached the balance tableMissing postingReport them as their own line; flag for the billing team
~−1%Small per-head differences across the restNoiseLeft in “unexplained”, where it belongs

The three causes overshoot the gap slightly (about 101%), which is what you’d expect when each is measured on its own and the rest nets out in small differences. That overshoot is the unexplained column doing its job: small, visible, and not hiding inside anything.

The advance bug was the big one. Advance payments, money people pay before their bill is raised, were recorded as a separate receipt type, with the amount in separate columns. The collection query only looked at the normal type:

-- Illustrative. BROKEN.
SELECT sum(amount) FROM receipts WHERE kind = 'payment';

-- FIXED: advance is collection too, shown as its own flow.
SELECT sum(amount) FILTER (WHERE kind = 'payment') AS collected,
       sum(amount) FILTER (WHERE kind = 'advance') AS advance_applied
FROM receipts;

The report showed advance collection at roughly four ten-thousandths of a percent of its real value. Not a rounding error: close to zero, against a real figure that turned out to be about 79% of everything collected so far that year. Most of the year’s money had arrived as advance, and the report was missing most of the money. The dashboard, built on the same reading, showed about a fifth of real collection.

Nobody had noticed because a comparison report has no way to notice. Collected looked low, outstanding looked high, and both are things a finance report can plausibly say about a year that isn’t over.

The definition gap was about a fifth of it, and nothing was broken. Billed summed the tax as raised. Outstanding held the tax after the early-payment discount. Both are correct numbers for different questions. The movement statement has a rebate line, so the difference stops being a gap and becomes a flow. This is the same trap as the “overdue versus issued” split in the stored-total post: two right answers to two different questions, sitting next to each other as if they were one.

The missing posting was small and real. Penalty and notice fees were on the bill rows and never reached the balance table. Of everything charged under those heads that year, essentially none of it showed up as owed. The report can’t fix that; it can show it. The fix belongs to whoever posts the balance, and the statement made that a short conversation instead of a long one.

The flow that has no record

Interest and penalty on the balance were the hardest part.

Interest accrues on outstanding. In this system, it is added to the balance directly. There is no ledger row that says “on this day, this much interest was charged”. So the movement statement has a term it can’t fill from records: the closing interest is known, the opening interest is known, the payments against interest are known, and the charge in between is only what’s left over.

That changes how to read “balanced”. On interest, the statement balances by construction, because the charge is solved for. A zero there proves nothing. So I reconciled on principal, where every flow has a record, and kept interest as a separate, clearly labelled line that the statement doesn’t claim to prove.

A related trap: in the older buckets, penalty was counted twice. The bill’s penalty columns hold what is still due, not what was raised. Adding them to billed, which already included the raised penalty, and then subtracting payments counted the paid part twice. In the oldest bucket, penalty came out about ten times too high, and the arrears bucket had the same problem at a larger size. Same lesson again: know what a column means before you add it to anything.

Two decisions that mattered more than the queries

Bucket by the bill’s year, not the receipt date. A payment made in the first days of April against last year’s bill is arrears collection, not this year’s. The old report put it by receipt date. It was a small slice, under 1% of the gap, and it made the current and arrears rows each wrong in opposite directions while the total looked fine. A movement statement per bucket catches that kind of error; a single total never will.

Swapping the source table moved almost nothing. An early suspect was that the report read a snapshot table instead of the live calculation table. Rebuilding it from the calculation table changed current billed by nothing and arrears by about 0.05%. Changing the definition of billed (discounted tax, unposted heads left out) moved it by about 10%. When two tables agree and the report is still wrong, stop comparing tables and start writing down what each column means.

One more thing about how the work was done: my only access to production data was read-only. That sounds like a limitation. It made the investigation cheap to repeat and impossible to make worse, and I’d choose it again.

Report shapeWhat it showsWhat it hidesUse it when
Side-by-side (billed, collected, outstanding)Three totalsEvery missing flow, inside whichever total absorbed itNever alone, for money
Movement statement with an unexplained columnEvery flow, and the size and place of what’s leftNothing it can’t record; unrecorded flows show as residualAny report where a reader will do the subtraction
Per-account reconciliationExactly which accounts are offThe overall shape; too detailed to readChasing a residual once the statement has located it

Balanced, and what the residual pointed at

On principal, the statement balanced on all 21 rows (nineteen areas, government properties, and the total) once the unexplained column was counted. That column was not zero everywhere at first, and that was the useful part.

In the arrears bucket, about 10% of the accounts with a residual carried about 98.6% of the unexplained amount: last year’s bills where the balance was higher than the bill. That narrowed a system-wide question to a list. Separately, credit balances moved into the advance account during a data migration showed nothing owed while the money sat elsewhere. Once the statement netted off held advance, the total residual dropped to about 3% of what it had been, and every remaining rupee had a named cause. In a later check, netting off held advance and adding upward revisions cut the principal gap by 98.4%.

That’s the reason to keep the residual visible. A zero you forced tells you nothing. A residual you can see, concentrated in a few accounts, tells you where to look next.

What actually broke in production

The old report and the dashboard under-reported collection for as long as advance payments existed as their own type, because neither read them. On the dashboard it got worse: when the advance fix was first applied there against a stale table that held no billed amount for the current year, the current balance would have gone negative: collection with nothing to collect against. The interim dashboard fix switched it to the newer table.

The rebuilt report is live, and I’d rather be exact about what that covers. It reads advance payments, and it kept the column layout people already knew. The movement statement, including the results in the section on the residual, is separate analysis and not the screen people use.

FAQ

Why doesn’t billed minus collected equal outstanding in my report? Because the identity has more terms than those three: additions and reductions after billing, rebates, advance payments, interest and penalty charged during the period, and whatever was never posted. A side-by-side report hides all of them inside the totals. Write each flow as its own column and the rows that don’t balance will tell you which term is missing or misread.

What is a movement statement? A movement (or roll-forward) statement starts from an opening figure, lists every flow that changed it in the period, and lands on the closing figure: opening + additions − reductions = closing. Accountants use it for fixed assets and receivables. In a software report, it turns “these numbers don’t match” into “this flow is missing or wrong”.

Should a financial report have an unexplained or residual column? Yes, whenever a reader is expected to do the subtraction. Without it, the unexplained amount still exists; it just sits inside whichever number absorbed it, where it looks plausible. A visible residual should be small, and when it isn’t, its location tells you which accounts to investigate.

How do I find which cause explains most of a reconciliation gap? Measure each candidate cause on its own, as a share of the gap, before fixing anything. In my case one cause was about three quarters of the gap, and fixing the smaller ones first would have left the report obviously wrong. If the shares add up to about 100%, you’re done; if they don’t, there’s another cause.

Should payments be grouped by receipt date or bill year? For a billed-versus-collected report, by the year of the bill the payment settles. A payment made early in the new year against last year’s bill is arrears collection. Grouped by receipt date, it inflates this year’s collection and understates arrears, while the total still looks right.

How do you reconcile interest that has no accrual records? You can’t prove it from records, so don’t pretend to. If interest is added to the balance directly, the period’s interest charge can only be solved for, which makes any balance on it automatic. Reconcile on the heads that have a record for every flow, label interest as derived, and fix the underlying problem by recording the charge when it happens.

Does it matter which table a report reads from? Less than you’d think. Swapping a snapshot for the live calculation table changed my totals by about 0.05%. Changing what “billed” meant changed them by about 10%. When a report is wrong and the tables agree, the definition is the problem.

What I’d still improve

  • Put the unexplained column on the live screen. The live report kept its old layout, so the residual is still something I compute, not something its readers see. Until it’s a column people look at, the next posting bug will hide the same way.
  • An alert on the residual. A threshold per row would turn the next posting bug into a message instead of a quarter-end surprise.

The one idea to take away

If your report can’t show where the difference went, it will show you the wrong number and call it right.

Every total in a finance report absorbs whatever nobody accounted for. The fix is not better totals. It’s a layout in which every flow has a line and whatever is left has a name, so the next bug shows up as a residual someone can point at instead of a number that quietly looks fine.


I write about backend reliability, Postgres and data correctness from work on food-tech, healthcare and public-sector platforms. More on what I build and how I work, on why a stored total column drifts from its parts, and on reconciling two systems instead of syncing them. If your report has a “difference” anyone computes by hand, give it a column.