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
nathov
Regular Visitor

Percentage of Subtotal (Hierarchy)

Hi guys,

 

I'm trying to creat a DAX formula in Power BI to work similar to % of Parent Row Total in Excel, but it isn't working as I expected.

Dax formula:

Revenue % =
VAR CurrentRetailerType = VALUES(Sales[Retailer type])
VAR CurrentProductLine = VALUES(Sales[Product line])
VAR RevenueForCurrentSelection = SUM(Sales[Revenue])
VAR TotalRevenue =
SWITCH(
TRUE();
ISINSCOPE(Sales[Retailer country]);CALCULATE(SUM(Sales[Revenue]);ALLSELECTED(Sales);Sales[Retailer type] IN CurrentRetailerType;Sales[Product line] IN CurrentProductLine);
ISINSCOPE(Sales[Product line]);CALCULATE(SUM(Sales[Revenue]);ALLSELECTED(Sales);Sales[Retailer type] IN CurrentRetailerType);
ISINSCOPE(Sales[Retailer type]);CALCULATE(SUM(Sales[Revenue]);ALLSELECTED(Sales))
)
RETURN
IF(
ISINSCOPE(Sales[Retailer type]);
DIVIDE(RevenueForCurrentSelection;TotalRevenue);
1
)
 
I uploaded the excel file to show what I need and the PBIX file on this link:

https://we.tl/t-Ar641SRfN9 

 

Can anyone help me, please?

7 REPLIES 7
amitchandak
Super User
Super User

@nathov , refer if this can help

https://community.powerbi.com/t5/Desktop/Percentage-of-subtotal/td-p/95390

https://www.kasperonbi.com/use-isinscope-to-get-the-right-hierarchy-level-in-dax/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi,

 

Based on this article, it should be:

RatioToParent = 
IF (
    ISFILTERED (Sales[Retailer country]);
    SUM (Sales[Revenue])
    /
    
    CALCULATE (
        SUM (Sales[Revenue]);
        ALL (Sales[Retailer country])
  
    );
    IF (
        ISFILTERED (Sales[Product line]);
        SUM (Sales[Revenue])
        /
        CALCULATE (
            SUM (Sales[Revenue]);
            ALL (Sales[Product line])
        );
        IF (
            ISFILTERED (Sales[Retailer type]);
            SUM (Sales[Revenue])
            /
            CALCULATE (
                SUM (Sales[Revenue]);
                ALL (Sales[Retailer type])
            )
        )
    )
)

The numbers appear to be right. 

2020-06-04_09-23-56.gif

 

Link to Power BI file

 

Please mark as solution if this is what you are looking for. 

Hi @stevedep,

 

Thanks for trying but this formula doesn't work if I have some filters applied on the visual, that's the problem that I'm trying to figure out.

 

 

Ah I see, to @Anonymous point, a star model is really required, no easy way around it. When you do this it will work:

star.png

Code:

RatioToParent = 
IF (
    ISFILTERED (country[Retailer country]);
  [countrycal];
    IF (
        ISFILTERED (line[Product line]);
      [productlinecalc];
        IF (
            ISFILTERED ('type'[Retailer type]);
          [typecalc]
    )
)
countrycal = CALCULATE( SUM (Sales[Revenue]))
    /
    
    CALCULATE (
       SUM (Sales[Revenue]);
        ALLSELECTED(country[Retailer country])
  
    )

The results are as expected:

results.png

 

Which you can see in the Power BI file, here

Please mark as solution if it works for you. 

Kudo's for the effort are appreciated.

 

Kind regards, 

Steve. 

Anonymous
Not applicable

There are a couple of things to note here.

First, this model is not following Best Practices. Please create a proper star schema with a hidden fact table and exposed dimensions. One of the dimensions should be Product (and most likely a date/time dimension since this is needed in each and every decent real-life model). Storing everything in one big table is not incorrect but it's making coding in DAX awkward and inefficient. Many a time also much harder.

Secondly, SWITCH must use ISINSCOPE from the most specific level to the most general. You are doing it the other way round.

Without a good model, you'll be facing issues constantly that should not be there in the first place. This will lead to complex and hardly maintainable DAX. Do not go this route if you want to stay sane. Please learn the rules of correct data modelling (star/snowflake schema).

Best
D

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.