A margin audit, and the leak that was not in the data

· by Krish Kumar
data-engineeringpandasforensicsauditparquet

I was asked a narrow question: where is the margin going? Twelve outlets, a year of transactions, and a gross margin that did not match what the menu costing said it should be.

The assumption going in — mine as much as anyone's — was that the answer would be discounting. It usually is. Someone is giving away too much, and once you quantify it you tighten the policy and recover the difference.

That is not what the data said.

The pipeline

A year of line-item data across twelve outlets is not large by any serious standard, but it is large enough that iterating on a CSV is miserable. The pipeline is staged, and each stage writes Parquet:

raw/       immutable export, never touched again
clean/     typed, deduplicated, canonical outlet and item names
joined/    lines joined to costing and category masters
features/  per-line margin, discount, void and modifier flags

Each stage reads the previous one and writes a new directory. Nothing mutates in place. When — not if — a mapping turns out to be wrong, you fix that stage and re-run forward, and you still have the original to prove what the source actually said.

Parquet rather than CSV for the obvious reason: columnar, typed, and about a tenth of the size, so a stage re-runs in seconds and you stay willing to re-run it. A slow pipeline quietly discourages you from checking your own work.

The canonicalisation stage was the one that mattered most and looked least interesting. The same item was spelled four ways across outlets. Until those collapse into one name, every aggregate downstream is wrong in a way that is invisible — it does not error, it just under-counts.

What came back clean

Price integrity at the till was high. The price charged matched the price on the menu in the overwhelming majority of lines, and the exceptions were explainable — genuine authorised overrides, concentrated in a small number of shifts.

Discounting was within policy. Voids were unremarkable. Wastage was real but roughly what the kitchen already believed it to be.

Each of these was a hypothesis, and each came back negative. That is a result, and it is worth stating plainly rather than quietly moving on, because "we checked and it is fine" is information the business did not previously have about its own controls.

The finding

The leakage was not in any of the categories anyone was watching. It was in a class of transaction that no control existed for at all — not a control that was failing, a control that had never been written. It did not appear in any report, because no report had a line for it. It was not an exception, because nothing had defined it as one.

The reason it took a full pipeline to find is that it was invisible in aggregate. At the level anybody normally looks — daily totals per outlet — it is a rounding error. It only becomes visible when you have every line for a year in one place and go looking for transaction shapes that no rule covers.

I am not going to detail the mechanism, because it is a live operational matter for the business. The transferable part is the method, and it is this: auditing the controls you have will never find the thing you have no control for. You have to enumerate transaction types from the data itself and then ask which of them anything is actually watching. The gap is the finding.

Shipping a finding, not a report

The thing I would do differently in every previous analysis I have written: do not stop at the number.

A document saying "approximately this much is leaking through this mechanism" is read, agreed with, and filed. Nothing changes, because nobody was given anything to run.

So the deliverable was three things:

  1. the quantified finding, with the method written out so it can be challenged
  2. a monitoring specification — what to watch, at what threshold, and who sees it
  3. a runnable control script implementing that specification against live data

The third item is what makes the first two matter. An audit that ends in a PDF describes a problem once. An audit that ends in a script that runs every week is a control, and the next instance gets caught in days rather than in next year's audit.

The general version

Stage your pipeline and keep every stage on disk. Canonicalise before you aggregate, or your aggregates lie quietly. State your negative results — proving the controls work is a finding. And finish by handing over something that runs, not something that is read.