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

Formula incl. subtotal calculation connected to filters /slicers

Hello.

I have a complex use case with PowerBI where I need help with. It has to do with formulas with subtotals that should be connected to slicers/filters.
As I understand I cannot use calculated columns as they will not update when I change the filter in a visual / table.

 

Let's assume I have the Sales and Margin Data for Customers for 2 years (indicated by different columns). If there are sales in both periods, they are called "Matching Pair". This is how the raw data looks like (last column optional, as it has to be calculated from scratch whenever applying filters:

SegmentCountryCustomerProductRevenues 2020Revenues 2019Margin 2020Margin 2019Matching PairsSales (price impact)Sales (price) %
AChinaCustomer XProduct 139’06129’61614’98711’551Matching Pairs30’6000%
BChinaCustomer YProduct 210’00011’3897’0006’247Matching Pairs-17%
BUSCustomer VProduct 152’00049’47129’39733’260Matching Pairs-531%
CGermanyCustomer ZProduct 230’0001’03520’000740Matching Pairs00%
CArgentinaCustomer WProduct 2 7’059 2’843Non Matching170%
AArgentinaCustomer UProduct 12’3094’6009351’878Matching Pairs204%
AUSCustomer TProduct 14’504 1’706 Non Matching110%
SubTotal   137’875103’17074’02556’519 30’64241%

 

The last column is calculated using the following Excel formula (haven't adjusted it yet to DAX as I am not sure yet how to do this using subtotals, filters and still calculating on a row level):

 

IF([@[Matching Pairs]]="matching pairs";IFERROR(-([@[Margin 2019]]*[@[Sales (price impact)]]*Table[[#SubTotals];[Revenues 2019]]/[@[Revenues 2019]]-([@[Margin 2019]]*Table[[#SubTotals];[Revenues 2019]]))/([@[Revenues 2020]]*((Table[[#SubTotals];[Revenues 2019]])^2)/([@[Revenues 2019]]));0);0)

 

It includes the Subtotals of Revenue 2019.

I want to create now a table / visual, that for example looks like this:

 

CountrySum of Sales (price) %
Argentina4%
China7%
Germany0%
US31%
Grand Total41%

And has a couple of filter /slicers options (e.g. product, country, segment). 

 

As the Sales Price % is calculated on a line item level and involves subtotals, the value is supposed to change, when I change the filters. 

I understand, that when I use a calculated column, dynamic filtering is not possible. And when using a measure, I would need to have the same granularity in the visual as in the raw data (but this I can't do, as my raw data in fact includes many more columns than in the example on top). 

 

Is there any other solution to this problem? 

 

Many thanks in advance! 

5 REPLIES 5
v-lionel-msft
Community Support
Community Support

Hi @Anouk7770 ,

 

Has your problem been solved?

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Lionel

 

Thanks a lot! 

The subtotal formula you suggested works perfectly. Still struggeling with the other formula though, I think the issue is still that the measure does not calculate it on a row level when I don't have the same granularity in my displayed table. 

 

I tried using that formula:

 

Sales (Price %) =
IF(
MAX(
Sheet2[Matching Pairs])="Matching Pairs",
SUMX (Sheet2,
IFERROR(-
(
Sheet2[Margin 2019] * Sheet2[Sales (price impact)]*[Subtotal of R_2019]/Sheet2[Revenues 2019]-(
Sheet2[Margin 2019]*[Subtotal of R_2019]))/(
Sheet2[Revenues 2020]*(
[Subtotal of R_2019]*[Subtotal of R_2019])/(
Sheet2[Revenues 2019])),
0)),
0)

 

It doesn't give me the results I was hoping for though. I tried to attach the sample PbIx file but it doesn't work somehow.  

Thanks! Kind regards

 

v-lionel-msft
Community Support
Community Support

Hi @Anouk7770 ,

 

Try to create such a measure.

Measure = 
IF(
    MAX( [Matching Pairs] ) = "Matching pairs",
    IFERROR(
        //Since I did not understand your mathematical calculation logic, here MAX([Margin 2019]) * MAX([Margin 2020]) is just an example.
        MAX([Margin 2019]) * MAX([Margin 2020]),
        0
    ),
    0
)

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Lionel

Thanks a lot! But the MAX function will not give me the subtotal (sum of all line items that are not excluded by a filter), no? it will give me just the maximum value, or am I wrong?

 

Thanks!

 

Kind regards

Anouk

Hi @Anouk7770 ,

 

Or try this.

First create a few measures to calculate the subtotals for each field, and then reference them in the formula. 

Subtotal of R_2019 = 
SUMX(
    ALLSELECTED(Sheet2),//This subtotal will change as the filters change.
    [Revenues 2019]
)
Subtotal of R_2020 = 
SUMX(
    ALLSELECTED(Sheet2), // This subtotal will change as the filters change.
    [Revenues 2020]
) 

 

Measure = 
IF(
    //The MAX() function here returns the value of Matching Pairs in the current row, not the maximum value.
    MAX(Sheet2[Matching Pairs]) = "Matching pairs", 
    IFERROR(
        //sample
        [Subtotal of R_2019] * [Subtotal of R_2020],
        0
    ),
    0
)

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

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.