Sub topic 6
Putting it together: the star schema
This chapter assumes you know what a fact table and a dimension table are, that you have chosen a grain, and that you understand primary and foreign keys. You should also have opened the Model view in Power BI Desktop. Here you combine those pieces into one shape, the star schema, and learn how to check that a model has it.
What a star schema is
A star schema is a model layout in which each fact table sits in the center and connects directly to several dimension tables. Each connection is a one-to-many relationship. The dimension is on the "one" side and the fact is on the "many" side. When you draw the tables in the Model view, the fact table is in the middle and the dimensions surround it, which gives the star its name.
The layout follows a few rules:
- Facts hold events and numeric columns, plus a foreign key to each dimension.
- Dimensions hold descriptive attributes and one unique key each.
- Dimensions connect to facts and not to each other.
- Report users filter, group, and slice by dimension columns. They aggregate fact columns.
Why this shape works
A star schema gives you predictable filtering. A slicer on DimProduct[Category] filters the products. The relationship then passes that filter to the matching rows in FactSales. A measure such as SUM ( Sales[SalesAmount] ) then sums only those rows. Every dimension behaves the same way, so you can combine slicers freely.
The shape also keeps the model small. The VertiPaq engine, which is the in-memory columnar storage engine behind Power BI, compresses each column on its own. Descriptive text such as product names is stored once per product in a dimension and not once per sale in the fact table. The fact table keeps only compact key columns and numbers. Chapter 14 covers the size and speed effects in more detail.
Building the schema step by step
Start from the grain, then work outward.
- State the grain of the fact table in one sentence. For example: "one row per order line."
- List the questions people will ask, such as sales by product, by month, or by store. Each "by" names a dimension.
- Give each dimension a unique key and put the matching foreign key in the fact table.
- Move descriptive text out of the fact table and into the dimensions.
- Keep numeric, additive columns in the fact table.
- Create one relationship per dimension.
The SQL below shows steps 3 to 5 for a product dimension and a sales fact table.
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 has one row per product, so ProductKey is unique. FactSales has one row per order line, and the pair OrderNumber and OrderLineNumber identifies it. ProductKey in the fact table is the foreign key that points back to the dimension.
Power BI does not enforce primary or foreign key constraints. The database does it in SQL, but the Power BI model only relies on the "one" side being unique. If the key column in a dimension contains duplicates, the relationship cannot be created as one-to-many, or it falls back to many-to-many. Check key uniqueness before you build relationships.
Relationship settings in a star
When you create a relationship in the Model view, check these settings.
| Setting | Star schema value | Reason |
|---|---|---|
| Cardinality | One to many (dimension to fact) | The dimension key is unique and the fact key repeats. |
| Cross-filter direction | Single | Filters flow from the dimension to the fact and not back. |
| Active | Yes for the main relationship | Only one active path can exist between two tables. |
| Key columns | Same data type on both sides | A type mismatch can block or slow the relationship. |
Power BI often detects relationships automatically and may choose the wrong direction or cardinality. Open each relationship and confirm these values instead of trusting the defaults.
How a filter travels through the star
Consider a report page with a slicer set to Category = "Bikes" and a card showing Total Sales. The steps are:
- The slicer filters
DimProductto the rows where Category is "Bikes". - The relationship on
ProductKeypasses the filter fromDimProducttoFactSales. FactSaleskeeps only the rows whoseProductKeymatches a remaining product.- The measure sums
SalesAmountover those rows.
Total Sales = SUM ( Sales[SalesAmount] )
Total Quantity = SUM ( Sales[Quantity] )These measures contain no filter logic. The star schema supplies the filtering through the relationships, so one measure answers every slice a user can build. The measures use a table named Sales while the SQL names it FactSales. In Power BI the table's name in the model is what DAX uses, and you can rename it when you load it.
Filters flow in one direction only. A filter on FactSales does not reach DimProduct when the relationship is single direction. For example, selecting one fact row in a table visual does not narrow the product slicer. This is the intended behavior, and it prevents the ambiguous results that bidirectional filters can cause.
Common problems when assembling the star
- Dimension keys that are not unique. Duplicate product IDs stop the "one" side from being valid. Remove duplicates in Power Query or fix the source.
- Fact rows with no matching dimension row. Power BI groups them under a blank member in visuals. Add an "Unknown" row to the dimension and map missing keys to it.
- Attributes left in the fact table. Columns such as ProductName or CustomerCity in the fact table bring back the single wide table. Move them to dimensions.
- Dimensions linked to each other. A link from
DimProducttoDimCategorycreates a snowflake. Flatten it unless you have a reason to keep it. Chapter 11 covers that choice. - Mixed grains in one fact table. If some rows are order lines and others are order totals, sums double count. Use one fact table per grain.
Checking your model
Use this short checklist before you write many measures:
- Every fact table has a stated grain.
- Each relationship goes from a dimension (one) to a fact (many), with single direction filtering.
- Every dimension key is unique and has no blanks.
- Visuals use dimension columns for axes, legends, and slicers, and fact columns only inside measures.
- Hide the foreign key columns in the fact table, so report authors use the dimension columns instead.
A quick test is to place a dimension column and Total Sales in a table visual, then compare the grand total with a query against the source. If the totals match for every dimension, the star is working.
Exercise
A model has a table Orders with one row per order line. It contains OrderNumber, OrderLineNumber, ProductID, ProductName, Category, CustomerName, and SalesAmount. Describe the star schema you would build. State the grain, the tables, the keys, and the relationships.
Show answer
The grain is one row per order line. Build a fact table FactSales with OrderNumber, OrderLineNumber, ProductID, CustomerID, and SalesAmount. Build DimProduct with ProductID (unique key), ProductName, and Category, and DimCustomer with CustomerID (unique key) and CustomerName. Create two one-to-many relationships, DimProduct[ProductID] to FactSales[ProductID] and DimCustomer[CustomerID] to FactSales[CustomerID], both with single direction filtering. Remove the descriptive columns from the fact table.
Exercise
A slicer on a dimension column filters a measure correctly, but selecting a row in a table visual built from the fact table does not change the slicer's available values. Is this a defect? Why?
Show answer
No. With single direction relationships, filters flow from the dimension to the fact and not the other way. This is the designed behavior of a star schema, and it keeps filter results unambiguous.