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
Anonymous
Not applicable

Measure that calculates the filtered total with relationship between tables

Hi there, I'm quite new to PowerBI and I'm having an issue calculating a percentage inside a measure, and I've identified the problem, in the total value. 

I've two tables:
TABLE: SUMMARY

CategoryProductYearValue
AApples2018150
ABananas2018200
BStrawberries2018250
BCherries2018300
CMelons

2018

350
CWatermelons2018400
AApples2019450
ABananas2019500
BStrawberries2019550
BCherries2019600
CMelons2019650
CWatermelons2019700

 

TABLE: GENERAL

CategoryYearTotal value
A

2018

1500
B20182000
C20182500
A20193000
B20193500
C20194000

 

I built a relationship many to many between tables using the Category variable.

The column GENERAL[Total value] contains the total value for category A, knowing that for table SUMMARY it's not mandatory to download in the database all products of category A => total value =\= SUM(SUMMARY[Value] with filter for category A.

I've a Slicer on Product level where for example I want to select Apples, Bananas and Strawberries only for Year 2018 and obtain a table as it follows:

ProductNumeratorDenominator
Apples1503500
Bananas2003500
Strawberries2503500

 

Where the Numerator is only the value of the Product in the table SUMMARY, instead the Denominator is the sum of the Total Value of the corresponding categories for filtered products (in this case I've products from category A and B, so is the some of Total Value of table GENERAL for categories A and B => then I can obtain a % as DIVIDE(Numerator; Denominator)

At the moment via measures, what I've achieved is the following:

ProductNumeratorDenominator
Apples1501500
Bananas2001500
Strawberries2502000

 

So I can't figure it out how to obtain a general Denominator that changes with the Products selected on the filter (for instance if I select in the slicer Products from Categoris B and C the Denominator should be 4500 (I'm filtering everything on year 2018) and so on (if I select a product or more for every category, the Denominator should be 6000)

I hope I've been clear enough, thanks in advance for helping me! 

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @Anonymous ,

I'd like to suggest you create a new table extract category values from two tables and use it as a bridge to link to tables instead of using many to many relationships.

Bridge =
DISTINCT (
    UNION ( VALUES ( SUMMARY[Category] ), VALUES ( GENERAL[Category] ) )
)

DAX Puzzle: Multiple many-to-many relationships 

These value will auto summarize based on their relationship:
10.png9.png

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

4 REPLIES 4
v-shex-msft
Community Support
Community Support

Hi @Anonymous ,

I'd like to suggest you create a new table extract category values from two tables and use it as a bridge to link to tables instead of using many to many relationships.

Bridge =
DISTINCT (
    UNION ( VALUES ( SUMMARY[Category] ), VALUES ( GENERAL[Category] ) )
)

DAX Puzzle: Multiple many-to-many relationships 

These value will auto summarize based on their relationship:
10.png9.png

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
edhans
Super User
Super User

I'm not clear on where you are getting your values. For example, you said if you select a slicer for every category, the denominator should be 6,000, but the detail table only has 5,100 total.

 

I'd forget the summary table, and especially any Many - to - Many relationship. Those are tricky and should only be used in very specific circumstances.

 

Try the below measure. It may not be what you want, but would be a matter of changing the REMOVEFILTERS() funtion's parameters, or swapping it out with ALLEXCEPT() or ALLSELECTED() depending on needs.

 

Percent of Total = 
VAR Denominator = 
    CALCULATE(
        [Total Value],
        REMOVEFILTERS(Sales)
    )
VAR Numerator = SUM(Sales[Value])
RETURN
DIVIDE(
    Numerator,
    Denominator,
    0
)

take a look at that and see if that helps. You should almost always (99.9999% of the time) use a detail table to create totals by removing filters, not by creating a new table to try and relate to it.

Post back and we'll see if we can tweak it to your exact needs once you've looked at it.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Anonymous
Not applicable

Hi edhans thanks for the reply!! 

The thing of summary and general table is not something I can forget because I've a big dashboard where both are needed (the summary is a focus on product level, the general on category, because for the summary you download only part of products for every category, like top x% products for value). 

The 5100 is the total of value in summary table, but as I mentioned it is not exhaustive of all products that could be inserted in the database => the general table instead contains the total value for each category (let's assume that category A has for year 2018 3 products: Apples with a value of 150, Bananas with a value of 200 and Cheese with a value of 1150 => the sum is 1500, so in the general table you see 1500 as a total for category A, but in the summary table, you don't have the value for cheese because in my vba code where I create the database is not mandatory to download data for all the products).

Let me know if I've been a little bit clearer, I know that is really tricky 😞 

PS: I've tried to use the measure you indicated, but I don't understand why, if I type CALCULATE([Name of Column]) it gives me an error saying that value for [Name of Column] cannot be determined and also when it comes to filter, I can use all the functions that I know like ALL(), ALLEXCEPT(), ALLSELECTED(), but I can't use the REMOVEFILTERS(), it doesn't even appear in the suggestion when I start typing; is it maybe I'm using an old version of Power BI?

Ok. By not having the summary table be the same as the detail table (records missing) it raises issues. You may need to study this document and focus on the different granularity section (higher grain facts section). It is one of the few times to use a many-to-many relationship, but your life would definitely be easier if the detail table had all of the records and the totals were just math.

 

As for CALCULATE([column name]) won't work. Calculate either needs a measure in the expression or a fully qualified table[column] name.

 

I think REMOVEFILTERS() was added a few months ago - July/August 2019 perhaps? You can use ALL() instead. REMOVEFILTERS is an alias I prefer to use as that is what you are doing within CALCULATE whereas ALL() is capable of much more, but for your needs ALL() will work. Or, you are typing REMOVEFILTERS() in the wrong place. It can only be used as a filter in a CALCULATE() or CALCULATETABLE() function. Doesn't work by itself, within FILTER() or any other function.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

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.