Grain First: Designing Fact Tables
Declare what one fact row means before anything else, then choose between transaction, periodic-snapshot, accumulating-snapshot and factless fact tables, and classify each measure as additive, semi-additive or non-additive, with runnable PostgreSQL examples of each mistake.
Contentsand lesson path
On this page
- Why it matters
- Declare the grain, at the most atomic level
- Four kinds of fact table
- Additive, semi-additive and non-additive measures
- Check your understanding
- Apply it
- Review prompt
Lessons of Dimensional Modeling
- Why Analytics Needs a Different Shape of Data Concept · 15 min
- Grain First: Designing Fact Tables Concept · 25 min
- Dimensions, Surrogate Keys and Conformance Concept · 20 min
- Worked Example: A Star Schema for Online Retail Worked example · 30 min
- Lab: Type 2 History and Point-in-Time Joins Lab · 30 min
- Check: Model a New Business Process Knowledge check · 8 questions
What you will learn
- Declare the grain of a business process and design transaction, periodic-snapshot or accumulating-snapshot fact tables with correctly classified additive, semi-additive and non-additive measures.
Prerequisites: Relational Data Modeling
01. Why it matters
Most wrong numbers in a warehouse are not caused by bad SQL. They come from a fact table whose rows mean different things, or from a measure that is added up when it should not be. Both mistakes produce a result that looks plausible, so nobody notices until two reports disagree.
Kimball's design process puts one decision before all others: declare the grain. The grain is a plain-language statement of what a single fact row represents, such as "one row per product line on a sales receipt" or "one row per account per day". Once the grain is fixed, the dimensions follow (what describes one such row?) and so do the measures (what was measured for it?). This lesson works through the grain, the four kinds of fact table and the three kinds of measure, each with a small example you can run in the sql-lab PostgreSQL 17 container. Create the database below, open it with docker exec -it sql-lab psql -U postgres -d grain and paste each example in order.
docker exec sql-lab psql -U postgres -X -q -c "SET client_min_messages = warning" \
-c "DROP DATABASE IF EXISTS grain" -c "CREATE DATABASE grain"02. Declare the grain, at the most atomic level
Prefer the most atomic grain the source captures: the lowest level of detail, such as an order line rather than a daily total. Atomic rows can be rolled up to any total later; a total can never be broken back down into the rows it came from. Then never mix grains in one table. The example below shows what happens when you do. A shop charges a delivery fee per order, and someone copies it onto each line of the order so that it "fits" a line-grain fact table. Order 1 has three lines and a fee of 6; order 2 has one line and a fee of 4.
CREATE TABLE fact_order_line (
order_id int, line_no int, revenue numeric(8,2),
delivery_fee numeric(8,2) -- an order-level amount copied to every line
);
INSERT INTO fact_order_line VALUES
(1, 1, 20, 6), (1, 2, 30, 6), (1, 3, 50, 6), (2, 1, 40, 4);
SELECT sum(revenue) AS revenue, sum(delivery_fee) AS delivery_fees
FROM fact_order_line; revenue | delivery_fees
---------+---------------
140.00 | 22.00
(1 row)The shop charged 10 in fees, but the table says 22: order 1's fee was counted three times. There are two clean fixes. Either keep order-level amounts in a separate fact table at order grain, or allocate the fee down to the line grain so that the line amounts add up to the order amount. The query below allocates in proportion to each line's revenue; other rules, such as by weight, are equally valid if the business agrees on them.
SELECT order_id, line_no, revenue,
round(delivery_fee * revenue / sum(revenue) OVER (PARTITION BY order_id), 2)
AS allocated_fee
FROM fact_order_line
ORDER BY order_id, line_no; order_id | line_no | revenue | allocated_fee
----------+---------+---------+---------------
1 | 1 | 20.00 | 1.20
1 | 2 | 30.00 | 1.80
1 | 3 | 50.00 | 3.00
2 | 1 | 40.00 | 4.00
(4 rows)Now the fees add up to 6 + 4 = 10. With real money, check that rounding still sums to the order amount, and put any remainder on one line.
03. Four kinds of fact table
The grain also decides the kind of fact table. There are four common kinds.
- A transaction fact has one row per event at the moment it happens: a sale line, a payment, a click. Rows are inserted once and normally not changed. It is the most common kind and the most flexible.
- A periodic snapshot has one row per thing per period, whether or not anything happened: stock on hand per product per store per day, or balance per account per month. It answers "what was the level at that time?" without replaying every transaction.
- An accumulating snapshot has one row per instance of a process with a known set of steps, such as an order going from placed to packed, shipped and delivered. It has a date column for each milestone, and the row is updated as the order moves on, which makes durations between steps easy to compute.
- A factless fact has keys but no numeric measures. It records that something happened, such as a student attending a class, or that something was true, such as a product being on promotion on a day. You count its rows, or compare it with another fact.
The accumulating snapshot is the least familiar, so run one. An order row is inserted when the order is placed, with empty milestone dates, and updated later.
CREATE TABLE fact_fulfilment (
order_id int PRIMARY KEY,
placed_date date NOT NULL, packed_date date, shipped_date date,
delivered_date date
);
INSERT INTO fact_fulfilment (order_id, placed_date) VALUES
(1, '2026-03-02'), (2, '2026-03-02'), (3, '2026-03-03');
UPDATE fact_fulfilment SET packed_date = '2026-03-03', shipped_date = '2026-03-04'
WHERE order_id IN (1, 2);
UPDATE fact_fulfilment SET delivered_date = '2026-03-06' WHERE order_id = 1;
UPDATE fact_fulfilment SET packed_date = '2026-03-05' WHERE order_id = 3;
SELECT order_id, placed_date AS placed, shipped_date AS shipped,
delivered_date AS delivered,
shipped_date - placed_date AS days_to_ship,
delivered_date - shipped_date AS days_in_transit
FROM fact_fulfilment ORDER BY order_id; order_id | placed | shipped | delivered | days_to_ship | days_in_transit
----------+------------+------------+------------+--------------+-----------------
1 | 2026-03-02 | 2026-03-04 | 2026-03-06 | 2 | 2
2 | 2026-03-02 | 2026-03-04 | | 2 |
3 | 2026-03-03 | | | |
(3 rows)Each lag is a subtraction on one row, and an empty cell simply means "not reached yet". With a transaction fact of status events you would need to find and pair two rows per order for every lag.
Now a factless fact. The promotions team asks: "Which promoted products did not sell at all?" Sales alone cannot answer it, because a product that did not sell has no sales row. A coverage table that lists which products were on promotion on which days can, by subtracting the products that sold.
CREATE TABLE fact_promotion_coverage (day date, product text); -- no measures
CREATE TABLE fact_sales (day date, product text, qty int);
INSERT INTO fact_promotion_coverage VALUES
('2026-03-07', 'kettle'), ('2026-03-07', 'toaster'), ('2026-03-07', 'blender');
INSERT INTO fact_sales VALUES ('2026-03-07', 'kettle', 4), ('2026-03-07', 'mug', 9);
SELECT day, product FROM fact_promotion_coverage
EXCEPT
SELECT day, product FROM fact_sales
ORDER BY product; day | product
------------+---------
2026-03-07 | blender
2026-03-07 | toaster
(2 rows)04. Additive, semi-additive and non-additive measures
A measure is additive if you can sum it across every dimension and get a meaningful number. Revenue and quantity are additive: sum them by day, product, store or all three. A measure is semi-additive if it sums across some dimensions but not across time. Stock levels and account balances are the usual examples. Each row of a snapshot restates a level, so adding three daily levels gives a number that never existed. A measure is non-additive if it cannot be summed at all: ratios, percentages, unit prices and distinct counts.
First the semi-additive case. A daily inventory snapshot holds stock on hand for two stores over three days.
CREATE TABLE fact_inventory_daily (day date, store text, on_hand int);
INSERT INTO fact_inventory_daily VALUES
('2026-03-01', 'leeds', 50), ('2026-03-01', 'york', 30),
('2026-03-02', 'leeds', 45), ('2026-03-02', 'york', 30),
('2026-03-03', 'leeds', 40), ('2026-03-03', 'york', 20);
SELECT sum(on_hand) AS wrong_sum_over_days FROM fact_inventory_daily;
SELECT day, sum(on_hand) AS units_in_all_stores
FROM fact_inventory_daily GROUP BY day ORDER BY day;
WITH per_day AS (
SELECT day, sum(on_hand) AS units FROM fact_inventory_daily GROUP BY day
)
SELECT (SELECT units FROM per_day ORDER BY day DESC LIMIT 1) AS closing_units,
round(avg(units), 1) AS average_daily_units
FROM per_day; wrong_sum_over_days
---------------------
215
(1 row)
day | units_in_all_stores
------------+---------------------
2026-03-01 | 80
2026-03-02 | 75
2026-03-03 | 60
(3 rows)
closing_units | average_daily_units
---------------+---------------------
60 | 71.7
(1 row)The company never held 215 units. Summing across stores within a day is correct (80, 75, 60). Across days you choose a semi-additive rule that matches the question: the closing level (60) for "what do we hold at the end of the period?", or the average daily level (71.7) for "how much stock did we carry?". BI tools often let you declare this rule per measure; write it down in the fact table's documentation either way.
Now the non-additive case. Two orders have very different sizes and margins. Storing only margin_pct invites the wrong aggregate.
CREATE TABLE fact_order_margin (
order_id int, revenue numeric(10,2), margin numeric(10,2), margin_pct numeric(5,1)
);
INSERT INTO fact_order_margin VALUES (1, 1000, 100, 10.0), (2, 10, 5, 50.0);
SELECT round(avg(margin_pct), 1) AS avg_of_pct,
round(100 * sum(margin) / sum(revenue), 1) AS pct_of_sums
FROM fact_order_margin; avg_of_pct | pct_of_sums
------------+-------------
30.0 | 10.4
(1 row)The average of the two percentages says 30 %, because a 10-pound order counts as much as a 1,000-pound one. The company really earned 105 on 1,010, which is 10.4 %. The rule for non-additive measures is: store the additive parts (here margin and revenue) in the fact table, and compute the ratio after aggregating. Distinct counts behave the same way: the distinct customers of January plus those of February is not the number of distinct customers in the two months, because some bought in both, so keep the customer key at atomic grain and count again for each period.
05. Check your understanding
06. Apply it
For the subscription business in the practice project, write one sentence of grain for each fact table before you write any SQL:
- payments (a transaction fact): what is one row?
- subscriptions per customer per month (a periodic snapshot): which measures are semi-additive, and which rule do you use across months?
- sign-up funnel from trial start to first payment (an accumulating snapshot): which milestone dates does a row hold?
For every measure, write whether it is additive, semi-additive or non-additive, and how a monthly report should aggregate it.
07. Review prompt
Without looking back, explain:
- what "declaring the grain" means, and why the most atomic grain is preferred;
- what goes wrong when an order-level amount sits on line-level rows, and two ways to fix it;
- the four kinds of fact table, with one example each;
- why stock on hand cannot be summed over days, and why a margin percentage cannot be averaged.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.