Tabellab

The Grain of a Fact Table: One Row per Order Line & the Duplicate Check

Lesson 1 of 20Lab

All lessons
  1. The Grain of a Fact Table: One Row per Order Line & the Duplicate CheckLabFree
  2. Surrogate vs Natural Keys: Key Mapping and the Unknown MemberLab Locked
  3. Joining Facts to Dimensions: Enriching Sales with XLOOKUP and INDEX/MATCHLab Locked
  4. Aggregating by Dimension Attributes: SUMIFS by Category and RegionLab Locked
  5. The Date Dimension: Date Keys, Quarters, Months and Weekdays from a DateLab Locked
  6. Slowly Changing Dimensions Type 2: Valid-From/To and the Right Version by DateLab Locked
  7. Many-to-Many & Bridge Tables: Spotting Double Counting and Allocating with WeightsLab Locked
  8. Model QA: Orphan Keys, Inner-Join Losses and Reconciling TotalsLab Locked
  9. Change Detection: Comparing the Source Extract with the Dimension (New, Changed, Unchanged)Lab Locked
  10. Type 1 vs Type 2 Reporting: Revenue as Was, Revenue as Is and the RestatementLab Locked
  11. Periodic Snapshot Facts: Month-End Stock from Movements and the Semi-Additive TrapLab Locked
  12. Accumulating Snapshot Facts: Order Milestones, Lag Days and Overdue Open OrdersLab Locked
  13. Factless Fact Tables: Promotion Coverage and the Products That Did Not SellLab Locked
  14. Junk & Degenerate Dimensions: Folding Flags into One Key and Keeping the Order NumberLab Locked
  15. Role-Playing Dimensions: One Date Dimension as Order Date and Ship DateLab Locked
  16. Conformed Dimensions & Drill-Across: Sales and Returns by Category Without the Fan TrapLab Locked
  17. Facts at Different Grains: Rolling Sales up to the Monthly Budget and Charting the VarianceLab Locked
  18. Aggregate Tables: A Monthly Summary, Its Reconciliation and the Distinct-Count TrapLab Locked
  19. Non-Additive Measures: Margin %, Conversion Rate and Average Price in SummariesLab Locked
  20. Final Project: Larkfield Home's Star-Schema Sales Dashboard with QA ChecksLab Locked

You are not signed in — the lab works, but your progress is not saved. Sign up

Lesson 1 of 20 is free. Unlock all 20 hands-on labs, the final exam and your certificate.

Buy course — CHF 9.90
Loading the spreadsheet…

The report says 1,803.80. The true number is 1,644.40.

A sales report shows a number, the finance team shows a different one, and the meeting spends forty minutes on the difference instead of on the business. In many cases the cause is not a wrong formula in the report. It is a table where one row does not mean what everybody thinks it means, or where the same row was loaded twice. Every SUM on top of that table is then wrong, quietly and consistently.

This free first lesson is for analysts who build reports in Excel or Power BI, finance and controlling teams, and anyone who wants to understand how a data warehouse is organised before working with one. It covers the first decision in dimensional modelling, the one that every later step depends on: the grain of a fact table. You will learn to state it in one sentence, turn it into a key, and check with two formulas whether the data actually follows it.

We use the same fictional business as the lab above, Larkfield Home, a homeware retailer with six orders and fourteen loaded rows of sales data. Two of those rows are copies that a retried data load inserted a second time. Finding them is your job in the lab.

The core concept: facts, dimensions and the grain

Dimensional modelling, the approach made popular by Ralph Kimball, splits data into two kinds of tables. A fact table stores measurements of a business process: quantities, amounts, durations, counts. A dimension table stores the context you use to filter and group those measurements: the product with its category, the store with its region, the date with its month and quarter, the customer with their segment. Facts are long and narrow, with many rows and mostly numbers and keys. Dimensions are short and wide, with fewer rows and many descriptive columns.

Placed together, the fact table sits in the middle and every dimension links to it with a key. Drawn on paper it looks like a star, which is why this design is called a star schema. Reporting tools such as Power BI and the Excel Data Model are built to work with exactly this shape: filter a dimension, and the filter flows into the fact table along the key.

Larkfield Home: a star schema for salesfact_salesorder_id + line_no (grain)date_keyproduct_keystore_keycustomer_keyqtynet_amountdim_datedate_keyyear, quartermonth, weekdaydim_productproduct_keycode, categoryunit costdim_storestore_keystore, cityregiondim_customercustomer_keysegmentcountryEach line is a many-to-one link: many sales rows point to one dimension row.
A star schema for Larkfield Home. The fact table holds the keys and the measures, the four dimensions hold the descriptions. This lesson is about the bold line in the fact table: the grain.

The grain answers the question: what exactly does one row of the fact table represent? For Larkfield Home's sales the answer is one row per order line. An order with three different products has three rows. Not one row per order, not one row per product per day, not one row per customer per month. Kimball's design process puts this decision second, right after choosing the business process and before choosing any dimension or measure, because everything else follows from it. Which dimensions can attach to a row depends on the grain, and which measures can be added up across rows depends on it as well.

A clearly stated grain gives you the key of the fact table for free. One row per order line means that the combination of order_id and line_no must be unique. Order 5003 has lines 1, 2 and 3, so 5003-1, 5003-2 and 5003-3 are three different rows. If 5003-2 appears twice, the table breaks its own grain, and every total that includes that order is too high.

Worked example: Larkfield Home's fact_sales

Here are the fourteen rows from the lab with the two helper columns filled in. Column F joins order_id and line_no into one grain key with =A2&"-"&B2. Column G counts how often each key appears in the whole column with =COUNTIF($F$2:$F$15,F2). Every value in G should be 1.

Larkfield Home (fictional): fact_sales as loaded, with the grain key in F and the duplicate check in G
Roworder_idline_noproduct_codeqtynet_amountF: grain_keyG: rows_per_key
250011LH-101259.805001-11
350012LH-2051129.005001-21
450021LH-310447.605002-11
550031LH-101129.905003-11
650032LH-412288.005003-22
750032LH-412288.005003-22
850033LH-2051129.005003-31
950041LH-5181349.005004-11
1050051LH-310671.405005-12
1150051LH-310671.405005-12
1250052LH-101389.705005-21
1350061LH-412144.005006-11
1450062LH-5181349.005006-21
1550063LH-2052258.005006-31

Rows 6 and 7 are the same line of order 5003 (two units of LH-412 for 88.00), and rows 10 and 11 are the same line of order 5005 (six units of LH-310 for 71.40). Notice that the two copies are identical in every column. That is typical for a load that ran twice: the duplicate does not look wrong on its own, it only shows up when you count keys.

The four summary cells of the lab
CellQuestionFormulaResult
E17Rows loaded=COUNT(E2:E15)14
E18Distinct order lines (the grain)=SUMPRODUCT(1/COUNTIF(F2:F15,F2:F15))12
E19Net amount, duplicates removed=SUMPRODUCT(E2:E15/G2:G15)1,644.40
E20Net amount as loaded=SUM(E2:E15)1,803.80

Three numbers tell the story. The table has 14 rows, but only 12 distinct order lines, which is the true number of facts at the declared grain. The net amount as loaded is 1,803.80, while the amount with each line counted once is 1,644.40. The difference, 159.40, is exactly the two duplicated lines (88.00 + 71.40). A report built on the raw table would overstate revenue by almost 10%.

How do the two one-cell formulas work? COUNTIF(F2:F15,F2:F15) returns an array with the count of every row's key: 1 for most rows, 2 for each copy of a duplicate. Dividing 1 by those counts gives 1 for unique rows and 1/2 for each of two copies, and SUMPRODUCT adds it all up, so a duplicated pair contributes 1/2 + 1/2 = 1. The amount formula uses the same idea: each amount divided by its row count, so the two copies of 88.00 contribute 44.00 each. In Excel 365 or 2021 you can also write =COUNTA(UNIQUE(F2:F15)) for the distinct count.

Net amount by order: as loaded vs duplicates removed0200400600800188.8500147.65002334.9246.95003349.05004232.5161.15005651.05006as loaded, no duplicateas loaded, with duplicateduplicates removedOrders 5003 and 5005 were loaded with a repeated line: 159.40 too much in total.
Net amount by order. Orders 5003 and 5005 contain a duplicated line, so their loaded totals (peach) are higher than the true totals (mint). The other four orders are unaffected. Total: 1,803.80 loaded versus 1,644.40 after removing duplicates.

Same table, three different counts0481216Rows loaded (COUNT)14Distinct order lines (grain)12Distinct orders6Only the middle number matches the declared grain of one row per order line.
The same table gives three different counts. Rows loaded counts copies, distinct orders ignores the lines, and only the distinct order lines match the grain.

If a lookup or check ever needs the grain key, build it once as a helper column and reuse it. The lab solution for E18 also accepts =SUMPRODUCT(1/COUNTIFS(A2:A15,A2:A15,B2:B15,B2:B15)), which works directly on the two key columns without a helper. Both give the same answer, the helper column is easier to read and to filter.

How to do it in Excel, step by step

  1. Write the grain down. Before touching a formula, type one sentence in a note cell: "Grain: one row per order line (order_id + line_no)". If you cannot write this sentence, the table is not ready for reporting.
  2. Turn the data into a table. Click inside the data and press Ctrl+T. Structured tables extend formulas automatically when new rows arrive, which is exactly what you want for a check that runs after every load.
  3. Build the grain key. In F2 type =A2&"-"&B2 and fill down with a double-click on the fill handle. The hyphen matters: without it, order 500 line 11 and order 5001 line 1 would both become "50011".
  4. Count each key. In G2 type =COUNTIF($F$2:$F$15,F2). Press F4 while the range is selected to add the dollar signs, then fill down.
  5. Make duplicates visible. Select F2:F15 and choose Home → Conditional Formatting → Highlight Cells Rules → Duplicate Values. Both copies of 5003-2 and 5005-1 turn red. Then press Ctrl+Shift+L and filter column G for values greater than 1.
  6. Add a one-cell test. Somewhere at the top of the sheet enter =MAX(G2:G15)=1. It shows TRUE when the grain holds and FALSE after a bad load. A single cell like this is easy to check in a monthly routine.
  7. Calculate both totals. Distinct lines: =SUMPRODUCT(1/COUNTIF(F2:F15,F2:F15)). Duplicate-free amount: =SUMPRODUCT(E2:E15/G2:G15). Keep the raw =SUM(E2:E15) next to it so the gap is visible.
  8. Fix the source, not just the sum. In a working copy, select the data and use Data → Data Tools → Remove Duplicates, ticking only order_id and line_no. In Power Query, select both columns and use Home → Remove Rows → Remove Duplicates. Then find out why the load ran twice, because the next one will.

Spreadsheet with a sales fact table, grain key and duplicate check next to dimension tables of a star schema
The course builds a small star schema by hand in a spreadsheet: a sales fact table at a declared grain, surrounded by its dimensions.

Choosing the right grain

The usual advice is to choose the most detailed grain the source system offers, often called the atomic grain. Order lines can always be added up to orders, days, months or regions, but a table that only stores monthly totals per store can never answer a question about a single product. Detailed facts are also easier to check, because each row matches something a person can look up in the source system.

Different business processes need different grains, and each gets its own fact table. A transaction fact table has one row per event, such as an order line or a payment. A periodic snapshot has one row per item per period, such as stock on hand per product per day. An accumulating snapshot has one row per process instance that is updated as it moves along, such as an order from placement to delivery. Mixing grains in one table, for example order lines and order-level shipping fees in the same column, is a common source of double counting: the shipping fee repeats on every line and gets summed three times for a three-line order.

A good grain statement is short, uses business words and names the event, not the columns. A few examples show the pattern:

  • Retail sales: one row per order line, key order_id + line_no.
  • Bank transactions: one row per posted transaction, key transaction_id.
  • Daily inventory: one row per product per warehouse per day, key product + warehouse + date.
  • Support desk: one row per ticket status change, key ticket_id + change sequence number.
  • Payroll: one row per employee per pay period per wage type, key employee + period + wage type.

Each statement immediately tells you which combination of columns must be unique, which dimensions make sense (a daily inventory row has a date and a warehouse, but no customer) and how to test the data. If two people on a team would write different sentences for the same table, settle that first. Arguments about report numbers later are almost always arguments about an unstated grain.

Measures also behave differently. Net amount and quantity are additive: you can sum them across every dimension. A stock level is semi-additive: you can sum it across products but not across days. A unit price or a margin percentage is non-additive: you recalculate it from additive parts instead of summing it.

Common mistakes and how to spot them

  • Using order_id alone as the key. One order has several lines, so =COUNTIF($A$2:$A$15,A7) returns 4 for order 5003 even though nothing is wrong with three of those rows. Spot it: count the grain key, not part of it.
  • Joining keys without a separator. =A2&B2 can make different rows look identical once IDs have different lengths. Spot it: always put a hyphen or pipe between the parts.
  • Counting rows and calling it facts. =COUNTA(F2:F15) returns 14 and includes the copies. Spot it: compare it with the distinct count; if they differ, the grain is broken.
  • Counting distinct orders instead of lines. =SUMPRODUCT(1/COUNTIF(A2:A15,A2:A15)) returns 6, the number of orders. Spot it: the distinct count must be at the declared grain.
  • Deleting rows that only look similar. Two different lines can legitimately have the same product, quantity and amount. Spot it: only rows with the same grain key are duplicates; check line_no before deleting anything.
  • Hiding the problem with averages. An average order value calculated from duplicated rows looks plausible. Spot it: reconcile the total with the source system every month, which is exactly what lesson 8 of the course practises.

Quick reference: functions and shortcuts

Quick reference: Excel functions and shortcuts for grain checks
Function or shortcutWhat it doesExample
&Joins values into one text, here the grain key=A2&"-"&B2 → 5001-1
COUNTIF(range, value)Counts how often a key occurs=COUNTIF($F$2:$F$15,F2)
COUNTIFS(r1,v1,r2,v2)Counts rows matching several columns, no helper key needed=COUNTIFS($A$2:$A$15,A2,$B$2:$B$15,B2)
SUMPRODUCT(1/COUNTIF(…))Distinct count in one formulaReturns 12 for the lab
COUNTA(UNIQUE(range))Distinct count in Excel 365 and 2021=COUNTA(UNIQUE(F2:F15))
SUMPRODUCT(E2:E15/G2:G15)Total with every duplicate counted once1,644.40
MAX(G2:G15)One-cell grain test: must be 1Returns 2 here
Home → Conditional Formatting → Duplicate ValuesColours every repeated grain keyShows 5003-2 and 5005-1
Data → Remove DuplicatesDeletes repeated rows for the selected columnsTick order_id and line_no
Ctrl+T, Ctrl+Shift+LCreate a table, toggle filtersFilter rows_per_key > 1

Real-world uses

  • Monthly sales reporting. Before a sales dashboard is refreshed, a one-cell grain test flags any load that inserted the same order lines twice, so wrong numbers never reach management.
  • Migrating to Power BI. Moving from a flat Excel export to a star schema starts with stating the grain of every fact table. It decides which dimensions you can relate and prevents many-to-many surprises later.
  • Finance reconciliations. Controllers reconcile revenue in the warehouse with the general ledger. Duplicate lines are one of the first things to rule out when the two do not match.
  • Inventory and logistics. A stock table at one row per product per warehouse per day needs a three-part key. Checking that the key is unique catches double loads of daily stock files.
  • Marketing and web analytics. Event tables (one row per click or session) are prone to duplicates when tracking fires twice. Counting a session-plus-event key reveals inflated conversion numbers.

What you will build in the rest of this course

This first lab is free. The remaining 19 labs belong to the full course, a one-time purchase of $9.90, and they build Larkfield Home's star schema piece by piece around the fact table you just checked.

Surrogate vs Natural Keys: Key Mapping and the Unknown Member. You replace the shop system's product codes with the warehouse's surrogate keys using XLOOKUP, send codes that are missing from the dimension to an Unknown member (-1) and measure how much revenue that affects.

Joining Facts to Dimensions: Enriching Sales with XLOOKUP and INDEX/MATCH. You join the sales facts to the product and store dimensions, bring in category, region and unit cost, and calculate the gross margin per line.

Aggregating by Dimension Attributes: SUMIFS by Category and Region. You turn the enriched rows into a category × region summary with SUMIFS, add totals and shares, and chart it as a stacked column chart.

The Date Dimension: Date Keys, Quarters, Months and Weekdays from a Date. You build the columns of a date dimension from plain dates with YEAR, MONTH, ROUNDUP and TEXT and use the quarter to summarise revenue.

Slowly Changing Dimensions Type 2: Valid-From/To and the Right Version by Date. Stores get re-zoned into new regions. You close old versions with a valid-to date and report each sale by the region that was valid when it happened.

Many-to-Many & Bridge Tables: Spotting Double Counting and Allocating with Weights. A product can belong to several collections. You measure how much a naive join double counts and allocate revenue with weights so the totals add up again.

Model QA: Orphan Keys, Inner-Join Losses and Reconciling Totals. You test the whole model like a data engineer: find fact rows without a matching dimension row, rebuild the report totals, reconcile them with the source system and chart the gaps.

Summary

A fact table stores measurements, dimension tables store the context, and together they form a star schema. The first and most important decision is the grain: what one row represents. For Larkfield Home it is one row per order line, so order_id plus line_no must be unique. A joined key with &, a COUNTIF per key and two SUMPRODUCT formulas reveal that the loaded table has 14 rows but only 12 real order lines, and that revenue is overstated by 159.40.

Now scroll up to the lab, build the grain key in column F, run the duplicate check in column G and calculate the distinct lines and the true net amount. It takes about fifteen minutes, the Coach checks each cell live, and you will never trust an unchecked SUM on a fact table again.

Frequently asked questions

What is the grain of a fact table?

The grain states exactly what one row of a fact table represents, for example one row per order line. It is decided before choosing dimensions and measures, and it defines which combination of columns must be unique.

What is the difference between a fact table and a dimension table?

A fact table stores measurements of a business process such as quantities and amounts, with keys to the dimensions. A dimension table stores descriptive context such as product category, store region or calendar month, used to filter and group the facts.

How do I find duplicate rows by key in Excel?

Build a key column, for example =A2&"-"&B2, and count each key with =COUNTIF($F$2:$F$15,F2). Any result above 1 is a duplicate. Conditional Formatting, Duplicate Values highlights them visually.

How do I count distinct values in Excel?

Use =SUMPRODUCT(1/COUNTIF(range,range)) in any Excel version, or =COUNTA(UNIQUE(range)) in Excel 365 and 2021. In a PivotTable added to the Data Model you can also choose Distinct Count as the value setting.

What is a star schema?

A star schema is a data model with one central fact table linked to several dimension tables by keys. It is the standard shape for reporting in tools such as Power BI and the Excel Data Model.

Why should a fact table use the most detailed grain?

Detailed rows can always be summed up to orders, months or regions, but summarised rows can never be broken down again. The most detailed grain keeps every question answerable and makes rows easy to check against the source system.

Keep going: Data Modeling Basics: Fact Tables, Dimensions, Keys & Star Schemas

This free lab is lesson 1 of 20. Unlock the other 19 hands-on labs, the final exam and your certificate for a one-time CHF 9.90.

  1. Surrogate vs Natural Keys: Key Mapping and the Unknown Member
  2. Joining Facts to Dimensions: Enriching Sales with XLOOKUP and INDEX/MATCH
  3. Aggregating by Dimension Attributes: SUMIFS by Category and Region
  4. The Date Dimension: Date Keys, Quarters, Months and Weekdays from a Date
  5. Slowly Changing Dimensions Type 2: Valid-From/To and the Right Version by Date
  6. Many-to-Many & Bridge Tables: Spotting Double Counting and Allocating with Weights
  7. Model QA: Orphan Keys, Inner-Join Losses and Reconciling Totals
  8. Change Detection: Comparing the Source Extract with the Dimension (New, Changed, Unchanged)
  9. Type 1 vs Type 2 Reporting: Revenue as Was, Revenue as Is and the Restatement
  10. Periodic Snapshot Facts: Month-End Stock from Movements and the Semi-Additive Trap
  11. Accumulating Snapshot Facts: Order Milestones, Lag Days and Overdue Open Orders
  12. Factless Fact Tables: Promotion Coverage and the Products That Did Not Sell
  13. Junk & Degenerate Dimensions: Folding Flags into One Key and Keeping the Order Number
  14. Role-Playing Dimensions: One Date Dimension as Order Date and Ship Date
  15. Conformed Dimensions & Drill-Across: Sales and Returns by Category Without the Fan Trap
  16. Facts at Different Grains: Rolling Sales up to the Monthly Budget and Charting the Variance
  17. Aggregate Tables: A Monthly Summary, Its Reconciliation and the Distinct-Count Trap
  18. Non-Additive Measures: Margin %, Conversion Rate and Average Price in Summaries
  19. Final Project: Larkfield Home's Star-Schema Sales Dashboard with QA Checks

Buy course — CHF 9.90 See the full course