Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now
It seems that the request for help in creating a “custom” or “hybrid” matrix is fairly frequent in the PowerBI forums (I have come across three different threads asking for assistance regarding this topic over the last week alone).
While the default table and matrix visuals do not cater for these types of custom layouts of information, developing a solution is not actually that complicated, though it does require some work.
As is normally the case with PowerBI, there are probably a number of ways to achieve this, but I thought I would showcase a method I find relatively “painless”.
The challenge: How can we create a “custom” layout for the information in a matrix?
In the following example we wish to include a combination of columns of aggregated values as well as values filtered by a dimension itself.
Let me try to explain what I mean by the question above. Let’s say we have a simple model for sales by Channel and Item and we would like to see the performance of the sales against a target over selected periods. The default matrix visual will allow us to visualise this, albeit the layout of the information is restricted to the default structure inherent to the visual. For example we can show the information:
1) Having the dimensions and period itself as rows, and the [Sum of Actuals], Sum of Target] and [% over target] as values:
2) Having the Dimensions as rows, the periods as columns, and the [Sum of Actuals], [Sum of Target] and [% over target] as values:
However there is no option currently to “turn off” certain elements in the headers (or rearrange the actual structure to a specific custom layout). If we want to see the periods as columns, each value (in this case three different measures) is shown for each period.
What if we wanted to “customise” the structure of the visual to show something like this? We can actually create a custom column layout to show the information we need. We just need to define it.
First the model for the example: Two fact tables (Actuals1 & Target2) and three dimension tables (Calendar Table, Dim Item and Dim Channel)
The key to this solution is to create a “custom” column layout to use in the column bucket in the matrix. In this example, the column layout we are seeking is this:
and the way to replicate this structure is by creating a corresponding table where each column header is a row of a table. This should not be hard to do in Power Query/Dax. For the purpose of this exercise
Since the periods in this table are referenced to the calendar table, the rows will “grow” accordingly.
Something worth mentioning at this point is that the index column is important for two reasons:
This table is unrelated to/disconnected from any other table in the model.
Just beware that if we are tempted to check the matrix out with the new column headers we have created the visual will “refuse to render” and complain about the lack of relationships between tables… just ignore this for now.
Since we are using as columns for our matrix an unrelated table, there are two main issues to be aware of regarding the filter context (with regards to the columns in the matrix) of the measures we are going to use. For this example:
We can use the simple aggregation measures for these values (simple SUM and DIVIDE functions). We will specify the filter context for these in the final measure.
2. In which measures we need to be specific as to the filter context and write the appropriate DAX to relate the filter to the corresponding context in the model. (the “Period Values”)
For these measures we need to be specific about the filter context we want in the calculation. Since we are using columns from the “Hybrid Table” (which has no relationships in the model), we need to use a function to establish a virtual “one-to-one/many” relationship (TREATAS function) to enforce the filter context and filter the values accordingly using the expression as the filter in the measure.
Basically what we are doing is saying: “when the matrix column = Jan – 2020, filter the rows to show those which are filtered by the Calendar Table [Period] = Jan – 2020”.
In this example, we want to show the % difference between the actuals vs the target for each period in the matrix:
Having established the measures we need, all we need now is the final measure we will use in the matrix to take into account the filter context for each of the “HybridColumns” we are using in the matrix columns. This measure will establish which of the previous measures must be used based on the filter context established by the columns included in the matrix from our Hybrid Table. Since the actual results vary from whole/decimal numbers, we also need to specify the FORMAT required for each result:
The filter context is established by the columns:
Which come from our “Hybrid Table” (we are using the index reference for the Hybrid table for simplicity purposes):
And this is the final measure which we will use for the values in the matrix:
We can now create the matrix using:
a) The Channel and Item dimensions as rows.
b) The HybridColumns from our Hybrid Table for the Column Bucket
c) The final measure [Values for Matrix] in the values bucket to get this:
d) (some conditional formatting for the values displayed)
Since we want to be able to filter the matrix by our periods of choice in a slicer, we need one more measure. The slicer on the page is from the ‘Calendar Table’ [Period] column, so we need to be able to select the periods to be shown in the matrix based on the selection made on this slicer from the calendar table. The measure must compare the values selected in the slicer with those established in the visual from the Hybrid Table:
We can then use this filter in the filter pane for the matrix visual to filter the columns based on the selection in the Calendar Table [Period] Slicer. We set the TopN for the HybridColumns field in the filter Pane as “1” to make the visual respond to the selection made in the ‘Calendar Table’ [Period] Slicer.
We thereby can now use the Calendar Slicer (Calendar Table [Period]) to filter the periods we want to see in the visual.
PS: If this tutorial is unclear, please feel free to contact me on this thread or via pm!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.