What really happened
Theo added refunds to the revenue model so it would report net revenue. Refunds should make revenue go down. Revenue went up.
The join from orders to refunds ran on order_id, and one order can have several partial refunds. An order with three refunds matched three times, so its total was added three times: $600 × 3 − $100 = $1,700 instead of $500.
Nothing failed, because nobody had written down what one row of the revenue model means. The payments system disagreed by $380K, and that is how it was found.
The fix: sum the refunds per order first, join one to one, and add a test that the model has exactly as many rows as the orders table.
The lesson: Know the grain on both sides.
Run the 112% bug
Net revenue is gross minus refunds, one row per order. Some orders have several partial refunds. Pick what the orders join to, then run it. The query runs in your browser on DuckDB with 2,000 synthetic orders and 616 refunds.
with refunds_per_order as (
select order_id,
sum(amount) as amount
from stg_refunds
group by order_id
)
select o.order_id,
o.gross_amount
- coalesce(r.amount, 0)
as net_amount
from stg_order_totals as o
left join as r
on r.order_id = o.order_idSource, tests and the agent skill: github.com/Kevin-yyuan/dbt-reconcile
A new bug every episode.
Go deeper
- The code: dbt-reconcileThe tests that catch this bug, plus an agent skill that runs the review. On GitHub.
- Episode 1: Revenue dropped 63% overnight. Every test passed.Count against the source.
- Episode 2: Yesterday's sales changed overnight. Nobody touched anything.Define the day once. Close it on purpose.





