Sub topic 2
Measures, aggregation and the field wells
This chapter assumes you can open Power BI Desktop, have a loaded data model with related tables, and know what sum, average and count mean. It explains how a visual gets its numbers, so you can choose the right field and the right aggregation before you choose a chart.
Where a visual's numbers come from
Every visual has field wells. These are the slots in the Data pane of the Visualizations pane, such as Axis, Legend, Values, X Axis and Y Axis. The wells fall into two groups:
- Grouping wells (Axis, Legend, Small multiples) take categorical, date or geographic fields. They decide how the data is split.
- Value wells (Values, Y Axis on a column chart, Size) take numbers. They decide what is calculated for each group.
A visual is a query. Power BI groups the rows by the fields in the grouping wells and calculates the value field for each group.
Implicit and explicit measures
When you drag a numeric column such as Sales[SalesAmount] into a value well, Power BI applies an implicit measure. It picks a default aggregation, usually Sum. You can switch it to Average, Count, Min, Max and so on from the well's drop-down list.
An explicit measure is a named DAX formula that you write once and reuse in any visual. Prefer explicit measures for three reasons:
- The calculation is defined in one place, so every visual agrees.
- Measures can do things a simple aggregation cannot, such as prior-year comparison.
- Measures keep working when you change the grouping, because they are evaluated for each group in the visual.
Total Sales = SUM ( Sales[SalesAmount] )Put [Total Sales] in the Values well of a bar, column, line or card visual. The visual then shows the sum for each category, month or filter state.
Measures that match the question
Different questions need different measures. These examples build on [Total Sales].
Sales LY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )Sales LY supports a line chart or KPI that compares a period with the same period a year earlier. It requires a date table that is marked as a date table.
Sales YoY % =
DIVIDE ( [Total Sales] - [Sales LY], [Sales LY] )DIVIDE returns a blank instead of an error when the prior-year value is zero or blank. This keeps cards and KPIs clean.
Sales % of Total =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALL ( Product[Category] ) )
)ALL ( Product[Category] ) removes the category filter from the denominator. Each category is then divided by the grand total, which suits a treemap or a bar chart ranked by share.
Measures and calculated columns
A calculated column is evaluated row by row when the data is refreshed, and it is stored in the table. Use one when you need a new grouping field, as in this price band.
Price Band =
SWITCH (
TRUE (),
Product[ListPrice] < 50, "Under 50",
Product[ListPrice] < 200, "50 to 199",
"200 and over"
)Place Price Band in the Axis well and a count of products in Values. The result is a simple distribution chart. In short, use a column to define groups and a measure to calculate values.
Common errors
- Summing a field that should not be summed, such as a unit price or a percentage. Use Average, or write a measure.
- Counting rows when you need distinct values. Count and Count (Distinct) give different answers.
- Putting a measure in a grouping well. Grouping wells need a field, not a measure.
- Using a time-intelligence function without a marked date table. The result is wrong or an error.
Exercise
You want a bar chart that ranks product categories by their share of total sales. Which measure do you put in Values, and why does it use ALL?
Show answer
Put Sales % of Total in Values and Product[Category] in Axis. The ALL function removes the category filter in the denominator, so each category's sales are divided by the grand total instead of by its own sales, which would always give 100%.