Skip to content

Fact Table Grain Explained: The Decision That Shapes a Warehouse

A central fact table with each row linked to surrounding dimension tables.

Before you design a single column of a fact table, you have to answer one question: what does one row represent? That answer is called the grain, and almost every later modeling problem, from double-counted revenue to impossible joins, traces back to getting it wrong or leaving it vague.

What a fact table holds

In a dimensional model, a fact table records events or measurements, such as sales, shipments or page views. Each row holds numeric measures, such as quantity and amount, plus keys that link to dimension tables describing the who, what, when and where. The overall layout is the one compared in star schema vs snowflake schema.

What grain means

The grain is a precise statement of what one row stands for. “One row per order line item” is a grain. “One row per customer per day” is a different grain. “Sales data” is not a grain, because it does not say whether a row is an order, a line, a payment or a daily total. Write the grain as a single sentence before choosing any columns.

Why declaring it first matters

Every column you add must be true at the declared grain. A measure such as order shipping cost belongs to the order, not to each line item, so putting it on a line-item row repeats it for every line, and summing it overcounts. Dimensions follow the same rule: only attach a dimension if each row has exactly one value for it. Many surprising totals in dashboards come from a fact table that quietly mixes grains.

Choose the finest grain you can

A common guideline is to store data at the lowest level of detail available, such as individual transactions. You can always summarize detailed rows into daily or monthly totals, but you cannot recover detail from a summary. Coarser summary tables can be built on top for speed, as in the gold layer of medallion architecture.

Three common kinds of fact table

  • Transaction fact table. One row per event, such as each sale. It is the most detailed and the most common.
  • Periodic snapshot. One row per entity per period, such as each account’s balance at month end. It suits measuring a level over time.
  • Accumulating snapshot. One row per process instance, updated as it moves through stages, such as an order moving from placed to shipped to delivered.

Additive and non-additive measures

An additive measure, such as sales amount, can be summed across every dimension. A semi-additive measure, such as an account balance, can be summed across customers but not across time, because adding balances from different days makes no sense. A non-additive measure, such as a ratio or a percentage, should not be summed at all, and it is better to store its numerator and denominator and compute the ratio at query time.

Grain and joins

When you join two fact tables of different grains, each row on the coarser side repeats for every matching row on the finer side, and totals inflate. Aggregate each fact table to a shared grain before joining, a trap described in SQL joins explained, and check your totals with the grouping rules in GROUP BY and HAVING.

Dimensions and history

The dimensions around a fact table change over time, and you must decide how to keep that history, using the patterns in slowly changing dimensions. The deliberate duplication in a dimensional model is the trade-off discussed in normalization vs denormalization.

A quick checklist

  1. Can you state the grain of each fact table in one sentence?
  2. Does every column in the table hold exactly one value per row at that grain?
  3. Are you storing the finest level of detail you can?
  4. Are non-additive measures stored as parts, not ratios?
  5. Do you aggregate to a shared grain before joining fact tables?

If you are designing a warehouse model and want a second opinion, see my cloud data warehousing service.

FAQ

What is grain in a fact table?
It is the definition of what one row represents, such as one row per order line item or one row per customer per day.

Why should I declare the grain first?
Every measure and dimension must be consistent with it. Declaring it first prevents mixed grains, which cause double counting and incorrect totals.

What is a semi-additive measure?
It is a measure that can be summed across some dimensions but not others, such as an account balance, which can be added across customers but not across time.

Related posts

Leave a comment

Your email address will not be published. Required fields are marked *