Sub topic 12
Turning a flat table into facts and dimensions in Power Query
This chapter assumes you can load data in Power BI Desktop, open the Power Query editor, and read simple DAX. It also assumes you know what grain, primary keys, and foreign keys are. Here you will split one flat table into a fact table and a dimension table.
Start from the flat table
Many sources arrive as one wide table. Each row is an order line, and every row repeats the product name, subcategory, and category. Suppose the query SalesFlat has these columns:
- OrderNumber, OrderLineNumber
- OrderDate
- ProductID, ProductName, Subcategory, Category
- Quantity, SalesAmount
The grain is one row per order line. The fact table keeps that grain. The product columns move into a dimension.
The plan
The split follows three steps:
- Build the dimension by referencing the flat query. Keep only the descriptive columns and remove duplicate keys.
- Build the fact table by referencing the flat query. Keep the keys and the numeric columns, and drop the descriptive columns.
- Relate the two tables on the key.
Build the dimension
Right-click SalesFlat and choose Reference. Do not choose Duplicate, because a reference reuses the loaded source instead of repeating the work. Rename the new query DimProduct and edit it:
let
Source = SalesFlat,
ProductColumns = Table.SelectColumns(Source, {"ProductID", "ProductName", "Subcategory", "Category"}),
DimProduct = Table.Distinct(ProductColumns, {"ProductID"})
in
DimProductTable.Distinct with {"ProductID"} keeps the first row for each ID. The key is now unique, which a one-to-many relationship requires.
Build the fact table
Create a second reference to SalesFlat and rename it Sales. Keep the keys and numbers, and remove the product descriptions:
let
Source = SalesFlat,
SalesColumns = Table.SelectColumns(Source, {"OrderNumber", "OrderLineNumber", "OrderDate", "ProductID", "Quantity", "SalesAmount"})
in
SalesColumnsProductID stays in the fact table as the foreign key. The names and categories now live only in DimProduct.
Check the result
Before you load, check these points:
ProductIDinDimProducthas no duplicates and no nulls.- Every
ProductIDinSalesalso appears inDimProduct. A missing one shows up as a blank row in visuals. - The
Salesrow count matchesSalesFlat. The split must not add or remove rows.
Then close and apply. In Model view, create a one-to-many relationship from DimProduct[ProductID] to Sales[ProductID] with a single filter direction.
Common errors
| Symptom | Likely cause |
|---|---|
| Relationship cannot be one-to-many | Duplicate keys remain in the dimension |
| Blank category in visuals | Fact rows have a ProductID missing from the dimension |
| Model is still large | `SalesFlat` is still loaded, or text descriptions remain in the fact table |
| Totals differ from the source | Rows were filtered out when the fact table was built |
Exercise
You reference SalesFlat to build DimCustomer and use Table.Distinct on CustomerName instead of CustomerID. Two different customers share the name "Alex Kim". What goes wrong?
Show answer
Table.Distinct keeps only one of the two customers, so the other customer ID has no row in the dimension. Fact rows for that ID show as blank in visuals. Remove duplicates on the key, CustomerID, not on a descriptive column.