Sub topic 6
Putting it together: the star schema
This chapter assumes you can load data into Power BI Desktop, open the Model view, and read basic SQL. It also assumes you know what primary and foreign keys are, and that you have already chosen a grain for your fact table and met the fact and dimension table roles from the earlier chapters.
What the star schema is
A star schema is a model layout with one or more fact tables at the center and dimension tables around them. Each dimension connects directly to the fact table. No dimension connects to another dimension. Drawn in the Model view, the fact table sits in the middle and the dimensions radiate out like points of a star.
The arrows show the direction a filter travels. A selection on a dimension narrows the rows in the fact table.
The division of labor
Each table type has a single job.
| Table type | Stores | Typical shape |
|---|---|---|
| Fact | Keys and numeric columns, one row per event at the chosen grain | Many rows, few columns |
| Dimension | Descriptive attributes, one row per member | Fewer rows, many text columns |
Keeping these jobs separate makes the model predictable. Numbers to aggregate live in one place. Attributes to filter and group by live in another.
A minimal star in SQL
The following definition shows the pattern. DimProduct has a unique key. FactSales is at order-line grain and carries a foreign key to the dimension plus numeric columns.
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)
);Note what the fact table lacks. It has no product name or category. Those attributes belong to the dimension and are reached through ProductKey.
Why the shape matters
The relationship between a dimension and a fact table is one-to-many. One product row matches many sales rows. Because each key is unique on the dimension side, a filter on the dimension selects an unambiguous set of fact rows. This gives you three practical benefits:
- Predictable filtering. Slicers and visuals use dimension columns, and the filter reaches the fact table along one path.
- Simple measures. A measure such as
SUM ( Sales[SalesAmount] )needs no extra logic to respect the filters. - One place for each attribute. A product category is stored once, so a rename or correction happens in one row.
Common mistakes when assembling a star
- Linking dimensions to each other. This creates extra filter paths and can make results ambiguous. Connect each dimension to the fact table instead.
- Putting descriptive text in the fact table. This repeats the same strings on every row and enlarges the model.
- Skipping the grain decision. If the fact table mixes order-level and line-level rows, totals become wrong.
Exercise
A fact table has the columns OrderNumber, ProductKey, ProductName, Category, and SalesAmount. Which columns should move to a dimension table, and what should remain in the fact table to link to it?
Show answer
ProductName and Category describe the product, so they move to a product dimension keyed by ProductKey. The fact table keeps OrderNumber, ProductKey (the foreign key to the dimension), and SalesAmount (the numeric measurement).