How would you model order-line sales in a data warehouse?

Instruction: A shop needs total units, net sales, and distinct order counts by month and product. Define the fact-table grain, measures, dimension keys, and reconciliation checks. Use an order with multiple lines to explain how you avoid double counting.

Context: Tests basic dimensional modeling: declaring fact grain, choosing additive measures, preserving dimensional history, and reconciling analytical totals to source orders.

Updated

Example answer

Declare the grain first: one fact row per order line, identified by (order_id, line_number). A single order containing three products has three fact rows. Do not mix order-level totals into line-level measures and then sum them, because that counts an order multiple times.

Use a sales_line fact containing the source order and line identifiers, order date key, product key, customer key, quantity, unit price, discount amount, and line net amount. Define currency and the treatment of tax, shipping, cancellations, and returns. For this exercise, net amount is quantity × unit_price − line_discount, in one currency, excluding tax and shipping. Returns would need an explicit policy before inclusion.

Join to product, customer, and date dimensions through keys that are unique in the referenced dimension. To report a customer or product as it was at purchase time, retain the applicable dimension version and connect each fact to that version; overwriting a category silently restates history.

Consider order A with two lines: two units at 10 each with a line discount of 2, and one unit at 5 without a discount. The two line amounts are 18 and 5, so net sales total 23 and units total 3. Counting fact rows gives two lines; COUNT(DISTINCT order_id) gives one order. An average unit selling price across lines is total net amount divided by total units, not the unweighted mean of line prices.

Validate that the order-line key is unique, dimension joins preserve the fact-row count, and reconciled sums match the source under the same revenue definition. Reloading the same source lines must not duplicate sales. This is a modeling and reconciliation task, distinct from defining OLTP and OLAP.

Related Questions