Sub topic 10
When you need a column: slicers, axes, filters and sorting
This chapter assumes you can move between Report, Table and Model views in Power BI Desktop. It also assumes you know a star-schema model and basic DAX such as SUM, IF and column references. The previous chapters explained that a measure is computed at query time in the current filter context, and that a calculated column is computed at refresh time in row context. Here you will see the practical consequence: some report features only accept columns.
Why some visuals need a column
A slicer, a chart axis, a legend, a matrix row header and a report filter all work the same way. Each one lists the distinct values of a field and uses the chosen values to filter the model. A column has stored values that the engine can list. A measure has no stored values. Its result depends on the filter context, and the visual itself creates that context. If a measure could also supply the groups, the groups would depend on the result they are supposed to define.
So a measure can't be the field you slice by. It can only be what you display for each slice.
| Report feature | Accepts a column | Accepts a measure |
|---|---|---|
| Slicer field | Yes | No (not directly) |
| Chart axis or legend | Yes | No |
| Matrix row or column headers | Yes | No |
| Filter pane, as the field being filtered | Yes | Yes, as a visual-level filter on the value |
| Value, tooltip or card | Yes, after aggregation | Yes |
| Sort By Column | Yes, as the sort key | No |
A measure can be used in the filter pane, but only as a visual-level condition such as "Total Sales is greater than 10000". It filters the groups a visual has already formed. It does not create the groups.
Creating a slicer field with a calculated column
Suppose the business wants to see sales split between high-priced and standard products. The Product table has a numeric List Price. Nobody can put List Price on a slicer in a useful way, because it has hundreds of distinct values. A calculated column on the Product table turns it into a small set of labels.
Price Band = IF(Product[List Price] >= 100, "High", "Standard")The column is evaluated once for every Product row during refresh. Drop Price Band on a slicer and you get two choices. Drop it on a chart axis and you get two bars. A measure such as Total Sales then fills in the value for each bar, in the filter context that the axis or slicer creates.
This is the standard division of labor. The column defines the groups. The measure calculates the number for each group.
Columns from related tables
The grouping you need is often on a different table from the one holding the facts. Chapter 7 covered RELATED. Combined with slicing, it lets you place a grouping on the table where the visual expects it.
Product Category = RELATED(Product[Category])This calculated column on Sales copies the category onto every sales row. Usually you do not need it for slicing. In a star schema, you can put Product[Category] directly on a slicer and the relationship filters Sales. Add a copy to Sales only when you need the value inside another row-level calculation on Sales, or when the model has no relationship that would carry the filter.
A common mistake is to create Product Category on Sales and use that for the slicer, while other visuals use Product[Category]. The two fields then filter independently of each other and the report shows confusing results. Keep one field for slicing and hide the duplicate.
Filtering with columns
Report filters, CALCULATE filter arguments and row-level security rules all compare column values. This measure changes the filter context for one column:
Red Sales = CALCULATE([Total Sales], Product[Color] = "Red")Product[Color] is an existing column, so no new column is needed. If the condition you need is not already a column, a calculated column can hold it, such as a flag or a band. In many cases you can avoid that, because CALCULATE accepts boolean expressions over columns directly:
High Price Sales = CALCULATE([Total Sales], Product[List Price] >= 100)This measure needs no Price Band column. Choose the column when the grouping will also appear on slicers or axes, or when several measures and visuals reuse it. Choose the inline filter when only one measure needs the condition.
Sorting a column by another column
Text columns sort alphabetically by default. That is wrong for months, weekdays and ordered bands. A month name column sorts as April, August, December, and so on.
The fix is a second column that holds the correct order, and the Sort by column setting. Both columns must live on the same table.
Month Name = FORMAT('Date'[Date], "MMMM")Month Number = MONTH('Date'[Date])To apply the sort in Power BI Desktop:
- Open Table view or Report view and select the
Month Namecolumn. - On the Column tools ribbon, choose Sort by column.
- Select
Month Number.
The same technique works for the price band. Without it, "High" sorts before "Standard" alphabetically, which happens to look right here. If you rename the bands to "Premium" and "Budget", the order flips and you need a numeric key.
Price Band Order = IF(Product[List Price] >= 100, 1, 2)Each value of the name column must map to exactly one value of the sort column. If Month Name had two different sort values for "March", Power BI reports an error when you apply the sort.
A measure can't be a sort key and can't be sorted by another column. Sorting inside a visual by a measure value is a different feature. It orders the rows by the result, and you pick it from the visual's sort menu.
What to check when a column does not behave
- The slicer shows every price instead of bands. You placed
List Priceon the slicer instead ofPrice Band. Check the field name. - A grouping shows a blank row. Some rows returned BLANK from the expression, or
RELATEDfound no matching row. Check for unmatched keys in the relationship. - The sort fails. The name column maps to more than one sort value, or the two columns are on different tables.
- A new column does not appear in the visual. Calculated columns appear in the Data pane at once, but a column inside a hidden table or a hidden column stays out of the list. Unhide it.
- The slicer changes totals you did not expect. Check which table the slicer field belongs to and whether the relationship direction filters the fact table. A slicer on a disconnected table filters nothing until a measure reads its selection.
The cost side
Every grouping column you add is stored in the model and increases refresh time and memory use. Columns with few distinct values, such as Price Band, compress well and cost little. A column with a unique value on every row costs much more. Chapter 9 covered this in detail. The practical rule here is to create grouping columns when a report feature needs them, and to avoid creating columns only to feed a single measure that could use an inline filter.
Exercise
A report needs a slicer that lets users pick "Weekday" or "Weekend" for each sale, based on the Date table's Date column. A colleague proposes a measure that returns "Weekday" or "Weekend". Explain why this fails and write a calculated column on the Date table that works. Then say what extra step is needed so the days of the week appear in Monday-to-Sunday order on a chart axis.
Show answer
A slicer needs a field with stored values to list, and a measure has none. Its result depends on the filter context, which the slicer itself creates. Use a calculated column instead: Day Type = IF(WEEKDAY('Date'[Date], 2) >= 6, "Weekend", "Weekday"). WEEKDAY with return type 2 numbers Monday as 1 and Sunday as 7. For the axis order, create a Day Name column, for example FORMAT('Date'[Date], "dddd"), and a Day Number column, WEEKDAY('Date'[Date], 2). Then select Day Name and use Sort by column to choose Day Number, so the names are no longer sorted alphabetically.