Sub topic 4
Dimension tables: the context you slice by
This chapter assumes you can load data into Power BI Desktop, open the Model view, and read a simple DAX expression. It also assumes you know what a primary key and a foreign key are. It builds on the previous chapters, where you chose a grain and designed a fact table. Here you design the tables that describe the facts.
What a dimension table is
A dimension table holds the descriptive context for your facts. A fact row says "3 units were sold for 59.97." A dimension says which product, which customer, which day, and which store. Report users put dimension columns on slicers, axes, rows, and legends. They put fact columns inside measures.
Dimension tables have these properties:
- One row per member of the thing they describe, such as one row per product.
- A unique key column that identifies each row.
- Many descriptive columns, mostly text, with some dates and flags.
- Far fewer rows than the fact table. A fact table can grow by millions of rows a day. A product dimension grows only when the catalog changes.
The one-row-per-member rule is what makes the relationship to the fact table one-to-many. If two rows share a key, Power BI cannot create a one-to-many relationship from that table. It will offer many-to-many instead, which brings the problems described later in this tutorial.
A worked example
The following SQL defines a product dimension and the fact table that points to it.
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)
);DimProduct.ProductKey is the primary key. FactSales.ProductKey is a foreign key that repeats once for every sale of that product. Category and Subcategory are stored directly on each product row. They are repeated across products in the same category, which is intentional. This is a denormalized dimension, and a later chapter compares it with snowflaking.
The grain of DimProduct is one row per product. The grain of FactSales is one row per order line. Both statements should be easy to say out loud for every table in your model. If you cannot state a table's grain in one sentence, the table is probably mixing levels.
Choosing columns for a dimension
Include any attribute a user might filter, group, or sort by. Typical examples are:
| Dimension | Useful attributes |
|---|---|
| Product | Name, subcategory, category, color, size, brand |
| Customer | Name, city, region, country, segment |
| Store | Name, city, region, format, open date |
| Date | Date, month, quarter, year, weekday, fiscal period |
Leave out columns that nobody slices by and that have very many distinct values, such as free-text notes or timestamps with seconds. They add memory use and clutter the field list without helping anyone.
Keep numbers that you intend to add up in the fact table. A product's list price is an attribute, because users might group by price band. A line's sales amount is a measurement and belongs in the fact table. If you are unsure, ask whether the value changes with each event (fact) or describes the thing involved in the event (dimension).
Hierarchies are columns, not tables
A hierarchy such as Category, Subcategory, Product is just a set of columns in one dimension table. In Power BI you can create a hierarchy in the Model view to give users a drill path, but the data lives in flat columns. Because each product belongs to one subcategory and each subcategory to one category, the repeated text costs little after compression.
Sort-by columns
Text columns sort alphabetically, which is wrong for months. A month column showing "Apr, Aug, Dec" is a common sign of a missing sort column. Add a numeric column such as Month Number to the dimension and set Sort by column on the month name. The date dimension in a later chapter uses this pattern. The same applies to any ordered label, such as "Small, Medium, Large".
Keys in dimensions
Each dimension needs a key that is unique and never blank. Two kinds are common:
- A business key comes from the source system, such as a SKU or customer number.
- A surrogate key is an integer you generate, with no business meaning.
Integer keys compress well and join quickly. If the source has text business keys, or the same key can mean different things over time, a surrogate key is safer. Whichever you use, the fact table must carry the same key as a foreign key, with the same data type.
Unknown members
Real data contains fact rows whose key has no match in the dimension, for example a sale with a missing customer. Power BI does not drop these rows. It groups them under a blank row that it adds to the dimension at query time. Totals stay correct, but the blank member confuses users. The usual fix is to add an explicit row such as -1 / Unknown to the dimension and load fact rows with a missing key against it.
Building a dimension in Power Query
When the source is a single flat table, you can derive a dimension by selecting the descriptive columns and removing duplicates on the key.
let
Source = SalesFlat,
ProductColumns = Table.SelectColumns(Source, {"ProductID", "ProductName", "Subcategory", "Category"}),
DimProduct = Table.Distinct(ProductColumns, {"ProductID"})
in
DimProductTable.Distinct with a column list keeps the first row for each ProductID. If a product appears with two different names in the source, one name is dropped silently. That hides a data quality problem, so check for it. Group by ProductID and count distinct names. Any count above 1 needs a decision from the data owner.
Dimensions and filter context
Dimensions matter in DAX because they are where filters start. When a user picks "Bikes" on a slicer, Power BI filters DimProduct to the rows with that category. The relationship passes those product keys to the fact table, and a measure then sums only the matching rows.
Total Sales = SUM ( Sales[SalesAmount] )
Total Quantity = SUM ( Sales[Quantity] )These measures contain no mention of products, customers, or dates. They do not need to. The filters arrive through the relationships. This is why you put filtering columns in dimensions. A slicer based on a fact table column filters only that table, and it cannot filter other facts that share the same dimension.
Common problems
- Descriptive text in the fact table. Repeating product names on millions of rows wastes memory and splits filtering across tables. Move them to a dimension.
- Duplicate keys. A key that repeats makes the relationship many-to-many or prevents it. Deduplicate in Power Query or fix the source.
- Mismatched key types. A text key in the dimension and an integer in the fact table will not relate. Convert one side.
- Mixed grain. A dimension with some rows at product level and some at category level gives wrong counts. Keep one level per table.
- Using a fact column on a slicer. The slicer will not filter other facts. Use the dimension column.
- Hidden blank members. A blank row in a visual means fact rows with no matching key. Find them and map them to an Unknown row.
Hide key columns in the report view once relationships exist, so users pick the descriptive columns instead. Hiding does not break relationships.
Exercise
A fact table has 5 million rows. Each row contains ProductID, ProductName, and Category along with SalesAmount. You are asked to build a dimension for these columns. Which columns go in the dimension, what is its grain, and what should you check before creating the relationship?
Show answer
The dimension holds ProductID (the key), ProductName, and Category, and the fact table keeps only ProductID and SalesAmount. The grain is one row per product. Before relating them, check that ProductID is unique in the dimension and that each ProductID has one consistent name and category. Also check that the key has the same data type on both sides and that no fact ProductID is missing from the dimension, which would show as a blank member.
Exercise
A matrix shows months in the order Apr, Aug, Dec, Feb. What is the likely cause and the fix?
Show answer
Month names are text and sort alphabetically. Add a numeric Month Number column to the date dimension and set the month name column to sort by it.