Sub topic 9
Writing measures against the star
This chapter assumes you can load tables into Power BI Desktop, you know the difference between a fact table and a dimension table, and you have already created one-to-many relationships from dimensions to a fact table. It shows how to write DAX measures that rely on those relationships.
Why measures stay simple on a star
On a star schema, a measure rarely needs to know about dimensions. It aggregates one fact column. The relationships carry the filters.
When a user puts Product[Category] on a visual rows axis or picks a month in a slicer, Power BI filters the dimension table. The one-to-many relationship then passes that filter to the fact table. The measure sums only the fact rows that remain.
Total Sales = SUM ( Sales[SalesAmount] )
Total Quantity = SUM ( Sales[Quantity] )Neither measure mentions product, date, or customer. Yet both respond to every slicer and visual on the page. This is the main benefit of the model: you write the aggregation once, and the filter context supplies the slicing.
Measures versus calculated columns
Use a measure when the result depends on the current filters. Use a calculated column only when you need a value stored per row, for example to use as a slicer field.
A measure is calculated when the visual is drawn and takes up no space in the model. A calculated column is stored for every row and increases model size. Totals, ratios, and averages belong in measures.
Building on base measures
Write simple base measures first. Then reference them in other measures. This keeps the logic in one place.
Average Price = DIVIDE ( [Total Sales], [Total Quantity] )
Sales Share of Category =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALL ( Product[ProductName] ) )
)DIVIDE returns blank instead of an error when the denominator is zero. ALL ( Product[ProductName] ) removes the product name filter but keeps other filters, such as the category on the row. The share is therefore each product's sales over its category's sales.
Changing the filter with CALCULATE
CALCULATE evaluates an expression under a modified filter context. Filter on dimension columns, not on fact columns, so the filter travels through the same relationships as every other filter.
Bike Sales =
CALCULATE ( [Total Sales], Product[Category] = "Bikes" )The same idea covers the inactive relationship from the role-playing chapter. USERELATIONSHIP switches which date column the measure uses:
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDateKey], 'Date'[DateKey] )
)Common errors
- Aggregating dimension columns. Summing a column from a dimension gives wrong totals, because the dimension has one row per member, not per event.
- Filtering on fact columns. A filter such as
Sales[ProductKey] = 5ignores the dimension attributes and is hard to read. UseProduct[ProductName]. - Repeating logic. Copying
SUM ( Sales[SalesAmount] )into many measures makes later changes error-prone. Reference[Total Sales]instead. - Blank dimension members. If a fact row has a key with no match in the dimension, it shows under a blank member. Check keys when a total looks right but a breakdown does not add up.
Exercise
A report shows Total Sales by Category correctly, but a new measure Sales Share shows 100% on every row. The measure is DIVIDE ( [Total Sales], CALCULATE ( [Total Sales], ALL ( Product ) ) ). How would you change it so each row shows its share of overall sales, and why was the result 100%?
Show answer
The result was 100% on every row because the expression was not the cause. Check the actual behavior: ALL ( Product ) removes the Product filter, so the denominator is the grand total, and each row's share should be below 100%. A result of 100% on every row therefore means the visual is not filtering the Product table. The category on the rows probably comes from a different table, such as a column in the fact table, or the relationship between Product and Sales is missing or inactive. Fix the relationship, or use the Product[Category] column in the visual. With a working relationship, the measure returns each category's share of the grand total.