Skills / Data pipelines and data engineering / Modelling data for analysis: facts and dimensions

Data pipelines and data engineering, lesson 3 of 8

Modelling data for analysis: facts and dimensions

Application databases are designed for running the app: saving an order quickly, updating a profile. Analysis needs a different shape, built so that questions such as "revenue by product category by month" are simple to ask. The most widely used design is the star schema.

Fact tables record events or measurements, usually one row per event: one row per order line, per payment, per page view. They hold:

  • Numbers to add up (quantity, amount, duration)
  • Keys pointing to dimension tables
  • A date or timestamp

Dimension tables describe the things involved in those events: customers, products, stores, dates. They hold the descriptive fields you filter and group by: product name and category, customer country, the month and weekday of a date.

Drawn out, the fact table sits in the middle with dimensions around it, like a star.

Why it works:

  • Questions become a simple pattern: join the fact table to the dimensions you need, filter, group and sum
  • Descriptions live in one place, so renaming a category means changing one row
  • Analysts and dashboard tools understand it easily

Decide the grain first. The grain is exactly what one row of a fact table represents. "One row per order line" and "one row per order" give different answers to "how many items sold?". Write the grain down and never mix grains in one table.

Plan for change. Customers move country and products change category. A common approach is to keep history in the dimension (a new row with valid-from and valid-to dates), so old reports stay correct.

Try it: Design a small star schema for a cinema chain: name one fact table and its grain, and three dimension tables with a few fields each.

Check yourself

1. What does a fact table usually hold?

2. In a star schema, where would "product category" live?

3. What is the grain of a fact table?