Q&A-2Define and enforce fact-table grain for a new analytical product.
Answer
The 90-second answer
Grain is the sentence that says what one row means, and it gets written before the columns. For a new analytical product I'd say it out loud: "one row per order line, current version only, keyed by (order_id, line_id), event time in UTC." Then I check that sentence against the source. Does the order service actually emit one row per line, or one per line per status change?
Assumptions I'd state up front: the source is append-only, corrections arrive as new versions, and the product needs line-level revenue rather than order-level.
Then I test every measure against that sentence. line_amount is at line grain, fine. shipping_cost is charged once per order, so it either gets allocated or lives in a separate order-grain fact. customer_lifetime_value is coarser still: separate model. Enforcement is where most teams stop; a declared grain with no test is a comment.
Worked example: catching fanout
Staging holds 3 order lines: 100.00, 140.00, 1000.00. 1240.00 total.
-- fct_order_lines: one row per (order_id, line_id)
select order_id, line_id, count(*) as c
from analytics.fct_order_lines
group by 1, 2
having count(*) > 1;Trace: the model joins staging to dim_product on product_key. dim_product is SCD2 and holds two rows for that key, the pre- and post-price-change versions. The join isn't filtered to is_current, so each of the 3 lines matches 2 dimension rows and the fact gets 6 rows. The having clause returns all 3 keys. The control total confirms it: sum(line_amount) is 2480.00 against 1240.00 in staging, exactly 2×. Fix by filtering dim_product.is_current or joining on a versioned key, then re-run the count.
Enforcement in dbt
uniqueandnot_nullon the surrogate key, ordbt_utils.unique_combination_of_columnson[order_id, line_id]when the key is composite.relationshipsfromfct_order_lines.product_keytodim_product.product_key, to catch orphaned keys.- A source-to-target count per
event_datepartition, so a late-arriving line landing in an already-loaded partition shows up instead of vanishing. - A daily control total on
line_amount.
Failure modes
Fanout from an unfiltered SCD2 join: caught by the duplicate-key test above. Late-arriving rows: caught by per-partition count reconciliation. Grain drift, where someone adds a promotion column that is one row per promotion per line: caught when a uniqueness test that used to pass starts failing. Silent redefinition, where a MERGE changes what a row means without a version bump: caught by comparing row counts and control totals before and after deploy.
Unknown, not-applicable, corrections
Unknown customer becomes a real customer_key = -1 row in the dimension, not NULL, so the relationships test passes and joins don't drop rows. Not-applicable (shipping on a digital line) is 0.00 with a flag, or NULL with a documented meaning: pick one and test it. Corrections are either a new version row with updated_at or an upsert on the natural key; state which, because it decides whether uniqueness is tested on the natural key alone or key plus version.
Allocation
Shipping of 12.00 on that order, allocated by line share: 0.97, 1.35, 9.68. Sums to 12.00. Name the column shipping_cost_allocated so nobody sums it against the order-level figure.
Contract and monitoring
Expose grain, owner and two example rows in the model contract and semantic layer, so a BI user reads "one row per order line" before writing a query. In production, alert on the uniqueness test failing and on daily control-total drift past a threshold. When grain genuinely changes, version the model (fct_order_lines_v2) or migrate with a documented impact list rather than editing the definition in place.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.