Sub topic 4
Dimension tables: the context you slice by
This chapter assumes you know Power BI Desktop's Model view, basic primary and foreign key ideas, and simple Power Query and DAX. It covers what a dimension table holds, how to design one, and the errors that most often break it.
What a dimension table is
A dimension table describes the things your facts are about. Each row is one product, customer, store, or day. The columns are descriptive attributes, such as name, category, or region. You use those attributes to slice, group, and filter the numbers in the fact table.
Dimensions have three traits:
- One row per member, so the key column is unique.
- Mostly text and categorical columns, with few numbers to add up.
- Far fewer rows than the fact table.
A worked example
The DimProduct table below stores one row per product. The ProductKey column is the primary key. The fact table points to it with a foreign key.
CREATE TABLE DimProduct (
ProductKey INT PRIMARY KEY,
ProductName VARCHAR(100) NOT NULL,
Subcategory VARCHAR(50),
Category VARCHAR(50)
);
CREATE TABLE FactSales (
OrderNumber VARCHAR(20) NOT NULL,
OrderLineNumber INT NOT NULL,
DateKey INT NOT NULL,
ProductKey INT NOT NULL REFERENCES DimProduct (ProductKey),
Quantity INT NOT NULL,
SalesAmount DECIMAL(18, 2) NOT NULL,
PRIMARY KEY (OrderNumber, OrderLineNumber)
);Category and subcategory are stored once per product, not once per sale. A product sold 50,000 times still has one row in DimProduct.
Designing a good dimension
- Pick the grain. Decide what one row means, for example "one product". Every attribute must describe that single member.
- Give it a unique key. Power BI needs unique values on the one side of a relationship. Duplicate keys cause an error when you create a one-to-many relationship.
- Add the attributes people filter by. Put category, region, or status here, not in the fact table.
- Use readable names. Column names show up in slicers, axes, and field lists. Prefer
Product Categoryovercat_cd. - Leave out unused columns. Each one costs memory and clutters the field list.
Hierarchies such as Category, Subcategory, and Product can live in the same table. Keeping them together is called denormalizing, and a later chapter compares it with snowflaking.
Common errors
- Duplicate keys. A dimension built from a flat table often repeats each product once per sale. Remove duplicates on the key, as in the Power Query step below.
- Blank or missing members. If the fact table holds a key that has no dimension row, Power BI shows a blank group in visuals. Add the missing member or an "Unknown" row.
- Numbers hidden in dimensions. A column like
ListPricethat you aggregate belongs in a fact or in a measure. Attributes you only filter by can stay. - Mixed levels. Putting customer and product attributes in one table breaks the grain, so rows no longer describe one thing.
let
Source = SalesFlat,
ProductColumns = Table.SelectColumns(Source, {"ProductID", "ProductName", "Subcategory", "Category"}),
DimProduct = Table.Distinct(ProductColumns, {"ProductID"})
in
DimProductExercise
A dimension table built from a flat sales file has 80,000 rows, but only 1,200 distinct products. What happens when you relate it to the fact table, and how do you fix it?
Show answer
Power BI cannot make a valid one-to-many relationship because the key column has duplicates on the one side. Select the product columns and remove duplicates on the product ID, as in Table.Distinct, so each product has a single row.