Sub topic 2
Building hierarchies for drilling
A hierarchy is an ordered set of fields that lets a visual move from a broad level to a finer one. This chapter covers how to build hierarchies and what they do to the data a visual shows. It assumes you can build bar charts and matrices in Power BI Desktop, you have a star-schema model with a date table, and you can write simple DAX measures.
Why hierarchies matter for drilling
Drill-down moves through levels in a fixed order, such as Category, then Subcategory, then Product. Power BI needs to know that order. A hierarchy supplies it. Each step down adds a filter to the visual's filter context, so the measure is recalculated for a smaller slice of the data.
Power BI gives you two kinds of hierarchy:
- Date hierarchy. Power BI creates one automatically for date columns when auto date/time is on. It has Year, Quarter, Month, and Day levels.
- Custom hierarchy. You build it yourself from columns in one table, for example Category, Subcategory, and Product Name.
The automatic date hierarchy
When you drop a date column into an axis, Power BI may show a hierarchy with Year, Quarter, Month, and Day. This works for quick reports. In a model with a proper date table, the better practice is to build your own hierarchy on that table. Then you control the level names, the sort order, and the fiscal calendar.
Building a custom hierarchy
Build the hierarchy on the table that holds all the levels. In a star schema this is usually a dimension table such as Product.
- Open Report view or Model view and find the Product table in the Data pane.
- Right-click the top-level column,
Category, and choose Create hierarchy. - Power BI creates a hierarchy with that column as its first level.
- Drag
Subcategoryonto the hierarchy, then dragProduct Nameonto it. - Rename the hierarchy to something readable, such as
Product Hierarchy.
The order of the levels matters. The top level is the broadest, and each level below must be more detailed than the one above it.
The diagram below shows the structure you just built.
Using the hierarchy in a visual
Drag the hierarchy, not the individual columns, into the Axis field well of a bar chart. The visual then shows the top level. Turn on drill mode with the down-arrow icon in the visual header, and a click on a bar moves to the next level for that bar.
Two details commonly cause confusion:
- If you put the three columns in the well separately, you get the same levels. The difference is that a saved hierarchy is reusable across visuals and keeps its order.
- A hierarchy can also be built in the field well itself. Adding several fields to one Axis well creates a drill path for that visual only.
Hierarchies and filter context
Each level of a hierarchy is a column, and each drill step changes which column is in scope. At the Category level, a bar for "Bikes" filters the visual to Category = Bikes. After drilling down, the visual shows Subcategory values, still filtered to Bikes. Your measures do not need to change for this to work, because the same measure is evaluated under a narrower filter.
Sometimes a measure must behave differently at each level. For that, ISINSCOPE tells you which level is showing:
Level Label =
IF (
ISINSCOPE ( Product[Product Name] ), "Product",
IF (
ISINSCOPE ( Product[Subcategory] ), "Subcategory",
IF ( ISINSCOPE ( Product[Category] ), "Category", "Total" )
)
)The test starts at the lowest level. When the visual shows Product Name, Subcategory and Category are also in scope, so checking the top level first would always return "Category".
Common errors
| Symptom | Likely cause |
|---|---|
| Drill arrows are missing | The visual has only one field in the axis, or it uses a measure only |
| Levels appear in the wrong order | The hierarchy levels were added in the wrong sequence |
| Months sort alphabetically | The month name column has no sort-by-column set to the month number |
| Hierarchy cannot be built | The columns come from different tables |
Exercise
Your Product hierarchy has the levels Category, Subcategory, and Product Name. In a measure that uses ISINSCOPE to return a level label, why do you test Product Name first?
Show answer
When the visual shows Product Name, the higher levels Subcategory and Category are also in scope. If you tested Category first, the measure would return "Category" at every level. Testing from the lowest level up returns the correct label.