Sub topic 2
Building hierarchies for drilling
A hierarchy is an ordered list of fields that lets a visual move from a broad level to a finer one. Drill-down, covered in the next chapter, depends on it. This chapter covers how to build hierarchies, how they behave in a visual, and the mistakes that make drilling fail.
Prerequisites
This chapter assumes you can do the following:
- Navigate Power BI Desktop: Report view, Model view, the Fields pane, and the Visualizations pane.
- Build simple bar charts, tables, and matrices.
- Work with a star-schema model that has a date table.
- Write simple DAX measures.
You should also know that every visual shows data filtered by the filters that reach it (its filter context). A hierarchy changes which field a visual groups by. It does not change the filtering rules.
What a hierarchy is
A hierarchy is a set of columns from one table, arranged from the highest level to the lowest. A typical example for a product table is Category, then Subcategory, then Product Name.
Each level is a column that already exists. The hierarchy does not store new data. It records the order of the levels, so a visual can swap one level for the next when a reader drills.
Hierarchies come in two kinds:
| Kind | Where it comes from | Typical levels |
|---|---|---|
| Date hierarchy | Created automatically for date columns when auto date/time is on | Year, Quarter, Month, Day |
| Custom hierarchy | Built by you in the model | Category, Subcategory, Product Name |
Date hierarchies
When auto date/time is enabled, Power BI creates a hidden date table for each date column. Dragging the date column into a visual shows a Date Hierarchy with Year, Quarter, Month, and Day. You can remove levels you do not need with the X beside each level in the field well.
This setting is convenient for small models. In a model with a dedicated date table, which this tutorial assumes, you normally turn auto date/time off. You then build the hierarchy yourself on your own date table. This gives you control over the level names, the sort order, and the fiscal calendar.
Build a date hierarchy on your own date table
- Open Model view or the Data pane in Report view.
- Right-click the
Yearcolumn in theDatetable and choose Add to hierarchy, then New hierarchy. - Right-click
Quarterand choose Add to hierarchy, then pick the new hierarchy. - Repeat for
MonthandDate. - Rename the hierarchy to something clear, such as
Calendar.
Make sure the month column sorts by month number. Otherwise a drilled visual lists months alphabetically (April, August, December). Select the Month column, open Column tools, choose Sort by column, and pick Month Number.
Custom hierarchies
You create a custom hierarchy the same way. Right-click the top-level column in the Data pane, choose Add to hierarchy, then add the lower levels in order.
You can also drag one column onto another in the Data pane to start a hierarchy. Reorder levels by dragging them inside the hierarchy.
For the product table, the result looks like this:
- Product hierarchy
- Category
- Subcategory
- Product Name
Rules that keep a hierarchy usable
- All levels must come from the same table. A hierarchy cannot mix columns from
ProductandCustomer. To combine them, bring the columns into one table or use a different technique, such as separate visuals. - Order the levels from the broadest to the most detailed. A reader drills from the top level to the bottom one.
- Choose levels that form a real parent and child relationship. Each Subcategory should belong to one Category. If "Accessories" appears under two categories, the drilled result may mix rows in a confusing way.
- Keep the hierarchy short. Three or four levels are enough for most visuals.
How a visual uses a hierarchy
Place the hierarchy, or its individual columns, in the Axis of a chart (or in Rows of a matrix). The visual starts at the first level. Because the levels sit in one ordered field, Power BI knows what the next level down is.
You can also build a drillable axis without a saved hierarchy. In the X-axis well, add several columns from the same or different tables, in order. A visual treats them like a hierarchy for drill-down. A saved hierarchy is still better when many visuals reuse the same levels, because you define it once and every report page gets the same structure.
The table below compares the two approaches.
| Approach | Strength | Limit |
|---|---|---|
| Saved hierarchy in the model | Reusable across visuals and reports, one definition | Levels must be in one table |
| Several fields in the axis well | No model change, columns can come from different tables | Rebuilt for every visual |
How hierarchy levels relate to filter context
Each level in the axis adds a grouping to the visual. When a visual shows Category only, each bar is the filter context for one category. When the reader drills down on the Bikes bar, the visual keeps the filter Category = Bikes and now groups by Subcategory.
This is why the measure in the visual does not need to change. A measure such as [Total Sales] evaluates in whatever filter context the current level supplies. The hierarchy supplies the grouping. The measure supplies the calculation.
flowchart TDThe diagram below shows the flow for one drilled bar.
Detecting the level in DAX
A measure sometimes needs to know which level a visual shows. The function ISINSCOPE returns TRUE when a column is a grouping level in the current visual. Test the lowest level first, because when the visual shows the lowest level, the higher levels are also in scope.
Level Label =
IF (
ISINSCOPE ( Product[Product Name] ), "Product",
IF (
ISINSCOPE ( Product[Subcategory] ), "Subcategory",
IF ( ISINSCOPE ( Product[Category] ), "Category", "Total" )
)
)Put this measure in a matrix that has the product hierarchy on its rows. The row for each category shows "Category". Expanding a category shows "Subcategory" on the child rows, and expanding again shows "Product". The grand total row shows "Total".
Do not use HASONEVALUE for this job. It reports whether one value remains in filter context, which can be true for reasons other than the visual's grouping. For example, a slicer that selects one category makes HASONEVALUE ( Product[Category] ) true even on the total row.
Building a hierarchy: worked example
Use the following steps to build and test a product hierarchy.
- In the Data pane, right-click
Product[Category]and choose Add to hierarchy, then New hierarchy. - Right-click
Product[Subcategory]and add it to the same hierarchy. - Right-click
Product[Product Name]and add it to the same hierarchy. - Rename the hierarchy
Product Hierarchy. - Add a clustered bar chart. Drag
Product Hierarchyinto Y-axis and[Total Sales]into X-axis. - Turn on drill mode with the down-arrow icon in the visual header. Click a bar. The visual shows the subcategories for that category.
- Add the
Level Labelmeasure to a table to confirm which level each row belongs to.
If the visual shows all categories after the click, drill mode was off, and the click only cross-filtered other visuals. The next chapter covers the modes in detail.
Common problems
The drill arrows are missing
The arrows appear only when the axis holds more than one level. Check that the field well contains the hierarchy, not just the top column. If you dragged only Category, the visual has no lower level to reach.
Levels appear in the wrong order
Text columns sort alphabetically. Set Sort by column to a numeric helper column, such as Month Number, Quarter Number, or a custom rank.
Blank values appear as a level
If some rows have no Subcategory, the visual groups them under (Blank). Fix the source data, or replace blanks in Power Query with a label such as "Unassigned".
A child value appears under two parents
A Subcategory name that repeats across categories can produce a clean display, but it can hide real structure. Name them clearly, or use a combined key in the data. Check the relationship with a table visual that shows Category and Subcategory side by side.
The hierarchy does not appear in another table's visual
A hierarchy belongs to its table. It does not show up in the field list of a different table's group.
Exercise
A matrix has Product[Category] on its rows and the Level Label measure from this chapter in its values. The reader expands a category to show its subcategories. What does the measure return on the category row, on a subcategory row, and on the grand total row?
Show answer
It returns "Category" on the category row, "Subcategory" on each subcategory row, and "Total" on the grand total row. On each row, ISINSCOPE checks which column is a grouping level for that row, starting with the lowest level. The grand total has no grouping column in scope, so the final fallback applies. This assumes the matrix rows hold Category followed by Subcategory in the row well.
Exercise
You try to create a hierarchy with Product[Category] and Customer[Region]. Why does it fail, and what are two ways to still get a drillable axis?
Show answer
A hierarchy can only contain columns from one table. You can place both columns in the axis well of a visual, in the order you want, which gives drill-down without a saved hierarchy. Or you can bring the needed columns into a single table, for example with a merge in Power Query, and then build the hierarchy on that table.