Case study
Daily Margin Brief
AI-assisted root-cause analysis of a reported margin anomaly, and a back-tested two-check design to turn the next pricing error into a morning alert before the reports are read.
Maturity: ConceptProblem
A manager asked a direct question: why did one region’s month-to-date margin look so far out of line with the others? The reporting pipeline was working correctly. It reported what the ERP system had posted. The real question was how to explain the number quickly, prove the explanation, and catch the next error of this kind before anyone read the reports.
This case describes the method. Client details and the incident’s own figures are withheld. The numbers below describe the design, the proof of concept or a synthetic demonstration.
Approach
I worked with an AI coding agent on read-only data. The agent wrote and ran the queries; I framed the question, checked every result against the source, and made the decisions.
- Reproduce. Rebuild the reported number exactly, from the source.
- Decompose. Break it down from region to product to sales order, until one line explains the change.
- Trace. Follow that line through the data warehouse, the general ledger, the ERP pricing conditions and the change log.
- Verify. Build a refreshable workbook so a manager can check every figure independently.
The trace ended at a single price-entry error in the ERP system, found in one working session.
Detection design
To catch the next error before the reports are read, the design uses two plain-SQL checks that ask different questions:
- Check A, overnight snapshot diff: what changed most since last night? Compare tonight’s snapshot with last night’s, net the change per sales order, and rank the changes above a dollar floor.
- Check B, price plausibility: is this unit price believable? Divide net revenue by base units and compare the result with the product’s 90-day median price. Flag lines at 10 times the median or more, above a dollar floor.
In back-testing on nightly snapshots, both checks ranked the error first on the first morning it was visible. The overnight check later found a second, different error: a quantity keyed in the wrong unit of measure.
Why the median, not the mean
Prices are skewed, with a long right tail. A single extreme line inflates the standard deviation, so an average-based limit moves up and hides smaller errors. On a synthetic set of price lines, one bad line moved the mean-plus-3-SD limit from 4.0 to 65 times the median price. The median-based limit stayed at 1.4 times. The design therefore moves the line-level check to the median and MAD on a log scale, with a dollar floor.
Which control chart for which error
Errors do not all look alike, so I also compared three control charts on a synthetic daily series and matched each to an error pattern. A Shewhart chart compares each day with fixed limits, so it flags a large one-day jump. EWMA and CUSUM accumulate evidence across days, so they flag a small shift that lasts, which a Shewhart chart misses. The limits follow the NIST/SEMATECH e-Handbook of Statistical Methods.
Published precedent
The design follows the loop of a published production system for price anomalies (Ramakrishnan et al., Walmart Labs, 2019, arXiv:1902.09566): detect, rank by dollar impact, explain, and correct. That system retrains machine-learning models on reviewed alerts. This design starts without machine learning and logs every review with a reason, so labelled history exists before any model is trained.
Proof of concept
I directed the agent to generate a Power BI project as code: a PBIP project with a TMDL semantic model, 8 tables and more than 60 DAX measures, with robust control limits computed in DAX and three plain statuses (Investigate, Watch, Expected). I presented it to management, who approved it to move to a build. It is not in production.
Human governance
- Read-only access to production data throughout.
- Every number reproduced from the source before it reached a reader.
- A signal starts an investigation; it never blocks a posting.
What is not proven yet
The thresholds are judgement and have not been tuned. The back-test covers two incidents, and the false-alarm rate has not been measured. The pilot is designed to answer these, with success measured by errors detected before reporting, alerts per day, the share explained, the false-alarm rate and review minutes.
Lessons
- Reproducing the reported number exactly, before explaining it, was what made the explanation credible.
- Choosing the statistic is a design decision. Robust methods suit skewed price data, and a dollar floor decides what matters.
- An AI agent made the legwork fast. The value came from the question, the checks on its output, and the decision about what to build.
