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
ReneDH
Frequent Visitor

Show correct subtotals & totals for Measures

All,

 

I've searched the forum already and find some solutions on how people are trying to solve this, however I can't figure out how to get this working.

 

I have a mastertable which looks like this:

 

LocationProductgroupAttributeValue
A1Costs100
A1Volume10
A2Costs200
A2Volume10
B1Costs500
B1Volume10
B2Costs1000
B2Volume10

 

And for every location I need to calculate the costs that we are making with the volume.
This is not a difficult thing to do, however there is a exception that for location A i need to consider all costs and volumes, whilst for other location i need to only take product group 1.

Based on this example I created the following measure.

 

 

 

Costs per Volume = 
VAR LOCATION =
    SELECTEDVALUE (Data[Location] )
VAR COSTSVOLUME_all =
    DIVIDE ( [COSTS], ( [VOLUME] ), BLANK () ) 
VAR COSTSVOLUME_GROUP1 =
    DIVIDE (
        CALCULATE (
            [COSTS],
            Data[ProductGroup] = 1
        ),
        CALCULATE ( [VOLUME], Data[ProductGroup] = 1 ),
        BLANK ()
    ) 
RETURN
    SWITCH (
        TRUE (),
        LOCATION = "A", IF( COSTSVOLUME_all = 0, BLANK (), COSTSVOLUME_all),
               IF ( COSTSVOLUME_GROUP1 = 0, BLANK (), COSTSVOLUME_GROUP1 )
    )

 

 

 

 

However when I add the cluster in front of my table (so both locations have the same cluster) it will calculate the subtotal for the cluster based on the formula for COSTVOLUME_GROUP1 whilst it would need to take into account the exceptions for location A.

1 ACCEPTED SOLUTION

@ReneDH,

 

Try this measure:

 

Costs per Volume = 
VAR LOCATION =
    SELECTEDVALUE ( Data[Location] )
VAR COSTSVOLUME_all =
    DIVIDE ( [COSTS], [VOLUME] )
VAR COSTSVOLUME_GROUP1 =
    DIVIDE (
        CALCULATE ( [COSTS], Data[ProductGroup] = 1 ),
        CALCULATE ( [VOLUME], Data[ProductGroup] = 1 )
    )
VAR vResultSingle =
    SWITCH (
        TRUE (),
        LOCATION = "A", IF ( COSTSVOLUME_all = 0, BLANK (), COSTSVOLUME_all ),
        IF ( COSTSVOLUME_GROUP1 = 0, BLANK (), COSTSVOLUME_GROUP1 )
    )
VAR vNumeratorTotal =
    CALCULATE ( [COSTS], Data[Location] = "A" || Data[Productgroup] = 1 )
VAR vDenominatorTotal =
    CALCULATE ( [VOLUME], Data[Location] = "A" || Data[Productgroup] = 1 )
VAR vResultTotal =
    DIVIDE ( vNumeratorTotal, vDenominatorTotal )
VAR vResult =
    IF ( HASONEVALUE ( Data[Location] ), vResultSingle, vResultTotal )
RETURN
    vResult

 

DataInsights_1-1624310139408.png

 

 

DataInsights_0-1624310023931.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

7 REPLIES 7
DataInsights
Super User
Super User

@ReneDH,

 

Try this measure. The concept is that it creates a virtual table in the current filter context, calculates your measure for each row in the virtual table, and then iterates the virtual table using SUMX.

 

CPV Total = 
VAR vTable =
    ADDCOLUMNS (
        SUMMARIZE ( Data, Data[Location], Data[Productgroup] ),
        "@Amount", [Costs per Volume]
    )
VAR vResult =
    SUMX ( vTable, [@Amount] )
RETURN
    vResult

 

Notice the difference in totals between the two measures:

 

DataInsights_0-1624283391326.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thank you for the reply, it is not exactly what I meant.

The formula that I posted is correctly calculating the cost per volume, per location.
ReneDH_1-1624285604253.png
However my total should be taken into account the exception that I wrote in my first formula. So calculate  with 800 on costs for the total and 30 on volume would result in an cost per volume of 26.67 

ReneDH_0-1624285533002.png

Hope this clarifies what I try to achieve, but already many thanks for looking into this topic.

 




@ReneDH,

 

Is this the desired result (the first two rows have Costs per Volume of 26.67)?

 

DataInsights_0-1624297766519.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




The idea is as follows:

ReneDH_0-1624304724049.png

So the cost per volume is calculated per location.

Whilst it needs to be also calculated in total (including the rules as stated in the first formula).

It is quite difficult to explain without giving insight into the real data behind, but due to the data i cant disclose more information.

@ReneDH,

 

Try this measure:

 

Costs per Volume = 
VAR LOCATION =
    SELECTEDVALUE ( Data[Location] )
VAR COSTSVOLUME_all =
    DIVIDE ( [COSTS], [VOLUME] )
VAR COSTSVOLUME_GROUP1 =
    DIVIDE (
        CALCULATE ( [COSTS], Data[ProductGroup] = 1 ),
        CALCULATE ( [VOLUME], Data[ProductGroup] = 1 )
    )
VAR vResultSingle =
    SWITCH (
        TRUE (),
        LOCATION = "A", IF ( COSTSVOLUME_all = 0, BLANK (), COSTSVOLUME_all ),
        IF ( COSTSVOLUME_GROUP1 = 0, BLANK (), COSTSVOLUME_GROUP1 )
    )
VAR vNumeratorTotal =
    CALCULATE ( [COSTS], Data[Location] = "A" || Data[Productgroup] = 1 )
VAR vDenominatorTotal =
    CALCULATE ( [VOLUME], Data[Location] = "A" || Data[Productgroup] = 1 )
VAR vResultTotal =
    DIVIDE ( vNumeratorTotal, vDenominatorTotal )
VAR vResult =
    IF ( HASONEVALUE ( Data[Location] ), vResultSingle, vResultTotal )
RETURN
    vResult

 

DataInsights_1-1624310139408.png

 

 

DataInsights_0-1624310023931.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Great this is the solution, thank you very much for the support.


One thing to ask:
The vNumeratorTotal and vDenominatorTotal calculation does that handle the filters in sequence? Because it calculates the costs if it location A or productgroup 1. 
However Location A has also product group 1.

Am I right that the calculate function first looks at the first argument (so if it is location A, it just calculates the costs over the whole location), whilst it is not location 1 it calculates the costs over the product group?

@ReneDH,

 

The filter in the CALCULATE function uses OR ("||"), so if Location = A or ProductGroup = 1, that row is included.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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.