Data Modeling

Header totals repeat at line-item grain when a model stores an order-level amount on every order-line row and then allows users to sum it. The durable fix is to declare two separate grains: a header fact table for one-row-per-order measures, and a line-item fact table for one-row-per-order-line measures. Join them through shared dimensions and degenerate identifiers for analysis, but do not pretend that header measures naturally live at line grain.

The modeling problem: one business event, two valid grains

Many transaction systems store a business document as a header plus detail rows. An ecommerce order has an order header and order lines. An invoice has invoice-level attributes and invoice lines. A shipment has shipment-level facts and shipment items.

The problem is not that either grain is wrong. Both are real. The problem appears when a model flattens them into one table and repeats header measures on every line.

For example, suppose order 1001 has three lines and an order total of 120. If the model stores 120 on each of the three line rows, a simple sum of order total returns 360. The dashboard is not doing bad math. It is faithfully summing a measure placed at the wrong grain.

The right question is: what does one row represent? If one row represents an order line, then line quantity and line extended amount belong there. Order total, order-level discount, shipping charge, and order count usually do not.

The core pattern: separate header facts from line-item facts

The common dimensional pattern is to build two fact tables when the business process has both header-level and line-level measurements.

  • Header fact table: one row per business document, such as one order. It stores measures that are true once per document.
  • Line-item fact table: one row per document line. It stores measures that are true for each line.
  • Shared dimensions: both tables can reference common dimensions such as customer, order date, sales channel, store, region, or salesperson when those dimensions apply at both grains.
  • Document identifier: both tables usually carry the operational document number as a degenerate dimension, such as order number or invoice number, to support drill-through and reconciliation.

This design keeps each additive measure at the grain where it is naturally additive. Users can still analyze the same business process, but the semantic layer or reporting model must choose measures from the correct table.

Modeling rule

If a measure is true once per order, store it once per order. If a measure is true once per line, store it once per line. Do not use repetition as a substitute for allocation.

A hypothetical order schema

Consider a simplified commerce model. The example is hypothetical and intentionally small so the grain decision is visible.

The order header fact has one row per order. It can contain order count, order subtotal, order-level discount, shipping charge, tax amount, and order total. These values describe the whole order.

The order line fact has one row per order line. It can contain quantity, unit price, line gross amount, line discount amount, and line net amount. These values describe the individual product or service line.

Both facts can carry order date key, customer key, channel key, and order number when those concepts apply consistently. The line fact also carries product key because product varies by line. The header fact usually should not carry product key because a multi-product order does not have one product.

Table Grain Example dimensions Example measures Do not store
fact_order_header One row per order Order date, customer, channel, region, order number Order count, order subtotal, order discount, shipping charge, tax amount, order total Product key, product quantity, line net amount
fact_order_line One row per order line Order date, customer, channel, product, order number, line number Quantity, unit price, line gross amount, line discount amount, line net amount Unallocated order total, unallocated shipping charge, unallocated order discount

Worked example: why repeated totals break totals

Suppose order 1001 has three lines. The order-level shipping charge is 9.00 and the order total is 129.00. The line-level net amounts are 40.00, 50.00, and 30.00, for a subtotal of 120.00 before shipping.

If order total is copied onto all three line rows, summing order total by channel returns 387.00 for this one order. If shipping charge is copied onto all three line rows, summing shipping returns 27.00. The model has created accidental multiplication.

In the separated design, the line fact sums to 120.00 for line net amount. The header fact sums to 9.00 for shipping and 129.00 for order total. A reconciliation check can then confirm that line net amount plus shipping equals order total for this simplified example.

The important point is that reconciliation is not the same as storage. You may reconcile header and line facts together, but you should not store a non-allocated header measure on every line and call it additive.

Design Rows for order 1001 Sum of line net amount Sum of shipping charge Sum of order total Interpretation
Repeated header total on line rows 3 line rows 120.00 27.00 387.00 Header measures are multiplied by line count.
Separate header and line facts 1 header row and 3 line rows 120.00 from line fact 9.00 from header fact 129.00 from header fact Each measure is summed at its natural grain.
Allocated shipping on line rows 3 line rows 120.00 9.00 allocated total 129.00 from header fact Line analysis is possible because shipping was deliberately allocated.

Rules for placing measures at the correct grain

A useful rule is to ask whether the measure is still true if you filter to one line. If the answer is no, the measure probably does not belong in the line fact unless it has been allocated.

  • Order count: belongs in the header fact as one per order. In a line fact, counting rows counts lines, not orders.
  • Line quantity: belongs in the line fact because quantity is measured per item or service line.
  • Product revenue: usually belongs in the line fact when it is calculated from line quantities and line prices.
  • Shipping charge: usually belongs in the header fact unless the business defines an allocation method across lines.
  • Order-level discount: belongs in the header fact unless it is allocated to lines using a documented rule.
  • Tax amount: depends on the source process. If tax is calculated per line, it can live at line grain. If it is only known at order grain, keep it at header grain or allocate it explicitly.

This is not a naming convention problem. Renaming order total as line order total does not change its grain.

Question Likely answer Modeling action
Can the measure be summed across order lines without changing its meaning? Yes Store it in the line fact.
Is the measure true only once for the whole order or invoice? Yes Store it in the header fact.
Does the business need the header amount by product or another line-level dimension? Yes Create a documented allocated line-level measure.
Is the value repeated only to make a flat extract easier? Yes Do not expose it as an additive line-level measure.

When allocation is acceptable

Sometimes the business needs to analyze a header-level amount by product, product category, supplier, or another line-level dimension. For example, finance may want to spread an order-level discount across the products on that order.

That can be valid, but it must be modeled as an allocation, not as a repeated total. An allocated measure should have a documented rule, such as proportional allocation by line net amount, equal allocation by line count, or allocation based on weight, volume, or another business driver.

The allocated amount should be named accordingly, such as allocated shipping amount or allocated order discount amount. It should not reuse the same name as the original header amount because the two measures answer different questions.

A good allocation also needs a reconciliation check: the sum of allocated amounts across lines for an order should equal the original header amount, allowing for documented rounding behavior.

Allocation warning

An allocated header amount is a new analytical measure. It should reconcile to the original amount, but it is not the same fact as the original header total.

How to expose the model in BI without inviting mistakes

Separating tables is necessary, but not sufficient. If a BI model exposes both facts carelessly, users can still combine measures and dimensions in ways that create confusing results.

The semantic layer should make the intended usage clear. Header measures should be available with header-compatible dimensions such as order date, customer, region, and channel. Line measures should be available with line-compatible dimensions such as product, product category, and line attributes.

If the BI tool supports relationships between fact tables only through shared dimensions, prefer that pattern over directly joining header facts to line facts in a way that multiplies header rows. If a report needs drill-through from a header metric to lines, use the order number or invoice number as a navigation path, not as permission to sum header measures at line grain.

When a tool allows users to drag any field anywhere, measure names and descriptions matter. Names such as order total, line net amount, and allocated shipping amount are safer than ambiguous names like amount.

BI checkpoint

A direct header-to-line join is useful for inspection, but dangerous as a published aggregate surface unless header measures are protected from summing after multiplication.

Common failure modes

Header and line-item fact tables usually fail in predictable ways. The failure is often hidden until someone slices a metric by a line-level dimension.

  • Flattening everything into one wide fact: convenient at first, but header measures multiply by line count.
  • Using distinct count as a universal fix: distinct count can help with order counts, but it does not fix repeated additive amounts like shipping or order total.
  • Joining header to line facts before aggregation: a query that joins one header row to many line rows and then sums header measures will repeat the header values.
  • Hiding grain in metric definitions: a metric that works only because of a complicated expression is fragile if users build their own reports.
  • Mixing allocated and original measures: allocated shipping by product and original shipping by order are both useful, but they should not be treated as the same measure.
  • Ignoring null or partial headers: some operational systems create lines before final header totals are known. The model should define how incomplete documents are filtered or labeled.

Diagnostic checks before publishing

Before publishing a header and line-item model, run a few simple checks. These checks are not a replacement for business review, but they catch many grain errors early.

  1. One-row-per-header test: confirm that the header fact has exactly one row per order number, invoice number, shipment number, or equivalent document key.
  2. One-row-per-line test: confirm that the line fact has exactly one row per document line key, not one row per document.
  3. Header multiplication test: join header to line detail for a small sample and verify that summing header measures after the join produces a larger number. This demonstrates why the joined detail should not be the published additive model.
  4. Reconciliation test: for a documented sample, compare the sum of line-level components to the relevant header total where the business rules say they should reconcile.
  5. Dimension compatibility test: list which dimensions are valid for each measure. If order total is shown by product, decide whether that is prohibited or represented by an allocated measure.
  6. Metric naming test: verify that original header measures and allocated line measures have different names and descriptions.

A practical design checklist

Use this checklist when modeling any parent-child transaction.

  • Declare the header grain in one sentence.
  • Declare the line-item grain in one sentence.
  • Place each measure at the lowest grain where it is naturally true and additive.
  • Do not copy header totals onto line rows unless the value is explicitly non-additive or clearly labeled for reference only.
  • If a header amount must be analyzed by line-level dimensions, create an allocated measure with a documented allocation rule.
  • Keep original and allocated measures separate.
  • Use shared dimensions to compare header and line metrics, not direct joins that multiply header measures.
  • Publish reconciliation checks so analysts know how the two facts tie together.
  • Document which dimensions are valid for each metric.

The goal is not to make the model more complex. The goal is to keep simple sums honest.

Key takeaways

  • Header and line-item fact tables solve a grain problem, not a tooling problem.
  • Header measures such as order total, shipping charge, and order-level discount should not be copied onto every line as additive facts.
  • Line measures such as quantity and line net amount belong at line-item grain.
  • If a header amount must be analyzed by product or another line-level dimension, create a clearly named allocated measure with a reconciliation rule.
  • The semantic layer should protect users from summing header measures after joining them to line detail.

Next step

Pick one existing order, invoice, or shipment model and label every measure as header grain, line grain, or allocated. Then run a small reconciliation sample to prove that header totals are not being multiplied by line count.

Recommended next reads