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.

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

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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