Theory · Page 6 of 34 · Modelling

6. Dimensional modelling: facts, dimensions, grain and slowly changing dimensions

A dimensional model organises data for reading: numbers about a business event in a fact table, the who, what, where and when around it in dimension tables. Get the grain right first; then decide how each dimension keeps its history.

A star schema forming, then a customer's history kept
Playback speed

The clip builds Harbour Mutual's claims star in Kimball's four steps. First the business process, a claim being reported. Then the grain, one row per claim. Then the dimensions around it, the date, the customer and the product, and the fact table in the middle with its measures, joined to each dimension. Then a customer moves to another country: type 1 overwrites the country, type 2 keeps the old row and adds a new one with its own surrogate key and validity.

The intuition: a business event and its context

Every morning Harbour Mutual's claims director asks the same kinds of question. How much did we incur last month, by product line? By region? For new customers only? Each question has a number (an amount, a count) and some ways of slicing it (by month, by line, by region).

A dimensional model is shaped like those questions. The numbers go in a fact table: one row per claim, with its paid, reserved and incurred amounts. The ways of slicing go in dimension tables around it: a date dimension, a customer dimension, a product dimension. Drawn, it looks like a star, which is why it is called a star schema.

Where the logical model (page 5) splits facts apart so each is written once, the dimensional model brings them back together so each question needs few joins. The product's line is repeated on every product row on purpose: the director filters by line without knowing that lines are a table of their own in the source.

Remember A fact table holds the measurements of one business event at one declared grain; dimension tables hold the context to filter and group them by.

The theory: four steps, grain, facts, dimensions and history

Ralph Kimball and Margy Ross describe dimensional design in four steps, always in this order: (1) choose the business process, an event the organisation measures, such as reporting a claim or collecting a premium; (2) declare the grain, exactly what one fact row represents; (3) identify the dimensions, the descriptive context true at that grain; (4) identify the facts, the numeric measurements true at that grain.

The rules of grain Declare the grain before choosing any dimension or fact, in business words: "one row per claim", "one row per claim payment", "one row per policy per month". Prefer the atomic grain, the lowest level the source records, because it answers questions nobody has asked yet; summaries can always be built from it. Keep one grain per fact table: a dimension or fact that is not true at the declared grain belongs in another fact table. Mixing grains double-counts.

Mixing grains is the most common dimensional mistake, and it is silent. Join policies to their claims and sum the annual premium: each policy's premium is counted once per claim, and policies with no claim drop out. On the seed, the premium at the policy's grain totals €475,275; after the join to claims it totals €351,120. Both queries run without an error; only one answers the question.

The annual premium at the policy's grain, and after a join to claims changes the grain (PostgreSQL)
SELECT
    (SELECT SUM(annual_premium) FROM policies) AS premium_at_policy_grain,
    (SELECT SUM(p.annual_premium)
     FROM policies p
     JOIN claims c ON c.policy_id = p.policy_id) AS premium_after_join_to_claims

Kinds of fact table. A transaction fact table has one row per event (each claim, each payment). A periodic snapshot has one row per thing per period (each open claim at each month end). An accumulating snapshot has one row per process instance, updated as it passes milestones (a claim's notified, assessed, settled and closed dates). A factless fact table records that something happened, with no measure (which policies a broker reviewed).

Kinds of fact. An additive fact can be summed across every dimension: paid amounts. A semi-additive fact can be summed across some dimensions but not time: the open reserve at month end can be summed across claims, but adding January's reserve to February's means nothing. A non-additive fact cannot be summed at all: a loss ratio must be computed from summed parts (page 9).

Dimensions. Each dimension row has a surrogate key made by the warehouse, so the dimension can keep history and survive changes in the source's keys. A conformed dimension has the same meaning and keys in every fact table that uses it, so facts from different processes can be compared; Kimball's bus matrix lists business processes against the dimensions they share and is the warehouse's plan. A role-playing dimension is one table used in several roles: the date dimension as loss date and as reported date. A degenerate dimension is a key with no table of its own, such as the claim number kept on the fact row. A snowflake normalises a dimension into several tables; Kimball advises against it, because it costs joins and buys little.

Slowly changing dimensions. Customers move, products are renamed, brokers change region. A slowly changing dimension (SCD) is a rule, chosen per attribute, for what happens to the dimension when its source changes. Kimball and Ross number the techniques:

The slowly changing dimension types compared
TypeWhat happens on a changeHistoryUse it when
0Nothing: the original value is keptThe original onlyThe value must never change, such as a customer's original start date
1The value is overwrittenNoneCorrections, or when only the current value matters
2A new row with a new surrogate key and validity dates; the old row is closedFullFacts must be reported as things were at the time
3A column holds the previous value beside the current oneOne step backA one-off change people want to compare across, such as a region reorganisation
4Fast-changing attributes move to a mini-dimension of their ownFull, in the mini-dimensionAttributes such as an age band or a risk score that change too often for type 2
5A type 4 mini-dimension, plus its current key kept on the base dimension as type 1Full, plus the current profileReporting by the current profile and the historical one
6Type 2 rows, plus a current-value column overwritten on every row (1 + 2 + 3)Full, plus the current valueFacts by the value at the time and by today's value, side by side
7Both a type 2 key and a durable key on the fact, joined to type 2 and type 1 viewsFull, plus the current valueThe same as type 6, chosen by the query rather than stored in the row

Type 2 is the workhorse. Its rows carry a validity range (valid_from, valid_to) and usually a current flag; a fact row is joined to the dimension row whose surrogate key was current when the event happened, so last year's claims stay with the region the customer lived in last year. Chapter 27 implements types 1 and 2 with Delta Lake's MERGE.

Why it matters

Harbour Mutual's warehouse is already a star schema, and its board figures are only as right as its grain. A premium copied onto every claim row, or a reserve summed after a join to payments, changes the loss ratio without a single error message.

The SCD choice decides what history the business can see. If the customer dimension overwrites the region (type 1), the regional loss ratio for 2024 changes every time a customer moves, and last year's board pack can never be reproduced.

Where it goes wrong

  • No declared grain. "One row per claim, mostly" ends in double counting.
  • Facts at the wrong grain. A policy's premium copied onto every claim row is summed once per claim.
  • Unconformed dimensions. Two customer dimensions with different keys make claims and premiums impossible to compare.
  • Type 2 everywhere. Tracking history on every attribute multiplies rows; choose per attribute, with the business.
  • Summing semi-additive facts over time. Twelve month-end reserves added together are not a year's reserve.

Check yourself

Choose an answer to each question, then press Check.

1. Which comes first in Kimball's four steps, before any dimension or fact is chosen?
2. A customer moves region and last year's claims must stay with the old region. Which SCD type?
3. Which fact is semi-additive?
Answer every question first.

Sources

The works this page draws on, summarised in its own words rather than quoted. Read them for the full argument.

  • R. Kimball and M. Ross, The Data Warehouse Toolkit: The Definitive Guide to Dimensional Modeling, 3rd edition (Wiley, 2013)The four-step design process, the grain rules, fact table types, additivity, conformed dimensions and the bus matrix, and the slowly changing dimension techniques 0 to 7.
  • R. Kimball et al., The Data Warehouse Lifecycle Toolkit, 2nd edition (Wiley, 2008)Dimensional modelling within a warehouse programme, and the bus architecture.

Practise it

The chapters that put this theory to work on Harbour Mutual and Kestrel:

The tools to try it in:

  • Model studio · Draw conceptual, logical and physical models, generate DDL for PostgreSQL, Spark SQL and T-SQL, and run it on Harbour Mutual's data in your browser.

Terms taught here: Star schemaFact tableDimensionGrainSlowly changing dimension

An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Cloud Data Architecture Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.