Sub topic 1
The building blocks of a Power BI model
A Power BI semantic model is a set of tables, the columns inside them, and the relationships that connect the tables. This chapter names those parts and shows how a star schema sorts tables into two roles. Later chapters cover each role in depth.
Prerequisites
This chapter assumes you can:
- Load data into Power BI Desktop and open the Model view.
- Explain what a table, row, column, primary key, and foreign key are.
- Read a simple DAX expression such as
SUM ( Sales[SalesAmount] ).
Tables, columns, and relationships
A model has three kinds of parts:
- Tables hold rows of data. Each table comes from a query, a calculated table, or an imported source.
- Columns describe each row. Some are text or dates you filter by. Others are numbers you add up.
- Relationships link one column in a table to one column in another table. They tell the engine how a filter on one table should reach another.
The Model view shows tables as boxes and relationships as lines between them. The layout of those boxes matters more than any single table.
Two roles: facts and dimensions
In a star schema every table plays one of two roles.
| Role | Stores | Typical columns | Row count |
|---|---|---|---|
| Fact table | Events or measurements, such as one sale | Foreign keys and numeric columns | Large, grows over time |
| Dimension table | Descriptions of the things involved in events | A unique key and descriptive attributes | Small, changes slowly |
A fact table answers "what happened and how much". A dimension table answers "who, what, where, and when". You group and filter by dimension columns, and you aggregate fact columns.
A first example
The two tables below show both roles. DimProduct describes products. FactSales records one row per order line and points to the product by 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)
);DimProduct has one row per product, and ProductKey is unique. FactSales has one row per order line. It holds ProductKey as a foreign key, plus the numbers Quantity and SalesAmount. In Power BI you would relate DimProduct[ProductKey] to FactSales[ProductKey].
The shape of the model
With one fact table and several dimensions, the Model view looks like a star. The fact table sits in the center and each dimension connects to it.
The arrows show the direction filters travel: from the dimension, the "one" side, to the fact table, the "many" side. A filter on Category in DimProduct therefore limits the rows of FactSales that get summed.
Common errors at this stage
- Loading one wide table that mixes descriptions and numbers. It works for small data but hides the two roles and grows large.
- Relating two tables on a column that is not unique on the "one" side. Power BI then cannot create a one-to-many relationship.
- Treating a numeric column, such as a year, as something to sum. Only fact columns that measure something should be aggregated.
Exercise
A table has one row per customer and the columns CustomerID, Name, City, and Segment. Is it a fact table or a dimension table, and why?
Show answer
It is a dimension table. It has one row per customer, a unique key (CustomerID), and descriptive attributes you can group or filter by. It holds no events or numeric measurements.
Exercise
In the example, which table is on the "one" side of the relationship on ProductKey, and which direction does a filter travel?
Show answer
DimProduct is on the "one" side because ProductKey is unique there. A filter travels from DimProduct to FactSales, so selecting a category limits the sales rows that are summed.