Sub topic 3
Moving Between Hierarchy Levels with COLLAPSE and EXPAND
Assume you know basic Power BI visuals, matrix hierarchies, and DAX measures. Visual calculations run on the rows and columns currently displayed in one visual.
Moving between hierarchy levels
A matrix might contain Year, Month, and Product Category rows. A visual calculation can move between these levels without rebuilding the model.
COLLAPSEevaluates a value at a higher level of the hierarchy.EXPANDevaluates a value at a lower, more detailed level.- The
ROWSaxis tells the function to navigate the row hierarchy.
For example, this calculation compares each row with its parent:
Percent of parent = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))At a Month row, COLLAPSE returns the Year value. At a Product Category row, it returns the value for the containing Month or other parent level.
Percent of parent and grand total
Use COLLAPSEALL to reach the highest row level, usually the grand total:
Percent of grand total = DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS))Use DIVIDE because it safely handles a zero or blank denominator.
Hidden fields and formatting
A calculation can use a field that is hidden from the visual. Keep a date or category field in the visual when it controls the calculation, then hide it from display through the field's visual options. The field remains part of the calculation context.
Set the result's data type and format in the visual calculation editing experience. Format percentages as Percentage and financial results as Currency. Correct formatting prevents users from reading 0.25 as 0.25 percent instead of 25 percent.
Limitations and choosing a measure
Use a visual calculation for visual-only results such as percent of parent, a running total, or a comparison between displayed rows. Use a measure when the same logic must work across many visuals, filter independently, appear in a model, or support relationships and reusable business rules.
Visual calculations cannot replace model calculations that must exist before the visual is evaluated.
Ranking and lookup functions
Advanced visual calculations include ranking and lookup functions. Ranking can order displayed categories. Lookup functions can retrieve a value from another displayed position. These functions also depend on the visual's axis, sorting, and visible rows, so test them after changing the layout.
Exercise
Which function should you use for a category's share of its parent, and which function reaches the grand total?
Show answer
Use COLLAPSE for the parent value and COLLAPSEALL for the highest hierarchy level.