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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
ZiyadSyauqi
Frequent Visitor

Calculate Custom Row Subtotal in Matrix Visual

Dear Power BI Community,

I need help creating a custom row subtotal in a Matrix Visual. Specifically, I want to calculate the vs_n_min_1_pct column by dividing tm_actual by lm_actual. Here’s a brief overview:

  • Current Result: The row subtotal directly sums all values within the subgroup (summing metrics values inside group_metrics).
  • Desired Result: For the row subtotal of Group2 in the column vs_n_min_1_pct, it should be 116.8%, which is calculated as 5,904,649 (tm_actual) divided by 5,055,722 (lm_actual) multiplied by 100%.

Data Sample

Here’s a simplified version of my data:

 

group_metricsmetricsAttributeValue
Group1A1lm_actual503,347
Group2A2lm_actual5,113,225
Group2B2lm_actual-387,922
Group2C2lm_actual270,220
Group2D2lm_actual19,299
Group2E2lm_actual40,900
Group1A1tm_actual446,129
Group2A2tm_actual5,023,533
Group2B2tm_actual589,795
Group2C2tm_actual267,103
Group2D2tm_actual13,253
Group2E2tm_actual10,965
Group1A1vs_n_min_1_pct88.60%
Group2A2vs_n_min_1_pct98.20%
Group2B2vs_n_min_1_pct-152.00%
Group2C2vs_n_min_1_pct98.80%
Group2D2vs_n_min_1_pct68.70%
Group2E2vs_n_min_1_pct26.80%


Current Results Screenshots:
This is what i put in my Matrix Visuals:

ZiyadSyauqi_0-1717487172462.png
and this is the results
ZiyadSyauqi_1-1717487186528.png

Expected Calculation

For Group2, the subtotal for vs_n_min_1_pct should be 116.8%, calculated as follows:

vs_n_min_1_pct_total = (5,904,649 / 5,055,722) × 100% = 116.8%

Issue

While this code works for individual metrics, the row subtotal directly sums up all values within the subgroup. Instead, I want to retain the calculation for tm_actual and lm_actual and specifically calculate the custom row subtotal for vs_n_min_1_pct.

Question

Is it possible to achieve this in Power BI? If so, how can I adjust my DAX measure or the matrix settings to calculate custom row subtotals as described?

Thanks!

 

2 ACCEPTED SOLUTIONS
v-huijiey-msft
Community Support
Community Support

Hi @ZiyadSyauqi ,

 

Thanks for the reply from @ridhoaryo24  / @amitchandak .

 

Please try:

 

Enter Power Query and pivot the Attribute column:

vhuijieymsft_0-1717572004687.png

 

This is the data after pivoting:

vhuijieymsft_1-1717572004688.png

 

Exit Power Query and create a measure:

Measure = 
VAR _tm = SUM('Table'[tm_actual])
VAR _lm = SUM('Table'[lm_actual])
RETURN
IF(
    HASONEVALUE('Table'[group_metrics]),
    DIVIDE(SUM('Table'[tm_actual]),SUM('Table'[lm_actual])),
    DIVIDE(_tm,_lm))

 

After creation, set the format:

vhuijieymsft_2-1717572035559.png

 

Drag the following fields to the Matrix visual object:

vhuijieymsft_3-1717572035559.png

 

The final page effect is as follows:

vhuijieymsft_4-1717572044469.png

 

If you have any other questions please feel free to contact me.

 

The pbix file is attached.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

View solution in original post

Thanks using some additional DAX code and combine with your solution i got this solution:

ValueNew = 
VAR lm_actual = CALCULATE(
    SUM(Table[Value]),
    Table[Attribute] = "lm_actual",
    ALLEXCEPT(Table, Table[group_metrics], Table[metrics])
)

VAR tm_actual = CALCULATE(
    SUM(Table[Value]),
    Table[Attribute] = "tm_actual",
    ALLEXCEPT(Table, Table[group_metrics], Table[metrics])
)

VAR lm_group = CALCULATE(
    SUM(Table[Value]),
    Table[Attribute] = "lm_actual",
    ALLEXCEPT(Table, Table[group_metrics])
)

VAR tm_group = CALCULATE(
    SUM(Table[Value]),
    Table[Attribute] = "tm_actual",
    ALLEXCEPT(Table, Table[group_metrics])
)

RETURN 
SWITCH(
    TRUE(),
    SELECTEDVALUE(Table[Attribute]) = "vs_n_min_1_pct" && HASONEVALUE(Table[metrics]),
            DIVIDE(tm_actual, lm_actual, 0),
    SELECTEDVALUE(Table[Attribute]) = "vs_n_min_1_pct" && NOT HASONEVALUE(Table[metrics]),
            DIVIDE(tm_group, lm_group, 0),
    SUM(Table[Value])
)

 

View solution in original post

5 REPLIES 5
v-huijiey-msft
Community Support
Community Support

Hi @ZiyadSyauqi ,

 

Thanks for the reply from @ridhoaryo24  / @amitchandak .

 

Please try:

 

Enter Power Query and pivot the Attribute column:

vhuijieymsft_0-1717572004687.png

 

This is the data after pivoting:

vhuijieymsft_1-1717572004688.png

 

Exit Power Query and create a measure:

Measure = 
VAR _tm = SUM('Table'[tm_actual])
VAR _lm = SUM('Table'[lm_actual])
RETURN
IF(
    HASONEVALUE('Table'[group_metrics]),
    DIVIDE(SUM('Table'[tm_actual]),SUM('Table'[lm_actual])),
    DIVIDE(_tm,_lm))

 

After creation, set the format:

vhuijieymsft_2-1717572035559.png

 

Drag the following fields to the Matrix visual object:

vhuijieymsft_3-1717572035559.png

 

The final page effect is as follows:

vhuijieymsft_4-1717572044469.png

 

If you have any other questions please feel free to contact me.

 

The pbix file is attached.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

Thanks @v-huijiey-msft i think your solution also works!

I already reply @ridhoaryo24 comment for another workaround without needed to do pivoting the table. My actual data need some additional "dynamic column header title" using DAX Columns as the column field so i think it is better not pivoting the data.

ridhoaryo24
New Member

Hi Ziyad,

You can make these measure to get the result you want

 

lm_actual = CALCULATE(SUM('Sheet1 (2)'[Value]), FILTER('Sheet1 (2)', 'Sheet1 (2)'[Attribute]="lm_actual"))

tm_actual = CALCULATE(SUM('Sheet1 (2)'[Value]), FILTER('Sheet1 (2)', 'Sheet1 (2)'[Attribute]="tm_actual"))

 

 

And then, you make this measure:

 

vs_n_min_1_pct =
VAR lm_group = CALCULATE(
[meas_lm_actual],
ALLEXCEPT('Sheet1 (2)', 'Sheet1 (2)'[group_metrics])
)
VAR tm_group = CALCULATE(
[meas_tm_actual],
ALLEXCEPT('Sheet1 (2)', 'Sheet1 (2)'[group_metrics])
)

 

Add those calculations to the Values accordingly.

There is no need to insert anything in the Columns.

 

May it help you.

 

Regards,

 

Ridho A.

Thanks using some additional DAX code and combine with your solution i got this solution:

ValueNew = 
VAR lm_actual = CALCULATE(
    SUM(Table[Value]),
    Table[Attribute] = "lm_actual",
    ALLEXCEPT(Table, Table[group_metrics], Table[metrics])
)

VAR tm_actual = CALCULATE(
    SUM(Table[Value]),
    Table[Attribute] = "tm_actual",
    ALLEXCEPT(Table, Table[group_metrics], Table[metrics])
)

VAR lm_group = CALCULATE(
    SUM(Table[Value]),
    Table[Attribute] = "lm_actual",
    ALLEXCEPT(Table, Table[group_metrics])
)

VAR tm_group = CALCULATE(
    SUM(Table[Value]),
    Table[Attribute] = "tm_actual",
    ALLEXCEPT(Table, Table[group_metrics])
)

RETURN 
SWITCH(
    TRUE(),
    SELECTEDVALUE(Table[Attribute]) = "vs_n_min_1_pct" && HASONEVALUE(Table[metrics]),
            DIVIDE(tm_actual, lm_actual, 0),
    SELECTEDVALUE(Table[Attribute]) = "vs_n_min_1_pct" && NOT HASONEVALUE(Table[metrics]),
            DIVIDE(tm_group, lm_group, 0),
    SUM(Table[Value])
)

 

amitchandak
Super User
Super User

@ZiyadSyauqi , You can refer the blog how to shift sub total

Power BI How to get the P&L formatting right: https://youtu.be/C9K8uVfthUU
Power BI How to get two columns format Profit and Loss Statement(P&L) right: https://youtu.be/WLg85yiMgHI
https://medium.com/microsoft-power-bi/power-bi-formatted-p-l-with-custom-sub-totals-and-blank-rows-e...

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.