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

Combine ALL() and FILTER() to remove rows

Dear all, 

 

I am relatively new to PowerBI and I am having some trouble understanding what criteria uses PowerBI to display or hide rows. 

Let's assume the following dummy data about sales:

 

CountryTable:

country_idcountry
1Spain
2Netherlands
3Germany

 

SalesTable:

sale_idcountry_idprovinceamount_euros
12A50
22B100
33C20

 

Both this tables are related by the country field with "direction" set to "Both". 

 

1. If I create a matrix with:

Rows: CountryTable.country

Values: SalesTable.amount_euros

Then, in the result table, PowerBI is omitting Spain, since it doesn't have any sales.

 

Now I want to do the same with the following calculation and I cannot understand why in this case PowerBI is displaying Spain. I want it to not show up:

 

2. I want to create a measure that displays the sum of amount_euros for provinces A and C, (but not filtered by country, so using the ALL() function). In other words, I want the following table:

 

countrynew_measure
Netherlands70
Germany70

 

If I do:

new_measure = 

VAR _table = FILTER (

ALL (SalesTable),

SalesTable[province] = "A" || SalesTable[province] = "C"

)

RETURN SUMX ( _table, [amount_euros] ) 

 

PowerBI is displaying:

 

countrynew_measure
Netherlands70
Germany70
Spain70

 

Isn't the filter of province supposed to "propagate"? Spain is not even showing up in the sales so Province is neither A or C...

 

Thank you very much in advance

1 ACCEPTED SOLUTION

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hello @Ashish_Mathur 

 

Thank you for your response. I am afraid though this is not the result I need: I need 70 in all rows, as I was stating in my original post.

 

Regards,

 

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
selimovd
Super User
Super User

Hello @Anonymous ,

 

in DAX every calculation is done independent of the calculations of other fields.

This means when you create in the visualization a table from the column CountryTable[country], the table looks like this:

country
Spain
Netherlands
Germany

 

If you add now the sum of SalesTable[amount_euros], this will be calculated for each row:

countryFormula evaluatedWhat is displayed
SpainSUM(amount_eurosblank ( will be hidden because blank)
NetherlandsSUM(amount_euros150
GermanySUM(amount_euros20

 

What happens now when you use the FILTER function?

 

The same mechanism happens for your calculation.

The FILTER(ALL(SalesTable))... will produce a table that looks like that:

sale_idcountry_idprovinceamount_euros
12A50
33C20

 

I will call this table for the rest of my response 'FILTER-Table'.

 

On this 'FILTER-Table' you use the SUMX function. Important here is that SUMX has a filter context (all the filter in the context, like the country in each row) and a row context (what happens as second argument, in your case you just refer to the column SalesTable[amount_euros] (by the way, always write column including the table and measures without. This is the common way to write DAX)).

 

So your SUMX is using 'FILTER-Table' and doing a SUM of the column 'FILTER-Table'[amount_euros], what is 50+20 = 70.

 

Your question is now why is Spain showing up?

This is what happens:

countrysumWhat is displayed
SpainThe FILTER function creates 'FILTER-Table', then SUMX happens70
NetherlandsThe FILTER function creates 'FILTER-Table', then SUMX happens70
GermanyThe FILTER function creates 'FILTER-Table', then SUMX happens70

 

 

The unexpected result comes from the fact, that you never do a context transition from the current context (country like Spain or in the next row Netherlands or in the next row Germany) to the row context of SUMX.

 

As the function is evaluated for every row, it also will show for Spain.

 

Does that make sense for you? I hope that was not too confusing 😅

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 
Anonymous
Not applicable

Hello @selimovd ,

 

Thank you very much for your reply. Allow me to go a bit "deeper" into it:

 

1. It's still not clear to me how to "hide" Spain. This is just a dummy example (I could simply hide it using the user interface filters of PowerBI Desktop) but in my real scenario I need this FILTER-table to be computed only for the countries that have sales. I guess I need some kind of "IF" structure but I cannot figure it out.

 

2. What do you mean exactly by:

 

2.1 all the filter in the context, like the country in each row. In fact,I struggle with something: the filter context here would be the variable "_table" or what I put in the PBI user interface in the "row" section when making the matrix, in this case the field "country"? I feel there is some ambiguity in general between the "filter context" and the "query context" I read in PBI's official doc.

 

2.2. by the way, always write column including the table and measures without. This is the common way to write DAX. You mean to write "SalesTable[amount_euros]" instead of "[amount_euros]"? I was not even sure if it was the same... I was quite confused because the "amount_euros" I want to sum is in fact the one from the FILTER-table you mention, not the one from the "SalesTable"... I thought that was what I was doing when writing [amount_euros] without the preceding table name...

 

2.3 you never do a context transition from the current context (country like Spain or in the next row Netherlands or in the next row Germany) to the row context of SUMX. How should I do this transition?

 

Thank you very much again for you help,

 

Kind regards,

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.