shuffleet
Home
Practice
Problems
Lessons
Plans
Compete
Challenges
Interviews
Leaderboard
Progress
Skills
Analytics
Submissions
Account
Settings

Grain, facts and dimensions

Intermediate · 4 min read

In two sentences

The grain of a table is what one row means: "one row per order line", "one row per customer per day". Fact tables hold measurements at a declared grain, dimension tables describe the things they're about, and most wrong numbers come from mixing two grains in one query.

See it

Two orders, one with three lines. The shipping fee is stored per order:

order_idshipping_fee
15
25
order_idline_noskuamount
11A20
12B10
13C10
21A20

Join them and sum: sales 60 ✓, shipping 20 ✗ (it's 10). Shipping lives at the order grain; the query works at the line grain, so order 1's fee is counted three times.

Two fixes: aggregate lines to the order grain before joining, or allocate the fee down to the lines so it's stored at the line grain:

order_idline_noamountshipping share
11202.50
12101.25
13101.25
21205.00

A star schema: the order-line fact at the centre, its dimensions around it, and the grain written on the fact

In SQL

Allocate an order-level amount to lines, proportionally to each line's amount:

SQL
SELECT l.order_id, l.line_no, l.amount,
       o.shipping_fee * l.amount / sum(l.amount) OVER (PARTITION BY l.order_id) AS shipping_share
FROM orders o
JOIN order_lines l ON l.order_id = o.order_id;

The shares add up to the fee for every order, so sums at any level stay right.

Test the grain you declared: no row may share the key.

SQL
SELECT order_id, line_no, count(*)
FROM fct_order_lines
GROUP BY order_id, line_no
HAVING count(*) > 1;      -- must return nothing

When to use it

fact table typeone row perexample
transactioneventorder line, payment, click
periodic snapshotthing per periodaccount balance per day, stock per product per day
accumulating snapshotprocess instance, updated as it movesorder with placed, paid, shipped, delivered dates

Best practices

  • Write the grain in one sentence before writing SQL, in the table's description. If you can't, the table isn't designed yet.
  • Choose the lowest grain the source gives you. You can always aggregate up; you can never split down.
  • Dimensions get surrogate keys (a meaningless integer per version), so type 2 history and several sources with clashing ids both work. Facts store that key.
  • Keep an "unknown" member (key -1) in every dimension, so a fact with a missing reference still joins and still counts.
  • Many-to-many (an order with several tags) goes through a bridge table with a weight per row, or every tag gets the full revenue.

Common mistakes

  • Joining two facts directly. Orders × shipments × payments, each at its own grain, multiplies rows. Aggregate each to a shared grain first (often order), then join.
  • A dimension that isn't unique on its key. One duplicate customer doubles every order of that customer. Test it.
  • Summing a measure that belongs to a coarser grain (the shipping fee above, an order discount on lines). Allocate it, or keep it in its own table.
  • Mixing grains in one table: order rows and line rows in the same fact, told apart by a flag. Every query then needs to remember the flag.

What an interviewer asks next

  • "Design the tables for a ride-hailing app." Start with the grain of each fact: one row per trip, one per payment, one per driver per day. Then the dimensions: rider, driver, city, date.
  • "Why surrogate keys, if the source has ids?" Versions (type 2), merged sources with overlapping ids, and stable keys when the source recycles them.

Go further

Kimball's "declare the grain" page is the rule this lesson is built on. The star schema page shows the overall shape.

Practise it

Revenue Doubled Overnight: Fix the Fan-Out Join, then Model a Ride-Hailing Star Schema from Operational Tables, then Revenue by Category Without Double Counting (Bridge Table).