Level 1: Model the data
AI can't guess your grain.
What one row means, how to write it down in one line, and a two-query test that tells you whether that line is true.
On this page
Guide 1 ended on one line: check the grain first. This guide is about what that means when you sit down with a real model.
Grain is what one row in a table stands for. One revenue entry. One order line. One product per month. Every total, every join and every filter depends on it, and it's almost never written down. It lives in the head of whoever built the table.
People guess the grain from column names, and they're close enough most of the time. A tool that reads your model guesses too, and it can't walk over and ask the person who built it. If the grain isn't written down, it isn't part of the model.
What grain means
Here's the Revenue table from guide 1 again.
| PKid | FKcustomer | FKproduct | reporting date | accounting date | amount |
|---|---|---|---|---|---|
| R-001 | C-001 | P-01 | 2025-04-01 | 2025-05-01 | 120.00 |
| R-002 | C-001 | P-01 | 2025-04-01 | 2025-04-01 | -20.00 |
| R-003 | C-001 | P-01 | 2025-05-01 | 2025-05-01 | 300.00 |
Revenue. Same three made-up rows as guide 1.
It's tempting to read it as one row per customer, product and month. It has a customer, a product and a date, and most reports show it by month. That reading is wrong. R-001 and R-002 are both C-001, P-01, April 2025. The real grain is one row per recognized revenue entry, and a month can hold any number of them, including a positive entry and a negative adjustment.
Why it matters: if you believe the grain is one row per customer, product and month, you'll treat the table like a lookup. You'll join other tables to it on those three columns, and every match you expected to be one row turns out to be two.
Write it down in one line
I use the same sentence for every fact table:
For the two tables in this series:
- Revenue. One row in Revenue is one recognized revenue entry, identified by id. It is not one row per customer, product and month.
- Terms. One row in Terms is one recorded contract term, identified by id. It is not one row per customer and product, and its amount is not revenue.
The "It is not" half does most of the work. It names the mistake somebody is going to make, before they make it.
Test the line
A grain statement is a claim about a key, so you can test it. If the key really identifies one row, grouping by it gives a count of 1 everywhere, and a query for counts above 1 comes back empty.
-- Claimed key: id. Expect no rows back. SELECT id, COUNT(*) AS rows_per_key FROM revenue GROUP BY id HAVING COUNT(*) > 1; -- The tempting reading: -- customer, product, month. SELECT customer, product, reporting_date, COUNT(*) AS rows_per_key FROM revenue GROUP BY customer, product, reporting_date HAVING COUNT(*) > 1;
The first query returns nothing, so id holds up. The second returns one row: C-001, P-01, 2025-04-01, with 2 rows per key. That's the tempting reading disproved with evidence, in two queries. (Every date in this example is the first of the month, so grouping by the date is the same as grouping by month.)
One table, two grains
The harder case is a table that holds two kinds of rows. Here's a made-up order table that keeps an order header and its lines together:
| PKrow_id | order_id | row_type | line_no | amount |
|---|---|---|---|---|
| OR-1 | O-100 | header | 150.00 | |
| OR-2 | O-100 | line | 1 | 100.00 |
| OR-3 | O-100 | line | 2 | 50.00 |
Made-up order rows. The header carries the total; the lines carry the parts.
Sum the amount for O-100 and you get 300.00 for an order worth 150.00. Nothing errors, same as guide 1.
Figure 1 · One table, two grains
The same kind of test spots it. The claimed key for an order line is order_id plus line_no, and the header row has no line number. Count the rows where part of the claimed key is missing:
-- Claimed key for a line: -- order_id + line_no. SELECT row_type, COUNT(*) AS rows_missing_key FROM order_rows WHERE order_id IS NULL OR line_no IS NULL GROUP BY row_type;
It returns one row: header, 1. Any rows back means the table holds more than one grain. A column called row_type, record_type or level is a warning sign on its own. The fix is to split it into one table of order headers and one of order lines. If you can't change the source, filter the model to one row type and say so in the grain statement.
Two facts, two grains
Sometimes each fact is fine on its own and they just sit at different grains. Say there's also a made-up monthly budget: one row per product per month, B-01, P-01, April 2025, 150.00, with no customer on it.
Don't join them row to row. Roll the finer table up to the coarser grain, then compare at that grain:
WITH actual AS ( SELECT product, SUM(amount) AS actual FROM revenue WHERE reporting_date >= '2025-04-01' AND reporting_date < '2025-05-01' GROUP BY product ) SELECT b.product, b.month, a.actual, b.budget, a.actual - b.budget AS variance FROM budget b JOIN actual a ON a.product = b.product WHERE b.month = '2025-04';
One row back: P-01, 2025-04, actual 100.00, budget 150.00, variance -50.00. In a Power BI or Tableau model it's the guide 1 shape again: both facts related to shared Product and Month dimensions, and nothing between the facts. Microsoft's star schema guidance covers the same idea for Power BI.
Figure 2 · Two facts, two grains
One more rule falls out of the grain. Budget has no customer, so it can't be split by customer. A report that puts budget next to a customer slicer should show it as blank or say it doesn't apply. It shouldn't repeat 150.00 against every customer.
What I check before any tool answers from a model
- Does every fact table have a one-line grain statement, with an "It is not" half?
- Does the claimed key pass the uniqueness test?
- Does any row have part of its key missing, or a row-type column?
- When two facts meet, are they compared at the coarser grain, through shared dimensions?
Write the grain down. Then test it.