Sub topic 1
The building blocks of a Power BI model
A Power BI report is only as dependable as the model underneath it. Before you can design a star schema, you need a clear picture of what the model is made of and how the pieces interact. This chapter covers tables, columns, relationships, and measures, and shows how a click in a visual becomes a calculation over those pieces.
Prerequisites
This chapter assumes you can do the following:
- Load data into Power BI Desktop and open the Model view.
- Read a table as rows and columns, and explain what a primary key and a foreign key are.
- Read a simple Power Query step and a one-line DAX expression such as
SUM ( Sales[SalesAmount] ).
You do not need to know what a fact or dimension table is yet. The next chapters define both.
What a semantic model contains
Power BI calls the model behind a report a semantic model (older documentation says "dataset"). It has four kinds of objects:
| Object | What it is | Where it lives |
|---|---|---|
| Table | A named set of rows and columns | Loaded by Power Query or created with DAX |
| Column | One attribute of a table, with a data type | Inside a table |
| Relationship | A rule linking a column in one table to a column in another | Between two tables |
| Measure | A DAX formula evaluated at query time | Attached to a table, but not stored per row |
Tables and columns hold data. Relationships decide how filters travel between tables. Measures turn the filtered data into numbers. If a report shows a wrong number, the cause is almost always in one of these four places.
Tables and columns
Each table in the model comes from one of two places. A Power Query query loads it from a source, or a DAX expression creates it as a calculated table. Either way, the Model view treats it the same.
Every column has a data type, such as whole number, decimal, text, date, or true/false. The type matters for two reasons:
- Relationships work only between columns with compatible types. A text
"1001"does not match a whole number1001. - The VertiPaq storage engine compresses each column separately. Columns with few distinct values compress very well, and columns with many distinct values, such as a timestamp with seconds, compress poorly.
The second point is the reason model design affects file size and speed. You will return to it when you reach the performance chapter.
Calculated columns and measures
DAX can add logic in two ways, and learners often confuse them.
| Row | Calculated column | Measure |
|---|---|---|
| Evaluated | Once per row, at data refresh | At query time, for the current filters |
| Stored in the model | Yes, and it uses memory | No, only the formula is stored |
| Context | Row context | Filter context |
| Typical use | A value you slice or filter by | A number you aggregate |
Here is the same business idea written both ways. The example assumes a Product table with a UnitCost column and a Sales table related to it.
-- Calculated column on the Sales table: one stored value per row
Line Margin =
Sales[SalesAmount] - Sales[Quantity] * RELATED ( Product[UnitCost] )
-- Measure: computed on demand, nothing stored per row
Total Margin =
SUMX (
Sales,
Sales[SalesAmount] - Sales[Quantity] * RELATED ( Product[UnitCost] )
)The measure is usually the better choice for anything you add up. It adds no storage and respects whatever the report filters to. Use a calculated column when you need the value as an axis, a slicer, or a filter.
Relationships
A relationship connects one column in a table to one column in another. When two tables are related, a filter on one can reduce the rows seen in the other. Each relationship has these properties:
- From and to columns. These are normally a key and its matching foreign key.
- Cardinality. This is one-to-many, many-to-one, one-to-one, or many-to-many. In a one-to-many relationship, the "one" side holds unique values in the key column.
- Cross-filter direction. This is single or both. Single means filters flow only from the "one" side to the "many" side.
- Active or inactive. A pair of tables can have only one active relationship at a time. Inactive relationships exist in the model but are ignored until a measure activates them.
The most common healthy pattern is a one-to-many relationship, with single-direction filtering, from a table of descriptions to a table of events. Later chapters name these tables and explain why this pattern works.
Autodetected relationships
When you load tables, Power BI Desktop may create relationships by matching column names and types. Treat these as suggestions. Open the Model view after every load and check each relationship's columns, cardinality, and direction. A wrongly guessed relationship produces numbers that look plausible but are wrong.
From a click to a number: filter context
When a user selects a slicer value or places a field on a visual axis, Power BI builds a filter context. This is the set of filters in force for one calculation. The engine then does the following:
- It applies the filters to the tables they target.
- It propagates them along active relationships, in the allowed direction.
- It evaluates the measure over the rows that remain.
The same measure therefore returns different numbers in different cells of a matrix. Nothing in the measure changes. Only the filter context changes. This is why the model's shape matters so much: the relationships determine which filters can reach which tables.
Here is a minimal measure to see this at work.
Total Sales = SUM ( Sales[SalesAmount] )In a table visual with Product[Category] on rows, each row shows Total Sales for that category only. The measure contains no category logic. The relationship from Product to Sales supplies it.
Common errors at this level
These mistakes appear again and again in new models.
- Duplicate keys on the "one" side. If you try to create a one-to-many relationship on a column that has repeated values, Power BI offers many-to-many instead. Accepting this to make the error go away hides a data problem. Find the duplicates first.
- Mismatched data types. A key stored as text in one table and as a number in the other cannot be related. Fix the type in Power Query, not with a DAX workaround.
- Unmatched keys. If a
Salesrow carries a key that does not exist on the "one" side, the report groups it under a blank row. A visual showing "(Blank)" is usually this problem. - Summing in the wrong place. Dragging a numeric column into a visual creates an implicit measure that can behave differently from a measure you write. Define explicit measures for anything that matters, and hide the raw columns.
- Relationships left to chance. Several active paths between two tables make filter behavior ambiguous. Power BI blocks or deactivates some of them. Keep one clear path.
A quick inspection routine
Use this short routine whenever you open an unfamiliar model.
- In Model view, list the tables and note which ones hold events and which hold descriptions.
- Click each relationship line and read its cardinality, direction, and active state.
- Check that key columns share a data type on both sides.
- Find the measures. Note which tables hold them and whether any raw numeric columns are still visible to report authors.
This routine takes a few minutes. It tells you whether the model is already shaped well or needs the redesign the rest of this tutorial describes.
Exercise
A report shows a row labeled "(Blank)" with a large Total Sales value when you place Product[Category] on rows. The relationship from Product to Sales is one-to-many. What is the most likely cause, and where do you fix it?
Show answer
Some Sales rows carry a ProductKey value that does not exist in the Product table, so they cannot be matched to any category and are grouped under blank. Fix it at the source or in Power Query: add the missing products to the Product table, or correct the bad keys in Sales. Do not hide the blank row in the visual.
Exercise
You need a value that users can place on a slicer, and you also need a total that changes with the report filters. Which one should be a calculated column and which one a measure, and why?
Show answer
The slicer value should be a calculated column (or a regular column), because slicers need a stored value for each row. The total should be a measure, because it must be evaluated at query time in the current filter context and should not take up storage per row.