Skip to content

Building a Matrix with Asymmetrical Columns and Rows

>> Download the sample (Power BI)

This article explains, as a stand-alone concept, how to build a visual with asymmetrical columns and rows in Power BI, using both standard DAX measures and the more flexible calculation groups.

This matrix in the image above is built using the Adventure Works database. Category (#1), Sub Category (#2) and Year (#3) are all columns coming from dimension tables in the model. The values section of this matrix (the numbers) come from the measure [Total Sales]. Power BI is designed to work this way. You use columns to slice and dice the data, and measures to do the calculations. The columns “filter” the data tables, then the measure is calculated on the data that remains after the filter is applied. The example above is a matrix, but it is exactly the same for pie charts, column charts, tree maps, etc.

Not every report you build will have layout requirements as simple as the one above. When using an Excel Pivot Table, there is a feature called “Fields, Items and Sets” that makes the process of creating asymmetric pivot tables relatively easy, but this doesn’t exist in Power BI. To illustrate, here is an example of an asymmetric visual (Power BI matrix in this case).

Sales

In the example above, the first 4 years come from the year column, just like before, but now there are 2 additional columns at the end of the matrix that are not simply the addition of the other columns. To create the last 2 columns above, measures need to be written, but you can’t currently show both measures and columns from a model like this in the same Power BI matrix. Rather, you need to build a solution that leverages a separate table to generate the columns needed, and then to generate the results you need in each column.

The sections below explain how to build an asymmetric matrix like this.

Below is a simplified version of the concept being demonstrated (a combination of columns and measures on a visual). If you needed a simple visual like the one above, it would of course be possible to write 6 measures and place them all in a single matrix. This will produce the visual above. But there are many use-cases where that won’t work. For example, if you wanted a slicer like the one shown below instead of an asymmetric table, you could not do that with measures alone.

Also, if you wanted a collapsible header/sub-header, as shown on the rows of the matrix below, where the sales by year are additive and the variance numbers are grouped together, you can’t do that with just measures.

This is a simplified version of the header table technique that you can use when you can’t just use measures, for whatever reason. If you can solve your problem by simply using measures, then of course you can do that without this technique.

Here is a pseudo code table that conceptually explains what needs to be done to solve this problem.

Note that the first 4 rows (2017 – 2020) are just standard Power BI behaviour. You can do this just by dragging the year column and the [Total Sales] measure. But if you want the change in sales and % change in sales too, then you need to use a different approach from the standard method.

There are currently 2 ways to do this:

  1. SWITCH Measures with Header Tables (connected or disconnected)
  2. Calculation Groups

Both approaches are covered in more detail below.

You can use a header table and a SWITCH measure to obtain the required results in a matrix.

Here are the steps:

  • Create and load a header table with the layout you want.
  • Create a relationship between the header table and the calendar table (if required).
  • Write a SWITCH measure to generate the result for each column item.
  • Use the SWITCH measure in your report.
  • Create the following header table in Excel and name it HeaderTable. It has the following columns:

    • Display Name for the column headers of the Matrix
    • Sort Order that will be used in the SWITCH measure and also to control the sort order of the display value.

  • Load the HeaderTable from the Excel workbook into Power BI.

Create a Relationship between the Header Table and Calendar Table

Section titled “Create a Relationship between the Header Table and Calendar Table”
  • Create a relationship between the HeaderTable and Calendar Table using the Year column from the Calendar table and Display Name from the HeaderTable.

Note that the first 4 values in the Display Name column will find a match in the Calendar table. But the last 2 rows in that column will not match any record in the Calendar table. This doesn’t matter, because the SWITCH measure will leverage this relationship when it is useful (the first 4 rows) and it will override the relationship when it is not useful – explained further below.

Technically, this model is now a Snowflake Schema and this is a good example of a time when it is OK to step away from the recommended Star Schema.

Write a SWITCH Measure to generate the result

Section titled “Write a SWITCH Measure to generate the result”
  • Next, write the SWITCH measure that determines what result to display based on the displayed value:
Values to Display =
VAR DisplayItem = SELECTEDVALUE ( HeaderTable[Sort Order] )
VAR Sales2020 = CALCULATE([Total Sales],all(HeaderTable),HeaderTable[Display Name]="2017")
VAR Sales2019 = CALCULATE([Total Sales],all(HeaderTable),HeaderTable[Display Name]="2020")
VAR Chg = Sales2020 - Sales2019
VAR ChgPct = DIVIDE(Chg,Sales2019)
RETURN
SWITCH (
TRUE (),
DisplayItem <= 4, [Total Sales],
DisplayItem = 5, Chg,
DisplayItem = 6, FORMAT(ChgPct,"#.0%")
)

In the measure the last display item (line 13) is formatted as a percentage using the FORMAT function. This is actually a text format and not a numeric format. As a consequence, you cannot use this technique in a chart of any kind, only in visuals that simply display the numbers (or text in this case).

  • Next, create the matrix. Place the column HeaderTable[Display Value] (#1 below) on Columns and the SWITCH measure [Values to Display] (#2 below) on Values. The Display Value column has been sorted by the sort order column.

There are some blank rows in the matrix for Lights, and Panniers. This is caused by the format string mentioned above. When there are no sales, the percentage change will return BLANK( ). When the result is blank, it is hidden in the visual. The FORMAT function has the effect of turning that blank into an empty string. Empty strings are not hidden automatically, so that is why the blank line now appears. This can be fixed with the following code modification.

Values to Display =
VAR DisplayItem = SELECTEDVALUE ( HeaderTable[Sort Order] )
VAR Sales2020 = CALCULATE([Total Sales],all(HeaderTable),HeaderTable[Display Name]="2020")
VAR Sales2019 = CALCULATE([Total Sales],all(HeaderTable),HeaderTable[Display Name]="2019")
VAR Chg = Sales2020 - Sales2019
VAR ChgPct = DIVIDE(Chg,Sales2019)
RETURN
SWITCH (
TRUE (),
DisplayItem <= 4, [Total Sales],
DisplayItem = 5, Chg,
DisplayItem = 6, IF( ISBLANK(ChgPct), BLANK(), FORMAT(ChgPct,"#.0%") )
)

Here, the single line of code inside the SWITCH essentially says “As long as it is one of the first 4 items, just give the normal result to [Total Sales]”. This effectively leverages the relationship between the header table and the calendar table for the first 4 items to do what it would do automatically without the header table.

Technically, it is not a requirement to use a relationship with a header table. This was done in this case because 4 out of the 6 results can be simply extracted from the base model automatically. A relationship did not have to be created; instead, the 6 results could have been hardcoded directly into the SWITCH measure.

There are 2 disadvantages of this SWITCH Measure/Header Table approach, shown above:

  • Any formatting variations from the base measure must be formatted as Text as shown, with the implications that it can’t be used in a chart.
  • One SWITCH measure has to be written for every base measure you want to do this with, e.g. [Total Sales], [Total Quantity], etc. (As an aside, you could also nest a second SWITCH measure and another disconnected header table to control the input base measure, but that is beyond the scope of this article.)

The second, newer way to solve this problem is to use calculation groups. The main benefits of using calculation groups are:

  • You can vary the number formatting depending on the column.
  • You can generalise it to work on any measure – not just [Total Sales].

The main downside is that, at the time of writing, you have to use Tabular Editor (a third-party external tool) to create the calculation group. This is, however, a good opportunity to learn how to use it.

Here are the steps to complete this task in Tabular Editor:

  • Go to Tabular Editor
  • Create a calculation group and name it, say, Sales Horizon
  • Create calculation items in the calculation group
  • Assign the ordinal values (sort order) for the calculation items
  • Assign the format for each calculation item
  • Save the model and switch back to Power BI Desktop
  • Use the calculation group in your report

Below, we go through each of these steps.

  • Go to Tabular Editor. You can find the tables in the data model in the left panel under the Tables folder. Right-click on the Tables folder to create a new calculation group. Name it Sales Horizon (#1 below).

Create Calculation Items in the Calculation Group

Section titled “Create Calculation Items in the Calculation Group”

This is where you will create the calculation items as shown in the table below:

Calculation Item Result Required
2017 Total Sales in 2017
2018 Total Sales in 2018
2019 Total Sales in 2019
2020 Total Sales in 2020
Chg 2020 vs 2019 Chg in Total Sales 2020 vs 2019
% Chg % Chg
  • To do this, first create the calculation items: right-click on the Sales Horizon “table” and select “new calculation item”. Call the first calculation item 2017 (#1 below) and write the DAX formula (#2 below).

There are different ways you could write the DAX formula for this calculation item. For example, you could hardcode [Total Sales] instead of SELECTEDMEASURE(), but the latter allows a lot more flexibility in the use of this calculation group (explained later).

  • Next, create the calculation items for 2018, 2019, and 2020 (#1 below) by simply copying 2017 and pasting it 3 times, then changing the name and the formula accordingly (#2 below).

  • Next, create the calculation item Chg 2020 vs 2019 as shown below. The reason you can see question marks next to the calculation items above is that the calculation group does not get validated by Power BI until it is saved. So click the Save button, and then click the DAX Formatter button to format the formulas.

  • The last calculation item required is the % Chg. The measure is shown below. The code from above can be reused.

Assign the Ordinal Values for the Calculation Items

Section titled “Assign the Ordinal Values for the Calculation Items”

The ordinal values of the calculated items define the sort order of the values in the visual. The ordinal numbers start from 0.

  • Change the ordinal parameter for 2017 to 0 as shown below (#1 below).

  • Next change the ordinal numbers for 2018, 2019, 2020, Chg 2020 vs 2019, and % Chg to 1, 2, 3, 4 and 5 respectively.

Assign the Format for Each Calculation Item

Section titled “Assign the Format for Each Calculation Item”
  • This is the last step in creating the calculation group and calculation items. It is possible to define the format of each calculation item separately as relevant. This is done in the property “Format String Expression” of the calculation item (#1 below). This has only been done for the last item in the list – the % Chg calculation. By not setting the format for the other items, the base measure formatting will be used instead.

  • Save the work in Tabular Editor and switch to Power BI Desktop. Power BI should prompt you to refresh the calculation group: click the Refresh Now button.

After creating a calculation group, you should find a new table called “Sales Horizon” in the data model as shown below:

As shown above, there is a column “Name” with the calculation items as values and the “Ordinal” column.

  • To build the visual below, remove ‘Calendar’[Year] from columns in the visual and add Sales Horizon[Name]. Then, add the measure [Total Quantity] to values in the visual. The correct number format should display for the quantity columns and the % column.

  • You can remove the [Total Quantity] measure and replace it with any other measure (such as [Total Sales], [Total Cost]) and those will work too. This is the benefit of using SELECTEDMEASURE() in the calculation group instead of hard-coding one specific measure.

Quantity

This article shows a simple example starting with 4 regular column values, followed by 2 calculations made from measures. In practice, the same techniques work for more complex layouts, including P&Ls.