Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

PaulDBrown

Creating a “custom” or “hybrid” matrix in PowerBI

Backround

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:

Default rows.JPG

 

2) Having the Dimensions as rows, the periods as columns, and the [Sum of Actuals], [Sum of Target] and [% over target] as values:

default columns.JPG

 

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.

Hybrid.JPG

 

One way to create a “Custom” or “Hybrid” matrix:

First the model for the example: Two fact tables (Actuals1 & Target2) and three dimension tables (Calendar Table, Dim Item and Dim Channel)

the model.JPG

 

Step 1: Creating the custom layout for the columns of the matrix

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:

Hybrid Columns.JPG

 

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

  • Create a table with three rows for the aggregate values (and an index for sorting/filtering purposes):

Values header table.JPG

  • Create a period table using the Calendar Table [Period] column referenced to the Calendar table already present in the model (in this case, the index starts at “4” to cater for the previous table rows in the previous table created):

period table.JPG

 

  • …and by appending these two tables, we have created the structure we need for the custom layout (let’s call this table “Hybrid Table”):

Hybrid table.JPG

 

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:

  1. It allows for the correct sorting of the actual columns, even if the periods are filtered
  2. The index column is actually very useful in the FILTER expressions of the measures needed.

This table is unrelated to/disconnected from any other table in the model.

Full model.JPG

 

 

 

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.

visual error.JPG

 

Step 2: Creating the measures for the custom matrix

Let the DAX fun begin…!

Filter Context.JPG

 

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:

  1. Which measures can rely on the actual relationships established in the model (the “aggregated values)

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.

SumActuals.JPG

  

SumTarget.JPG

 

%DiffTarget.JPG

 

           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.

TREATAS expression.JPG

 

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:

TREATAS.JPG

 

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:
Hybrid Columns.JPG

 Which come from our “Hybrid Table” (we are using the index reference for the Hybrid table for simplicity purposes):

Hybrid table.JPG

 

And this is the final measure which we will use for the values in the matrix:

Final Measure.JPG

 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)

Final Custom Matrix.jpg

 

Step 3: Enabling the filtering of the columns in the custom matrix


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:

FILTER Matrix1.JPG

 

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.

TopN filter.JPG

 

We thereby can now use the Calendar Slicer (Calendar Table [Period]) to filter the periods we want to see in the visual.

 

Conclusion: The flexibility offered by PowerBI and DAX allows us to create “custom” matrices to reflect the layout of information we need to depict in our reports.

 

PS: If this tutorial is unclear, please feel free to contact me on this thread or via pm!

Comments

Thanks For your Reply @PaulDBrown

 

Hi,

Thank you so much for the important post!!

I got stuck at the first step, when we need to:

  • Create a period table using the Calendar Table [Period] column referenced to the Calendar table already present in the model (in this case, the index starts at “4” to cater for the previous table rows in the previous table created)

My Calendar table does not exist in the Pwer Query Editor. How can I reerence it?

 

Thank you 🙂

@BI_Speaker 

 

You can create the table with DAX using UNION to append the table with the aggregations and the calendar table

How would you go about sorting the hybrid columns in the matrix visual? I would like to see i.e. the largest or smallest value of a column. 

I used this guide to create a similar matrix and one of the columns shows "Low", "Medium", "High" based on a measure.

 

How can I do another visual with a table that counts the occurence of each of those values like:

LevelCount
Low3
Medium4
High2

@darry3ni 

Sorry, I'm not quite getting what you need. What is it that you are counting? Can you provide a sample PBI file?

@PaulDBrown  I have a metrics report where 1st column is Category & 2nd column is Sub-Category. In columns have date hierarchy like Year, Quarter & the Month. The expectation is when row is drilled up until Category then the total should disappear but only show when expanded sub-category. All the sub-totals for Year/Qtr/Month should appear only for Sub-category & not for Category. Is this possible ?

@sudhakarsha 

Sorry, I'm not too sure what you mean. Are you working with a regular matrix?

I don't think you can have a total appear or disappear when using the drill function. You can however keep values for sub-catgory using ISINSCOPE.

Thank you, Paul, this is brilliant. I get a lot of requests for a hybrid matrix, and I always struggle to explain why I can't do it... now I don't have to, yay!

@JenM 

Thanks, I appreciate your appreciation!

BTW, you can also create an inactive relationship between the tables and use the USERELATIONSHIP function instead of TREATAS

but filter wil not work if in measure exist filtered expression, then filter bypass filters in measures

@Digger 

Sorry, I'm not sure what you mean. Can you expand on the issue please?

hi,

@PaulDBrown im unable to sort column in hybrid power bi matrix ,

Please help me

DataBank_0-1655817720500.png

 

@DataBank 

 

You need to include an index column when creating the table. You then use this index column to sort the Hybrid Columns field in the table view once loaded, using the "Sort by column" option in the ribbon

index sort.png

2022-06-21.png

 

@PaulDBrown thanks for ur prompt reply 

here is my hybrid table, i also have index column but whenever i put hybridcolumns in matrix then sorting not working 

Hybrid Table

DataBank_0-1655826542094.png

 

Matrix output when with hybrid columns

DataBank_2-1655826804642.png


Matrix output when with index column

DataBank_1-1655826713611.png


@PaulDBrown please help me