EN

When a join turns $120 of orders into $220

A left join to refunds counts a $100 order twice. Reproduce the SQL fanout, trace it to the affected order, and fix it at the right grain.

Two orders total $120.00. After a left join to refunds, the same order amounts add up to $220.00. Order 101 has two refunds, so the query adds its $100.00 order amount twice. The order IDs were unique in orders, and the query ran successfully.

Where the extra $100 came from

Here is the synthetic input. Each row in orders represents an order. Each row in refunds represents a refund event, and an order can have several.

Order ID Order total Matching refund events
101 $100.00 r1, r2
102 $20.00 r3
select sum(o.order_total) as gross_order_total
from orders as o
left join refunds as r on o.order_id = r.order_id;

The joined query sees three order amounts: $100.00, $100.00, and $20.00. That multiplication is join fanout. The diagnostic I wanted in a code review was the specific join and key where it started.

Find the key with Metric Fanout Audit

Metric Fanout Audit takes CSV snapshots and a TOML file that declares the fact key, metric, and joins. The fixtures are in the Metric Fanout Audit repository. Run the demo with Python 3.11 or later and uv:

git clone https://github.com/fmanrique8/metric-fanout-audit.git
cd metric-fanout-audit
uv run --locked fanout-audit check examples/orders-refunds/fanout.toml
FAIL  first fanout: orders -> refunds
order_total: 120.00 -> 220.00 (delta +100.00)
rows: 2 -> 3
FANOUT  orders -> refunds  rows 2 -> 3  order_total 120.00 -> 220.00
  order_id='101': 2 right matches, rows 1 -> 2, delta +100.00

Those two matches tell the reviewer where to look. For a left join, zero matches still produce one output row. Two matches produce two. The CLI counts matches per right-hand key and tracks how many times each original fact appears after every join. That gives it the exact sum and row count for these declared joins without materializing the wider result. The tests verify the example against a real SQLite join.

Fix the join at the order grain

If the report needs a refund count alongside each order, roll up the refunds first. Each order_id can then match at most one right-hand row.

with refunds_by_order as (
    select order_id, count(*) as refund_count
    from refunds
    group by order_id
)
select
    sum(o.order_total) as gross_order_total,
    sum(coalesce(r.refund_count, 0)) as refund_events
from orders as o
left join refunds_by_order as r on o.order_id = r.order_id;

Run the same audit with the grouped refund fixture:

uv run --locked fanout-audit check examples/orders-refunds/repaired.toml

The repaired demo reports 2 rows and $120.00. The query also keeps the count of 3 refund events. SUM(DISTINCT order_total) would deduplicate dollar values and undercount two different orders with the same price. Grouping the right table by order preserves each order’s contribution.

Two wrong rows can cancel out

Two adjustments of +100.00 and -100.00 each match two label rows. The sum is 0.00 before and after the left join. In SQLite, the same query counts 2 rows before and 4 after. The audit flags both duplicated keys, with changes of +100.00 and -100.00, and exits 1. Runnable fixture

uv run --locked fanout-audit check examples/signed-adjustments/fanout.toml

This check covers additive measures from the base table and declared left equality joins on local inputs. If a report intentionally works at refund-event grain, decide how to aggregate the order metric at that grain. In Looker, accurate primary keys and relationship settings let symmetric aggregates account for fanout. Looker’s relationship guidance

This is a companion to When dbt tests pass and revenue is still wrong, which traces a separate way to get a wrong total from a table whose keys pass validation.