Matrix visual in Power BI: rows, columns, subtotals

How the matrix visual in Power BI groups rows and columns with hierarchies, expands and collapses levels, shows subtotals, the stepped layout, values on rows, frozen headers, custom totals, and the DAX that keeps subtotals right.

Short answer: a matrix is a table with groups on both sides, like a pivot table in Excel: fields in Rows and Columns make the headers, measures in Values fill the cells, and each level gets a subtotal. Put several fields in Rows to make a hierarchy that people expand and collapse with the plus icons. Use it for "this measure by that and by that", for example revenue by category and month.

A table can list revenue for every category and month as 120 rows. A matrix shows the same numbers as 10 rows by 12 columns, with a total for each category and each month, and lets the reader open one category to see its products.

What does a matrix look like?

CategoryQ1Q2Q3Q4Total
Bikes4105204806902,100
Bikes · Mountain190240230330990
Bikes · Road150190170250760
Bikes · Touring709080110350
Accessories95110120150475
Clothing608570105320
Total5657156709452,895
Revenue by category and quarter, with subtotals; Bikes expanded to its subcategories

How do you build one?

  1. In Visualizations pick Matrix.
  2. Drag the row groups to Rows (for example Product[Category], then Product[Subcategory]), the column groups to Columns (for example Date[Quarter]) and measures to Values.
  3. Use the drill icons on the visual, or right-click a row header, Expand or Collapse, to open levels. The Auto-expand option in the row and column headers settings opens all levels by default.
  4. In Format visual, Layout and style presets sets the Stepped layout (child levels indented under the parent in one column) or not (one column per level), and Row subtotals and Column subtotals switch subtotals per level and set their position (top or bottom).
  5. Several measures in Values? Values, Options, Switch values to rows puts the measures down the side instead of across.

Row headers are frozen by default, so they stay in view when scrolling sideways; Layout, Freeze, Row headers changes that, and viewers can freeze or unfreeze for their session with a right-click. Column widths can Fit to content, Grow to fit or use a Fixed width, with custom widths per column.

How do you keep subtotals right?

Subtotals, like totals, evaluate the measure for all the rows of the group together. For sums that is the same as adding up. For a measure that should be added up per row (commission per customer, the higher of two values per month), write it with an iterator at the level it is defined:

Best Month Revenue =
MAXX ( VALUES ( 'Date'[Month] ), [Revenue] )

Commission =
SUMX ( VALUES ( Customer[Customer] ), IF ( [Revenue] > 100000, [Revenue] * 0.05, 0 ) )

To show something different on the subtotal rows (for example, blank for a ratio that makes no sense when summed), test the level with ISINSCOPE:

Share in Category =
IF (
    ISINSCOPE ( 'Product'[Subcategory] ),
    DIVIDE ( [Revenue], CALCULATE ( [Revenue], REMOVEFILTERS ( 'Product'[Subcategory] ) ) )
)

Subcategory rows show their share of the category (Mountain is 47% of Bikes); category and total rows show nothing. ISINSCOPE in DAX goes through the cases.

The no-DAX way is a custom total: right-click a numeric column, Customize total calculation, and pick Sum, Average, Min or Max of the displayed rows. Power BI renames the original column with a _Base suffix, hides it, adds a visual calculation with the original name, and marks the total cell. Reset to default undoes it. Custom totals work on tables and matrices, for numeric columns only, and not in Explore.

When is a matrix better than a chart?

When people need the numbers, in a grid they already know from Excel: budgets by department and month, sales by region and product line, a P&L. With conditional formatting (background colours as a heatmap, data bars, icons), the matrix also shows the pattern. A Background color gradient on the values makes the strongest cells the darkest, so in a region-by-month matrix a region that jumps in March lights up without anyone reading a number.

What goes wrong?

  • Subtotals that do not add up. See above: iterate at the right level, blank meaningless subtotals with ISINSCOPE, or use custom totals.
  • Too many levels open. Four levels expanded across thousands of rows are slow and unreadable. Keep the default collapsed and let people drill.
  • Months in alphabetical order. Sort Month Name by Month Number in the model.
  • Measures as columns that wrap. With five measures and twelve months, 60 columns do not fit; switch values to rows or cut measures.
  • Blank rows. Rows with no value in any measure are hidden by default; Show items with no data on the field brings them back when they matter.

Where can you check this?

Where do you start?

Build the matrix with one row field and one column field first, add the second row level, then check every subtotal. The table visual covers totals and formatting, and visual calculations explain what custom totals are built on.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.