Sub topic 10
When you need a column: slicers, axes, filters and sorting
This chapter assumes you know Power BI Desktop's Report, Table and Model views, how a star-schema model fits together, and basic DAX such as SUM and IF. It also assumes you know the difference between row context and filter context from earlier chapters.
The rule: fields that group or filter must be columns
A measure returns one value for each filter context. It has no list of values that a visual can place on an axis or in a slicer. A column does. Each row holds a value, and the distinct values become the items a user can pick or group by.
So when you want to slice, group, or filter by something, you need a column. A measure can still supply the numbers that fill the visual.
| Visual element | Needs a column? | Reason |
|---|---|---|
| Slicer items | Yes | The slicer lists the distinct values in a column |
| Chart axis or legend | Yes | The visual groups rows by a column's values |
| Table row headers | Yes | Each row is one distinct column value |
| Visual, page or report filter on a field | Yes | A filter keeps rows where the column matches |
| Values in a chart or table | Usually a measure | The value must change with each group |
Example: a price band
Suppose you want a slicer that separates high-priced products from the rest. The list price is already a column, so you can derive a label from it with a calculated column on the Product table.
Price Band = IF(Product[List Price] >= 100, "High", "Standard")DAX evaluates this expression once per Product row at refresh time and stores the result. Drop Product[Price Band] on a slicer and it offers two items, High and Standard. Picking one filters the Product table. The relationship then carries that filter to Sales, so your measures respond.
Total Sales = SUM(Sales[Line Amount])Place Price Band on a chart axis and Total Sales as the value. Each bar shows the sales for the rows in that band.
A measure cannot do this job. You could write a measure that returns "High" or "Standard", but it would return a single value for the current context. A slicer cannot list the values a measure might return.
Sorting a text column
A text column sorts alphabetically by default. "High" comes before "Standard", which happens to work here. Month names are a common failure, because April would come before January.
To fix it, add a numeric column that gives the correct order, such as a month number. Select the text column, then in the Column tools ribbon choose Sort by column and pick the numeric one. The sort column must have one value for each value in the text column, or Power BI reports an error.
Sorting is another job only a column can do, since the sort order is a property of a column.
Where to create the column
A calculated column is one place to build the field. Other places often fit better:
- Power Query or the data source. If the label depends only on the same row, you can build it before the data enters the model. This is covered in a later chapter.
- A calculated column in DAX. Use it when the logic needs a DAX function such as
RELATED, or when it must use other model tables.
Both create a stored column. The decision is about where the logic is easiest to maintain.
Check your understanding
Exercise
A colleague wants a slicer with the options "Profitable" and "Loss-making" for each product, based on the product's cost and list price. The two values are columns on the Product table. Should this be a calculated column or a measure? Write the expression.
Show answer
A calculated column, because a slicer needs a column that holds a value for each row. For example: Profitability = IF(Product[List Price] > Product[Standard Cost], "Profitable", "Loss-making"). It is evaluated per Product row at refresh time and gives the slicer two items. A measure cannot supply slicer items.
Exercise
You add a text column Month Name to a Date table, and a chart axis shows April before January. What do you add, and which setting do you change?
Show answer
Add a numeric column such as Month Number, with one number for each month name. Select Month Name and use Sort by column in Column tools, choosing Month Number.