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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
mobul
Frequent Visitor

Adding Custom formula to Matrix visual

Hi there,

 

I am trying to replicate an existing report visual in PowerBI and I'm not sure how to accomplish it. The matric visual appears to be my best bet but I'm struggling to understand how to

  1. calculate the feature using the correct formula in PowerBI Desktop (May 2024 edition).
  2. include the feature in the visual

I'm not sure how to explain it so a sample might be the best. The input is very basic and you could presume its originally sourced from a table of transactions which is then aggregated by item sold,repaired, recycled over year end and regions. 

 

In the desired output the % New Market is the value New Market as a percentage of the total across the region. 

 

The matrix visual allows me to add a row total but I'd like to instead include the calculation.  I have created measures for the items sold, repaired, recycled and included them on the values section of the matrix visual. Year and Region are dimensions on the rows section. 

 

How do I calculate the formula (measure, calc column, something else) and where do I add it to the visual?

Sample Input

Year EndRegionItems SoldItems RepairedItems Recycled
2020New Market418352412
2020Established606368141
2020Home641407388
2021New Market146395598
2021Established133359148
2021Home414139507
2022New Market214498270
2022Established316379444
2022Home140648347
2023New Market222136629
2023Established483132373
2023Home168395467
2024New Market347302190
2024Established528433302
2024Home536497224

 

Desired Output

Year EndRegionItems SoldItems RepairedItems Recycled
2020New Market418352412
2020Established606368141
2020Home641407388
 % New Market25%31%44%
2021New Market146395598
2021Established133359148
2021Home414139507
 % New Market21%44%48%
2022New Market214498270
2022Established316379444
2022Home140648347
 % New Market32%33%25%
2023New Market222136629
2023Established483132373
2023Home168395467
 % New Market25%21%43%
2024New Market347302190
2024Established528433302
2024Home536497224
 % New Market25%25%27%
2 ACCEPTED SOLUTIONS
MarkLaf
Super User
Super User

I was able to get something close to your dsired output:

 

MarkLaf_2-1744248237073.png

This is using a matrix visual, three measures for your columns, and an additional measure to capture reusable dynamic formatting string:

 

Items Recycled_Subtotal% = 
VAR _sum = SUM( 'Table'[Items Recycled] )
VAR _sumNewMarketOnly = 
    CALCULATE( 
        SUM( 'Table'[Items Recycled] ), 
        TREATAS( { "New Market" }, 'Table'[Region] ) 
    )
RETURN
IF( 
    ISFILTERED( 'Table'[Region] ), 
    _sum, 
    DIVIDE( 
        _sumNewMarketOnly, 
        _sum 
    ) 
)

 

Items Repaired_Subtotal% = 
VAR _sum = SUM( 'Table'[Items Repaired] )
VAR _sumNewMarketOnly = 
    CALCULATE( 
        SUM( 'Table'[Items Repaired] ), 
        TREATAS( { "New Market" }, 'Table'[Region] ) 
    )
RETURN
IF( 
    ISFILTERED( 'Table'[Region] ), 
    _sum, 
    DIVIDE( 
        _sumNewMarketOnly, 
        _sum 
    ) 
)

 

Items Sold_Subtotal% = 
VAR _sum = SUM( 'Table'[Items Sold] )
VAR _sumNewMarketOnly = 
    CALCULATE( 
        SUM( 'Table'[Items Sold] ), 
        TREATAS( { "New Market" }, 'Table'[Region] ) 
    )
RETURN
IF( 
    ISFILTERED( 'Table'[Region] ), 
    _sum, 
    DIVIDE( 
        _sumNewMarketOnly, 
        _sum 
    ) 
)

 

Dynamic Format = IF( ISFILTERED( 'Table'[Region] ), "0", "0%" )

 

For each of the measures (not including [Dynamic Format]), set the format to Dynamic, and put [Dynamic Format] in the Format bar:

MarkLaf_3-1744249086037.gif

 

Here are the steps to update the matrix visual's formatting to match what I have at the top.

 

1) Add in Table[Year End] and Table[Region] to Rows and the three measures (Items Recycled_Subtotal%, Items Repaired_Subtotal%, Items Sold_Subtotal%)

MarkLaf_4-1744249202265.png

 

2) Rename the measures so they match the columns they are based on

MarkLaf_5-1744249316778.png

 

3) Change in Format visual > Visual > Layout and style presets:

  • Layout: Tabular
  • Repeat row headers: On

MarkLaf_6-1744249699614.png

 

4) Change in Format visual > Visual > Blank rows:

  • Blank rows: On
  • Blank rows > Border: On

MarkLaf_7-1744249926055.png

 

5) Change in Format visual > Visual > Row headers:

  • +/- icons: Off

MarkLaf_8-1744250170820.png

 

6) Change Format visual > Visual > Row subtotals > Per row level: On

6a) For Row level: Region

  • Subtotal label: % New Market

MarkLaf_9-1744250771418.png

6b) For Row level: Year End

  • Show subtotal: Off
    Note: this removes the "extra" Total row at very bottom

MarkLaf_10-1744250828937.png

 

View solution in original post

I may not be understanding your question, let me know if the below answer isn't responsive.

 

To clarify, as noted in my #1 step, you need to create all three measures I define and add them to the matrix visual: [Items Recycled_Subtotal%], [Items Repaired_Subtotal%], and [Items Sold_Subtotal%]. I had sort of lazily put all the measures in one code block, so this point may have been a bit muddled. I just edited my response to clearly deliniate the three calculation measures + measure for dynamic formatting.

 

The three measures and the dynamic format all toggle on the same thing, namely whether we are in a filter context where 'Table'[Region] is getting filtered (in retrospect, probably ISINSCOPE is the better test in case you have outside filters on Region). There are three levels in the matrix we defined: All (grand total, which we hide) > Year End > Region.

 

To show more specifically, here is the breakdown of 'is or is not filtered' context in the visual:

 

MarkLaf_0-1744310917440.png

 

If you want to get a better sense of how the measures are working, here are the components split out on Items Sold (they all follow same pattern, so just looking at one should be sufficient).

 

Sum Regular = SUM( 'Table'[Items Sold] )
Sum New Market Only = 
CALCULATE( 
    SUM( 'Table'[Items Sold] ), 
    TREATAS( { "New Market" }, 'Table'[Region] ) 
)

MarkLaf_3-1744314108828.png

As we can see, the regular SUM is both:

  • The number to display, at the Region level
  • The denominator we want, at the Year End level

And, the New Market sum always just gives us the SUM for Region = 'New Market' for the given Year End. We only need to use it at the Year End level as our numerator.

 

On your last question re: how is % New Market working, this is just some formatting trickery - the relevant steps are:

  • (Step #3) Changing 'Layout and style presets > Layout' to 'Tabular'
    Note: besides changing the layout, this automatically moves the row subtotal from the top to the bottom, which you could do without going into Tabular layout via 'Row subtotals > Rows' formatting:
    MarkLaf_1-1744311929221.png
    Another note: with subtotal at bottom, the Grand Total goes away. We instead have the repeating Region subtotal at bottom of each Region grouping, and then the single Year End subtotal at bottom.
  • (Step #6a) As mentioned, the matrix actually adds in new rows with the 'Total' row heading. The word 'Total' is customizable through formatting options, which is what we take advantage of to introduce '% New Market' into the visual. '% New Market' is just the new bottom 'Total' row header relabeled.
    MarkLaf_2-1744313632480.png

    Note: in my instructions, we are actually turning on the 'Per row level' toggle to change these settings per level, mainly to turn off subtotals at Year End level and change the subtotal label at Region level. The general concept applies the same, though.

View solution in original post

7 REPLIES 7
MarkLaf
Super User
Super User

I was able to get something close to your dsired output:

 

MarkLaf_2-1744248237073.png

This is using a matrix visual, three measures for your columns, and an additional measure to capture reusable dynamic formatting string:

 

Items Recycled_Subtotal% = 
VAR _sum = SUM( 'Table'[Items Recycled] )
VAR _sumNewMarketOnly = 
    CALCULATE( 
        SUM( 'Table'[Items Recycled] ), 
        TREATAS( { "New Market" }, 'Table'[Region] ) 
    )
RETURN
IF( 
    ISFILTERED( 'Table'[Region] ), 
    _sum, 
    DIVIDE( 
        _sumNewMarketOnly, 
        _sum 
    ) 
)

 

Items Repaired_Subtotal% = 
VAR _sum = SUM( 'Table'[Items Repaired] )
VAR _sumNewMarketOnly = 
    CALCULATE( 
        SUM( 'Table'[Items Repaired] ), 
        TREATAS( { "New Market" }, 'Table'[Region] ) 
    )
RETURN
IF( 
    ISFILTERED( 'Table'[Region] ), 
    _sum, 
    DIVIDE( 
        _sumNewMarketOnly, 
        _sum 
    ) 
)

 

Items Sold_Subtotal% = 
VAR _sum = SUM( 'Table'[Items Sold] )
VAR _sumNewMarketOnly = 
    CALCULATE( 
        SUM( 'Table'[Items Sold] ), 
        TREATAS( { "New Market" }, 'Table'[Region] ) 
    )
RETURN
IF( 
    ISFILTERED( 'Table'[Region] ), 
    _sum, 
    DIVIDE( 
        _sumNewMarketOnly, 
        _sum 
    ) 
)

 

Dynamic Format = IF( ISFILTERED( 'Table'[Region] ), "0", "0%" )

 

For each of the measures (not including [Dynamic Format]), set the format to Dynamic, and put [Dynamic Format] in the Format bar:

MarkLaf_3-1744249086037.gif

 

Here are the steps to update the matrix visual's formatting to match what I have at the top.

 

1) Add in Table[Year End] and Table[Region] to Rows and the three measures (Items Recycled_Subtotal%, Items Repaired_Subtotal%, Items Sold_Subtotal%)

MarkLaf_4-1744249202265.png

 

2) Rename the measures so they match the columns they are based on

MarkLaf_5-1744249316778.png

 

3) Change in Format visual > Visual > Layout and style presets:

  • Layout: Tabular
  • Repeat row headers: On

MarkLaf_6-1744249699614.png

 

4) Change in Format visual > Visual > Blank rows:

  • Blank rows: On
  • Blank rows > Border: On

MarkLaf_7-1744249926055.png

 

5) Change in Format visual > Visual > Row headers:

  • +/- icons: Off

MarkLaf_8-1744250170820.png

 

6) Change Format visual > Visual > Row subtotals > Per row level: On

6a) For Row level: Region

  • Subtotal label: % New Market

MarkLaf_9-1744250771418.png

6b) For Row level: Year End

  • Show subtotal: Off
    Note: this removes the "extra" Total row at very bottom

MarkLaf_10-1744250828937.png

 

mobul
Frequent Visitor

Hi @MarkLaf 

Thanks for your detailed and very helpful response.


I hope I understand your solution correctly. You use the dynamic format to toggle whether a measures output is displayed as a precentage of total or the sum of the particular item. 

 

I presume the measure's DAX is repeated for ease of writing and its not important that the "other measures" percentages are calculated because they won't get toggled ?

 

What I'm not clear on is how the % New Market measure is activated/toggled as a percentage or how it materialises on the table?Wouldn't you need to include the measure %New Market twice in the values list or is the tabular layout an important component of the solution? 

I may not be understanding your question, let me know if the below answer isn't responsive.

 

To clarify, as noted in my #1 step, you need to create all three measures I define and add them to the matrix visual: [Items Recycled_Subtotal%], [Items Repaired_Subtotal%], and [Items Sold_Subtotal%]. I had sort of lazily put all the measures in one code block, so this point may have been a bit muddled. I just edited my response to clearly deliniate the three calculation measures + measure for dynamic formatting.

 

The three measures and the dynamic format all toggle on the same thing, namely whether we are in a filter context where 'Table'[Region] is getting filtered (in retrospect, probably ISINSCOPE is the better test in case you have outside filters on Region). There are three levels in the matrix we defined: All (grand total, which we hide) > Year End > Region.

 

To show more specifically, here is the breakdown of 'is or is not filtered' context in the visual:

 

MarkLaf_0-1744310917440.png

 

If you want to get a better sense of how the measures are working, here are the components split out on Items Sold (they all follow same pattern, so just looking at one should be sufficient).

 

Sum Regular = SUM( 'Table'[Items Sold] )
Sum New Market Only = 
CALCULATE( 
    SUM( 'Table'[Items Sold] ), 
    TREATAS( { "New Market" }, 'Table'[Region] ) 
)

MarkLaf_3-1744314108828.png

As we can see, the regular SUM is both:

  • The number to display, at the Region level
  • The denominator we want, at the Year End level

And, the New Market sum always just gives us the SUM for Region = 'New Market' for the given Year End. We only need to use it at the Year End level as our numerator.

 

On your last question re: how is % New Market working, this is just some formatting trickery - the relevant steps are:

  • (Step #3) Changing 'Layout and style presets > Layout' to 'Tabular'
    Note: besides changing the layout, this automatically moves the row subtotal from the top to the bottom, which you could do without going into Tabular layout via 'Row subtotals > Rows' formatting:
    MarkLaf_1-1744311929221.png
    Another note: with subtotal at bottom, the Grand Total goes away. We instead have the repeating Region subtotal at bottom of each Region grouping, and then the single Year End subtotal at bottom.
  • (Step #6a) As mentioned, the matrix actually adds in new rows with the 'Total' row heading. The word 'Total' is customizable through formatting options, which is what we take advantage of to introduce '% New Market' into the visual. '% New Market' is just the new bottom 'Total' row header relabeled.
    MarkLaf_2-1744313632480.png

    Note: in my instructions, we are actually turning on the 'Per row level' toggle to change these settings per level, mainly to turn off subtotals at Year End level and change the subtotal label at Region level. The general concept applies the same, though.

Hi @mobul,

 

Thank you for reaching out to Microsoft Fabric Community.

 

Thank you @MarkLaf and @Irwan for the prompt response.

 

As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided by the super user resolved your issue.
If our super user response resolved your issue, please mark it as "Accept as solution" and click "Yes" if you found it helpful.

 

Thanks and regards,

Anjan Kumar Chippa

Hi @mobul,

 

We wanted to kindly follow up to check if the solution provided by the super user resolved your issue.

If our super user response resolved your issue, please mark it as "Accept as solution" and click "Yes" if you found it helpful.

 

Thanks and regards,

Anjan Kumar Chippa

Hi @mobul,

 

As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided by the super user resolved your issue.
If our super user response resolved your issue, please mark it as "Accept as solution" and click "Yes" if you found it helpful.

 

Thanks and regards,

Anjan Kumar Chippa

Irwan
Super User
Super User

hello @mobul 

 

please check if this accomodate your need.

Irwan_0-1744239488399.png

1. unpivot your table (since you want to measure % New Market for all Recycled, Repaired, and Sold)

Irwan_1-1744239547971.png

2. since you want to insert measure in row matrix, you need create 4 measures with following DAX

New Market =
CALCULATE(
    MAX('Table'[Value]),
  'Table'[Region]="New Market"
)
Home =
CALCULATE(
    MAX('Table'[Value]),
    'Table'[Region]="Home"
)
Established = 
CALCULATE(
    MAX('Table'[Value]),
    'Table'[Region]="Established"
)
% New Market = 
var _New =
    CALCULATE(
        MAX('Table'[Value]),
        'Table'[Region]="New Market"
    )
var _Total =
    SUM('Table'[Value])
Return
DIVIDE(
    _New,
    _Total
)
3. in matrix visual format option, go to value then check "switch values to rows"
Irwan_2-1744239742836.png

4. assign those 4 measures as Matrix Values, Year as Matrix Row, and Attribute as Matrix Column (attribute is consisted of Recyled, Repaired, and Sold).

Irwan_3-1744239836760.png

 

Hope this will help.

Thank you.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.