IFan invoice has no matching paymentTHENflag it and rank it by money at risk.
Automated billing reconciliation and anomaly detection. A rule-based reconciliation engine for high-volume logistics billing. It matches about 99,000 invoices and payments, flags the billing anomalies that quietly erode margin, and ranks every exception by € at risk. Includes an exploratory machine-learning extension for anomaly detection.
My role. Developed with an AI-assisted workflow: I defined the business logic, reconciliation rules and validation criteria, and used AI tools to write the Python implementation.
The data. A synthetic dataset modelled on billing and logistics operations, with no proprietary information. Fully reproducible from a fixed seed: code on GitHub.
Reconciliation is manual, slow, and leaks margin
A logistics operation issues tens of thousands of invoices a month. Matching them to incoming payments is a spreadsheet-driven process, so billing errors slip through: duplicate invoices, wrong rates, misapplied fuel surcharges, short payments, unapplied cash. Each erodes margin or inflates days sales outstanding, and at volume nobody can review them all by hand.
What I built
An end-to-end, one-command pipeline
A synthetic but realistic dataset (500 customers, 12 months, about 3% of transactions seeded with six real-world error types as hidden ground truth), a SQL reconciliation core, an Isolation Forest anomaly layer, and an evaluation of both against that ground truth.
The six anomaly types
Injected at about 3%, held out as ground truth
Rate discrepancy, duplicate invoice, fuel-surcharge error, short or over payment, unapplied cash, overdue. Plus deliberately dirty data (mixed date formats, noisy customer keys, swapped currency labels) so the cleaning stage is genuine.
Most of the logic lives in SQL; machine learning closes the gap
Cleaning, exact and fuzzy invoice-to-payment matching, duplicate detection via window functions, and rate-card revalidation are all expressed in SQL. Python handles data generation, the anomaly model and evaluation.
- GeneratePython and Faker, seeded. Rate cards, terms, dirty data, hidden labels.
- CleanNormalise keys, fix currency labels, parse mixed date formats.
- MatchExact on reference, then fuzzy by customer and amount (window rank).
- RulesDuplicates, rate revalidation, short or over pay, unapplied cash.
- ModelExploratory: an Isolation Forest scores each invoice for subtle anomalies.
- RankExceptions register with aging, severity and € at risk, fed to Power BI.
Data model
Four reference and transaction tables, plus a hidden ground-truth table.
Rules are precise; machine learning recovers what rules cannot see
Because every anomaly was seeded, detection could be measured exactly. Three systems compared against the same ground truth.
Detection performance
| System | Precision | Recall | F1 |
|---|---|---|---|
| Rules only | 99.4% | 83.3% | 0.91 |
| Machine learning only | 18.8% | 19.0% | 0.19 |
| Rules and machine learning | 53.2% | 93.8% | 0.68 |
Combining them lifts recall from 83% to 94%. Precision falls because the model adds candidates a reviewer triages in € order: a fuller net, not a cleaner one.
Recall by anomaly type
Where each layer earns its keep.
Five of the six anomaly types are shown. Overdue invoices occur naturally rather than being seeded, so there is no ground truth to measure recall against; the aging rule flags them in the exceptions register.
The insight. The rule engine hits 99% precision but is structurally blind to one whole error class: subtle fuel-surcharge mistakes sit under its fixed 2% threshold, giving 0% recall. The Isolation Forest, learning the normal shape of an invoice, recovers 63% of them. Machine learning here complements business rules, it does not replace them. Rules give explainability and precision on known patterns; the model extends coverage to the unknown ones.
Reconciliation Control Tower
The pipeline feeds a two-page Power BI report. Executives see exposure and trend; operations drill into a ranked, filterable exception queue with a model-performance panel. The numbers shown are the live pipeline output.
Tooling and reproducibility
SQL
Cleaning, exact and fuzzy matching, window-function duplicate detection, rate-card revalidation, exceptions register.
Python
pandas and Faker generator with a fixed seed; date parsing; orchestration in one script.
scikit-learn (exploratory)
Isolation Forest on per-invoice features: rate deviation, surcharge ratio, payment behaviour, peer z-scores.
Power BI
Two-page Control Tower over the exported exceptions, trend and KPI feeds.
One command, fully reproducible. A single run_pipeline.py regenerates the data, runs the SQL layer, scores the model, evaluates against ground truth, and exports the dashboard feeds, deterministically, in about 16 seconds.
Related case study. The same decision-logic mindset applied on the job: an internal application that automated billing-credit approvals, cutting turnaround from about 2 business days to 2–3 minutes. Read the auto-approval case study.
Next case study: Pullmo, a product in pilot for coach hire companies IF a route is entered THEN price it, itemise it, and lock what the customer accepted.