Q&A-1Define grain for an orders semantic model.
Answer
Grain is what one row in a fact table represents. For an orders model, the first decision is whether that row is an order header, an order line, a shipment, a payment, or a daily snapshot. I'd pick order line as the base grain for sales, because price, discount, tax, and product are determined at the line. Order header becomes a dimension or a separate fact at order grain. Shipments and payments get their own facts at their own grains.
Assume a retail domain: one order has many lines, lines can ship in separate shipments, payments can be partial, refunds can be partial. We need order count, units sold, gross revenue, net revenue, and on-time delivery.
Worked example, hypothetical order #1001:
| entity | grain | rows for #1001 | key |
|---|---|---|---|
| order header | one row per order | 1 | order_id |
| order line | one row per line | 3 | order_id, line_id |
| shipment | one row per shipment | 2 | shipment_id |
| payment | one row per payment | 1 | payment_id |
Metrics at line grain: order count = COUNT(DISTINCT order_id) = 1. Units sold = SUM(quantity) = 5 (lines: 2, 2, 1). Gross revenue = SUM(line_amount) = $120. If you used order grain and stored order_total = $120, you get the same revenue, but you cannot get units by product. If you used line grain and did COUNT(*) for orders, you get 3, which is wrong.
Trade-offs. Order grain is simpler, fewer rows (1x), faster for order dashboards, but cannot answer product or fulfillment questions. Line grain is 3.2x rows on average (hypothetical), more storage, but supports product margin and returns. Shipment grain is needed for on-time delivery; if you only have order grain, you cannot compute partial shipment delays. Deciding criteria: what is the most granular question you must answer? If you need product-level returns, line grain. If you only need daily order counts, order grain. When wrong: using order grain for line-level metrics forces a join to a line table and aggregation, which can double-count if the join fans out; using line grain for order count without DISTINCT overcounts by lines per order.
Failure modes. Duplicate rows from retries or late-arriving updates. Detection: uniqueness test on (order_id, line_id) should return zero duplicates. If it fails, you have a fan-out or duplicate load. Operational cost: a 2x duplicate on 320M rows doubles reported revenue until backfilled. Another failure: mixing grains in one table causes metric reconciliation failures. Order count from line fact vs order fact differ by lines per order. Detection: reconcile order count between facts; if line fact count is 3.2x order fact, that's expected, but if you expected 1:1, you have a grain mismatch.
Event and processing times: at line grain, event time is the order-placed timestamp, processing time is ingestion timestamp. Late updates: if a line is added after order placement, you need to handle it; at order grain, you might miss it. That is a grain choice consequence, not a test checklist.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.