Shipping revenue from an order-line fact is overstated — find the modelling flaw
fct_order_line is declared at one row per order line, so analysts can slice revenue by product. Finance reports that monthly shipping revenue, SUM(shipping_fee) over this fact, is about 2.7 times the billing system's figure, while goods revenue matches billing exactly.
Requirements:
- name the cause;
- give SQL that finds the affected orders and proves the totals do not reconcile;
- fix the model so both totals match billing and product-level analysis still works.
INSERT INTO fct_order_line
(order_id, line_no, product_key, order_date_key, line_amount, shipping_fee)
SELECT l.order_id, l.line_no, l.product_key, o.order_date_key,
l.qty * l.unit_price, o.shipping_fee
FROM src_order_lines l
JOIN src_orders o ON o.order_id = l.order_id;Find and fix the bug.
The grain is one order line, but the order-level fee sits on every line, so SUM counts it once per line. Find orders whose lines repeat one fee, check totals against billing; allocate the fee to lines (remainder on one) or use an order-grain fact.
- Patching the report with
SUM(DISTINCT fee), which merges different orders that happen to share a fee - Allocating with rounding and no remainder line, so the allocated shares no longer add up to the order's fee
- Picking dimensions and measures before declaring what one fact row means
- Allocate by line value or by weight — how do you choose, and who signs it off?
- When is a separate order-grain fact better than allocating the fee to lines?
The grain is declared as one row per order line, but shipping_fee is an order-level measure. The join to src_orders copies the fee onto every line, so a three-line order contributes its fee three times to SUM(shipping_fee). Goods revenue reconciles because line_amount really belongs to the line. An average of about 2.7 lines per order (weighted by fee) gives the 2.7× overstatement.
Find the affected orders and prove the mismatch:
-- orders whose fee is repeated on every line
SELECT order_id, COUNT(*) AS lines, MIN(shipping_fee) AS fee
FROM fct_order_line
GROUP BY order_id
HAVING COUNT(*) > 1
AND MIN(shipping_fee) = MAX(shipping_fee)
AND MIN(shipping_fee) > 0;
-- reconcile with the source: a correct model shows no difference
SELECT (SELECT SUM(shipping_fee) FROM fct_order_line) AS fact_fee,
(SELECT SUM(shipping_fee) FROM src_orders) AS source_fee;Fix 1 — allocate the fee across lines in proportion to their value. Rounded shares need not add up to the original fee, so the whole remainder goes to the order's first line:
WITH lines AS (
SELECT l.order_id, l.line_no, l.product_key, o.order_date_key,
l.qty * l.unit_price AS line_amount,
o.shipping_fee,
SUM(l.qty * l.unit_price) OVER (PARTITION BY l.order_id) AS order_amount,
ROW_NUMBER() OVER (PARTITION BY l.order_id ORDER BY l.line_no) AS rn
FROM src_order_lines l
JOIN src_orders o ON o.order_id = l.order_id
), shares AS (
SELECT *,
COALESCE(ROUND((shipping_fee * line_amount / NULLIF(order_amount, 0))::numeric, 2), 0) AS fee_share
FROM lines
)
INSERT INTO fct_order_line
(order_id, line_no, product_key, order_date_key, line_amount, shipping_fee)
SELECT order_id, line_no, product_key, order_date_key, line_amount,
fee_share + CASE WHEN rn = 1
THEN shipping_fee - SUM(fee_share) OVER (PARTITION BY order_id)
ELSE 0 END
FROM shares;If every line of an order is free (order_amount = 0), all shares are zero and the first line takes the whole fee, so the total still reconciles. The allocation rule (by value, by weight) is a business decision to agree with finance.
Fix 2 — drop shipping_fee from the line fact and add fct_order at one row per order. The fee can no longer be sliced by product, but nothing is invented. The general lesson: declare the grain first, then choose dimensions and measures, and check every measure against that grain.