Sub topic 11
Flattening dimensions versus snowflaking
This chapter assumes you can load data in Power BI Desktop, read the Model view, and understand primary and foreign keys. It also assumes you have seen the DimProduct table from the earlier chapters.
The two shapes
A product has a subcategory, and a subcategory has a category. You can store this hierarchy in two ways.
- Flattened (denormalized): one dimension table holds product, subcategory, and category columns. Category names repeat on every product row.
- Snowflaked (normalized): separate tables hold products, subcategories, and categories. Each table links to the next with a one-to-many relationship.
Why flattening is the default
The flattened dimension is the one used in DimProduct from earlier in this tutorial:
CREATE TABLE DimProduct (
ProductKey INT PRIMARY KEY,
ProductName VARCHAR(100) NOT NULL,
Subcategory VARCHAR(50),
Category VARCHAR(50)
);For Power BI, this shape has three advantages.
- Simpler filter paths. A slicer on
CategoryfiltersFactSalesthrough one relationship. In a snowflake, the filter must pass through a chain of tables. - Easier for report authors. All product attributes are in one field list, so authors don't need to know which table holds which column.
- Little storage cost. VertiPaq compresses columns with repeated values very well. A
Categorycolumn with a handful of distinct values over many rows takes little space.
When a snowflake is acceptable
Snowflaking is not wrong. It can make sense in these cases:
- Several dimensions share the same sub-dimension. For example, both
DimCustomerandDimStorelink to aDimGeographytable. - Part of the hierarchy is very large and changes often, and you want to maintain it in one place.
- The source system is already normalized and you have no reason to reshape it.
The cost is that every extra hop adds a relationship that the engine must follow. Filters flow in a single direction, from the "one" side to the "many" side. A slicer on DimCategory reaches FactSales only if every relationship in the chain points toward the fact table.
Comparing the options
| Aspect | Flattened | Snowflake |
|---|---|---|
| Tables in the model | Fewer | More |
| Relationships on the filter path | One | Several |
| Field list for authors | One table per concept | Attributes split across tables |
| Repeated text values | Yes, but compressed | No |
| Typical use in Power BI | Default choice | Shared sub-dimensions |
Flattening a snowflake with a merge
If your source is a snowflake, you can flatten it in Power Query. This query merges subcategory and category columns into the product table:
let
Products = Product,
WithSub = Table.NestedJoin(Products, {"SubcategoryID"}, Subcategory, {"SubcategoryID"}, "Sub", JoinKind.LeftOuter),
ExpandSub = Table.ExpandTableColumn(WithSub, "Sub", {"SubcategoryName", "CategoryID"}),
WithCat = Table.NestedJoin(ExpandSub, {"CategoryID"}, Category, {"CategoryID"}, "Cat", JoinKind.LeftOuter),
ExpandCat = Table.ExpandTableColumn(WithCat, "Cat", {"CategoryName"}),
DimProduct = Table.SelectColumns(ExpandCat, {"ProductID", "ProductName", "SubcategoryName", "CategoryName"})
in
DimProductAfter loading, disable loading for the source tables Subcategory and Category so they don't appear in the model.
Common errors
- Leaving the source tables loaded. The model then contains both the flat table and the snowflake pieces, and authors can pick the wrong field.
- Bidirectional filters to make a snowflake work. If a slicer on an outer table does not filter the fact table, check the relationship direction first. Flattening usually fixes the problem without bidirectional filtering.
- Duplicate keys after a merge. If a lookup table has repeated keys, the merge multiplies product rows. Confirm that
ProductKeyis still unique after flattening.
Exercise
Your model has FactSales, DimProduct, DimSubcategory, and DimCategory, all chained with single-direction one-to-many relationships. A slicer on DimCategory[CategoryName] has no effect on sales. What is the most likely cause, and what is a cleaner fix than switching to bidirectional filters?
Show answer
At least one relationship in the chain points the wrong way, so the filter does not travel from DimCategory through DimSubcategory and DimProduct to FactSales. The cleaner fix is to flatten Subcategory and Category into DimProduct in Power Query (using left outer merges), then relate DimProduct directly to FactSales with a single one-to-many relationship.