Sub topic 11
Flattening dimensions versus snowflaking
This chapter assumes you can load data in Power BI Desktop, read the Model view, and write simple DAX and Power Query. It also assumes you know what a fact table, a dimension table, and a one-to-many relationship are. The decision here is how to shape a dimension: as one wide table (flattened, or denormalized) or as several linked tables (snowflaked).
The two shapes
A dimension often has a natural hierarchy. A product belongs to a subcategory, and a subcategory belongs to a category. A normalized source database usually stores each level in its own table. You have two ways to bring this into a model.
- Flattened dimension: one table holds every level as columns. Each product row repeats its subcategory and category names.
- Snowflaked dimension: each level keeps its own table. Product relates to Subcategory, and Subcategory relates to Category. The fact table relates only to Product.
The name "snowflake" comes from the shape the diagram takes. The fact table sits in the middle, and the dimensions branch outward like the arms of a snowflake.
What the flattened version looks like
This is the dimension from the earlier chapters. All three levels live in one table.
CREATE TABLE DimProduct (
ProductKey INT PRIMARY KEY,
ProductName VARCHAR(100) NOT NULL,
Subcategory VARCHAR(50),
Category VARCHAR(50)
);The category name Bikes appears on every bike row. In a transactional database that repetition is a problem, because a rename must be applied in many places. In an analytical model it is acceptable, for reasons covered below.
What the snowflaked version looks like
Here each level is its own table, and each table carries a key to its parent.
CREATE TABLE DimCategory (
CategoryKey INT PRIMARY KEY,
Category VARCHAR(50) NOT NULL
);
CREATE TABLE DimSubcategory (
SubcategoryKey INT PRIMARY KEY,
Subcategory VARCHAR(50) NOT NULL,
CategoryKey INT NOT NULL REFERENCES DimCategory (CategoryKey)
);
CREATE TABLE DimProduct (
ProductKey INT PRIMARY KEY,
ProductName VARCHAR(100) NOT NULL,
SubcategoryKey INT NOT NULL REFERENCES DimSubcategory (SubcategoryKey)
);In Power BI, you would then draw three relationships: Category to Subcategory, Subcategory to Product, and Product to the fact table. Each is one-to-many, and each filters in the direction of the arrow toward the fact.
Why flattening is the default in Power BI
Flattening is usually the better choice in Power BI, for four practical reasons.
- Shorter filter paths. A slicer on Category in a snowflake must filter Category, then Subcategory, then Product, then the fact table. Each hop is a relationship the engine must follow. A flattened table has one hop.
- A simpler model for report authors. Someone building a report sees one
Producttable with every product field in it. In a snowflake they must know which of three tables holds each field. Field lists get longer and easier to misuse. - Simpler relationships. Fewer relationships mean fewer places for an inactive or ambiguous path to appear.
- Little storage cost. Power BI stores each column with compression. A column like
Categoryhas very few distinct values, so repeating it across thousands of product rows costs little. Dimensions are also small compared with fact tables, so the repeated text rarely affects model size.
> The last point is the one people most often get wrong. Repeated text in a dimension is cheap. Repeated text in a fact table is expensive.
That sentence is worth keeping in mind as a rule, but the rule itself belongs in a callout.
When snowflaking is reasonable
Flattening is a default, not a law. Some cases favor keeping the extra tables.
- A shared lookup used by several dimensions. If both
DimProductandDimSupplierpoint to the sameDimCountry, keepingDimCountryseparate avoids duplicating its attributes in two places. Even so, think about whether users will filter by country on both. If they will, a flattened copy of the country columns in each dimension may be simpler. - A level with its own facts. Suppose you have a budget table recorded at category level, not product level. That fact table needs a Category dimension to relate to. Keeping
DimCategoryas a real table gives the budget fact something to connect to. - Attributes that change on a different schedule. If a large dimension has a small set of columns refreshed far more often than the rest, splitting those columns out can reduce refresh work. This is uncommon, so confirm that refresh time is a real problem first.
- A very wide dimension. A table with hundreds of columns can be hard to navigate. Splitting related columns into a second table is sometimes used here, though a better first step is to hide the columns report authors do not need.
The budget case deserves a closer look, because it is the most common reason to keep a separate level. A budget fact at category grain cannot relate to DimProduct, because many products share a category and Category is not unique there. A relationship needs a unique key on the one side. The fix is a small DimCategory with one row per category, which both the budget fact and the product hierarchy can use.
The cost of snowflaking in practice
A snowflake is not wrong, and the model will work. These are the issues you pay for.
- Slower and more complex filter propagation. Each relationship in the chain is another step in the query plan.
- Hierarchies that span tables. A Power BI hierarchy must be built from columns in one table. If Category, Subcategory, and Product live in three tables, you cannot build a single drill-down hierarchy from them without flattening or adding calculated columns.
- Cross-table sorting and display problems. You cannot sort a column by a column in another table, and a visual that mixes fields from several snowflake tables can behave in surprising ways.
- More relationships to keep correct. Every relationship is a chance for a mismatched key or an unexpected filter direction.
Flattening in Power Query
If your source is normalized, flatten it before loading. In Power Query you merge the lookup tables into the product query and expand the columns you need. The following query assumes three source queries named Product, Subcategory, and Category, each with the keys shown in the SQL above.
let
MergedSub = Table.NestedJoin(
Product, {"SubcategoryKey"},
Subcategory, {"SubcategoryKey"},
"SubTable", JoinKind.LeftOuter
),
ExpandedSub = Table.ExpandTableColumn(
MergedSub, "SubTable", {"Subcategory", "CategoryKey"}
),
MergedCat = Table.NestedJoin(
ExpandedSub, {"CategoryKey"},
Category, {"CategoryKey"},
"CatTable", JoinKind.LeftOuter
),
ExpandedCat = Table.ExpandTableColumn(
MergedCat, "CatTable", {"Category"}
),
DimProduct = Table.SelectColumns(
ExpandedCat,
{"ProductKey", "ProductName", "Subcategory", "Category"}
)
in
DimProductThen disable load on the Subcategory and Category queries, so they feed DimProduct but do not appear in the model as separate tables.
A left outer join keeps every product even when its subcategory key has no match. Check afterward that no product ended up with a null Category. An inner join would silently drop those products, and their sales would then fail to match any row in the dimension.
Edge cases to check
- Duplicate keys after a merge. If the lookup table has two rows for one key, the merge multiplies product rows and
ProductKeystops being unique. Power BI will then refuse to create the one-to-many relationship, or it will create a many-to-many one. Confirm the row count ofDimProductequals the count before merging. - Blank levels. Some products have no subcategory. Replace nulls with a label such as
Unassignedso the slicer shows an explicit value rather than a blank. - Renames. After flattening, a category rename arrives through the source and the refresh updates every row. You do not need to edit anything by hand.
Exercise
A budget table is recorded at category level, one row per category per month. Your DimProduct is flattened with a Category column that repeats across products. Why can't you relate the budget table to DimProduct[Category], and what is the smallest change that fixes it?
Show answer
A one-to-many relationship needs unique values on the one side, and DimProduct[Category] repeats for every product in the category. Create a small DimCategory table with one row per category, relate Budget to it, and relate DimCategory to the other tables you want it to filter. The category slicer should then come from DimCategory.
Exercise
After merging Subcategory into Product in Power Query, the Product row count rises from 504 to 611, and Power BI cannot create a one-to-many relationship on ProductKey. What is the likely cause and how do you check it?
Show answer
The Subcategory lookup probably contains duplicate rows for some SubcategoryKey values, so the merge returns more than one match per product. Check the lookup for duplicate keys, for example by grouping on SubcategoryKey and counting rows, then remove the duplicates before merging.