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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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

@PaulDBrown 

You are my power bi hero,
thanks i have resolve my issue .....

@DataBank 

Have you sorted the HybridColumns field by the index column (as per my last screenshot above)?

Hyb.gif

 

@PaulDBrown thanks u r really humle  person thanks for support

@DataBank 

Thank you for your kind words! happy to have helped!

hi Paul,

Pls can you share the dataset used?

 

Thanks!

@s-white 

 

Here is the link to the zip file containing the Excel source file and the PBIX file 

 

You will however need to edit the Source data in Power Query since it now points to the file on my local drive.

Let me know if you have problems accessing the file.

Thank you!! Accessed it no problem. Time to play around with the dax codes.

 

Great tutorial!! 

Got one question, how are you tweaking if I wanna have two "dynamic-hybrid" measures, in my case Monthly Actuals same as you did for months and months. So If I choose Mar-22 then on the right I wanna have Actuals Sum of that month, if I select Mar-22 and Apr-22 then Sum of those two months...and so on until 12 months, and at the very end Actuals_Sum for all months.... that means it has to be dynamic. I am struggling with this solution of yours but no success....

Screenshot 2022-09-06 120033.png

@M1rzA_V 

I take it you need two more columns on the right:

1) the sum of the selected months

2) The sum of all months:

When you say you want the sum of all months do you mean for all months & years?

 

@PaulDBrown  Hi Paul! I received your notification per Email but I cant see your comment here... donno why, tried to log off/on but still I am seeing only my comment...

I saw what you asked me;

I take it you need two more columns on the right:

1) the sum of the selected months

2) The sum of all months:

When you say you want the sum of all months do you mean for all months & years?

Yes! I need the sum of selected months, and if no selection then sum of all months. But it has to separated from YTDs, what I managed from your tutorial...but I am not getting the rest...

@M1rzA_V 

ok. You need to add a new row to the "Hybrid Totals" table in Power Query to reflect the sum of the months (I've called it "Total Actuals"). Give it a very high index number - it has to be higher then the max index for any period in the model)

totals table.png

 

Once loaded, sort by the index columnOnce loaded, sort by the index column

 Assuming your measure for the sum is [Sum of Actuals], change the [Values for Matrix] measure to include this new total and to establish the behaviour based on the period slicer selection (ISFILTERED):

Values for Matrix =
VAR calc =
    IF (
        SELECTEDVALUE ( 'Hybrid Table'[Index] ) = 1,
        [Sum of Actuals],
        IF (
            SELECTEDVALUE ( 'Hybrid Table'[Index] ) = 2,
            [Sum of Target],
            IF (
                SELECTEDVALUE ( 'Hybrid Table'[Index] ) = 3,
                FORMAT ( [% diff over Target], "#0.0%" ),
                IF (
                    SELECTEDVALUE ( 'Hybrid Table'[Index] ) = 1000,
                    [Sum of Actuals],
                    //This is the new line to get the total by month or all months
                    IF (
                        ISFILTERED ( 'Calendar Table'[Period] ),
                        FORMAT ( [% diff by period], "#0.0%" )
                    )
                )
            )
        )
    )
RETURN
    IF (
        AND ( ISBLANK ( [Sum of Actuals] ), ISBLANK ( [Sum of Target] ) ),
        BLANK (),
        calc
    )
// The AND expression is to exclude unnnecessary blank values in the final matrix visual

line of code.pngChange the [FILTER Matrix Columns measure] to include the new total column:

FILTER Matrix columns =
VAR dim =
    VALUES ( 'Calendar Table'[Period] ) // Lists the filtered values for the Period
VAR clod =
    CALCULATETABLE (
        VALUES ( 'Hybrid Table'[HybridColumns] ),
        'Hybrid Table'[HybridColumns]
            IN { "Sum of Actuals", "Sum of Target", "% diff. over Target", "Total Actuals" }
    ) // lists the specified values from the HybridColumns
VAR REF =
    UNION ( dim, clod ) // Appends the VAR above to produce a virtual table
VAR matrix =
    VALUES ( 'Hybrid Table'[HybridColumns] ) // lists the HybridColumns Values
RETURN
    COUNTROWS ( INTERSECT ( REF, matrix ) )
// returns a value of "1" for the list of values which are coincidental for both virtual tables calculated in the VARs

filter.pngand you get:

Test.gifHere is the link to the file:

https://1drv.ms/u/s!AhGKDZz6e4HehI1W0Z_XD9vdekiwaA?e=U9zFZ5 

 

@PaulDBrown  Amazing man! Exactly what I wanted!!!! I was around with my trials but this was actually pretty simple... As I see I needed to add highest index to it....

Only one thing I removed, this line 

 ISFILTERED ( 'Calendar Table'[Period] ),

 

,because I havent been maybe very clear, but if there is no filter (month-year in my case) show me all the  months for the given fiscal year...

This is one of the best tricks that I have seen in last year in Power BI 😃 Thanks a lot!
Screenshot 2022-09-07 095550.png

When I try to write the following measure and check it for errors I get 

“The measure formula must start with an equal sign”.  What am I doing wrong?

 

Values for Matrix =

VAR calc =

IF (SELECTEDVALUE ('fHybridTable'[Index]) = 1,

[DeployQualCount],

IF (

SELECTEDVALUE ('fHybridTable'[Index]) = 2,

[HDQualCount],

IF (

SELECTEDVALUE ('fHybridTable'[Index]) = 3,

[SailorQualCOunt],

IF (

ISFILTERED ('dPmonths'[P-Month]),

)

)

)

)

RETURN

IF (

AND ( ISBLANK ([DeployQualCount]), ISBLANK ( [HDQualCount])),

BLANK (),

calc

)

@bkanderso 
I don't see an issues with the formular missing an equal sign as the error suggested, but you're missing some arguments in the last IF statement. I have simplified the formula using a switch function.

 

Values for Matrix = 

VAR calc =
SWITCH(SELECTEDVALUE ('fHybridTable'[Index]),
                        1,[DeployQualCount],
                        2,[HDQualCount],
                        3,[SailorQualCOunt],

IF (
ISFILTERED ('dPmonths'[P-Month]), //missing arguments in last if-statement
))

RETURN

IF (
AND ( ISBLANK ([DeployQualCount]), ISBLANK ( [HDQualCount])),
BLANK (),
calc
)

 

@skanord 

I would suggest you include @bkanderso in your response (I'm not sure he/she will receive the message otherwise..this is a blog environment as opposed to the regular forum)