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

Excel DAX query with bridge table using CROSSFILTER

Hi, this is my second take on a problem I have using the correct syntax on an Excel DAX query:

RelationshipsRelationships

 

TablesTables

 

I did a working measure in powerpivot using a pivot table with filters:

CountryGrp:=CALCULATE(SUM(DataTable[Value]);CROSSFILTER(DataTable[IdCountry];Country[IdCountry];Both);CROSSFILTER(Country[IdCountry];CountryGroup[IdCountry];Both))

Can I duplicate this behavior with a DAX query?

I need to SUM the values where IdCountryGroup = 1 and Years = 2019

The result would be:

2019  North America  15000

 

This is where I'm at with no success:

EVALUATE
ADDCOLUMNS (
    SUMMARIZE ( DataTable, DataTable[Year] ),
    "Val",
        CALCULATE (
            SUM ( DataTable[Value] ),
            CROSSFILTER ( DataTable[IdCountry], Country[IdCountry], BOTH ),
            CROSSFILTER ( Country[IdCountry], CountryGroup[IdCountry], BOTH ),
            FILTER ( CountryGroup, CountryGroup[IdCountryGroup] = 1 )
        )
)

And how can I put a filter in 2 different tables?
Thank you for your help!

1 ACCEPTED SOLUTION

@JFGrenier  Sure, that's the best decision, you may write a complex DAX that might break tomorrow if not written considering every possible detail, but a data model that's perfect would always give you the required soution with minimal DAX!

View solution in original post

11 REPLIES 11
CNENFRNL
Community Champion
Community Champion

Weird that my previous reply disappeared without any trace ...🤔

@JFGrenier IMHO, in order to propagate filter from multiple(*) side to one(1) side of a relationship, expanded table would be a preferable choice.

As to your issue, filters on CountryGroup(*) can take effect to Country(1) this way,

 

CALCULATETABLE ( VALUES ( Country[IdCountry] ), CountryGroup )

 

then such a filtering propagates naturally from Country(1) to DataTable(*) subsequently.

Pls try measure

 

Total Values :=
CALCULATE (
    SUM ( DataTable[Value] ),
    CALCULATETABLE ( VALUES ( Country[IdCountry] ), CountryGroup )
)

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

AntrikshSharma
Community Champion
Community Champion

@JFGrenier Just change the data model a little bit and the DAX that you have to write will be short and simple, working PBI file is attached below my signature:

1.PNG2.PNG

Hello AntrikshSharma!

@JFGrenier  Still you should focus on changing the model, create 2 rows for Egypt in that case, with a unique key for both?

Let's say that Egypt is in 3 CountryGroup, thing is that I would have to copy the data associated with Egypt 2 more times with a different Id. I'm already at 14 millions rows in the DataTable and for a 32 bits system, I think it's near maximum.  But what if I add multiple Id column in my CountryGroup table. I'll check how many country can be in multiple CountryGroup. If there are 15 country, I'd just add 15 columns of Id's  and change the relationship accordingly. Do you think the DAX function RELATED could work? Or do you know of any other function that can change relationships?  My DAX queries are built at run time via an Excel userform and trapping these exceptions would be easy.  You point me in a new direction! Changing the model maybe my only solution!

 

But before that, I'll give one more shot at this query:

Where (syntax) could I add a filter to the  DataTable[Year]=2019 && DataTable[HS2]=10  in the query below?

In the Summarize function, in the Calculate or else???

EVALUATE
ADDCOLUMNS (
    SUMMARIZE ( DataTable, DataTable[Year] ),
    "Val",
        CALCULATE (
            SUM ( DataTable[Value] ),
            CROSSFILTER ( DataTable[IdCountry], Country[IdCountry], BOTH ),
            CROSSFILTER ( Country[IdCountry], CountryGroup[IdCountry], BOTH ),
            FILTER ( CountryGroup, CountryGroup[IdCountryGroup] = 1 )
        )
)

 

Thank's again!

 

 

@JFGrenier  Try this: for countryname in case there are more than 1 value you can use CONCATENATEX ( CALCULATETABLE construct. The file is below my signature:

Table =
ADDCOLUMNS (
    CALCULATETABLE ( SUMMARIZE ( Data, Data[Year] ), Data[Year] = 2019 ),
    "Val",
        CALCULATE (
            [Total Value],
            CROSSFILTER ( Data[IdCountry], Country[IdCountry], BOTH ),
            CountryGroup[IdCountryGroup] = 1
        ),
    "CountryName",
        CALCULATE (
            DISTINCT ( CountryGroup[CountryGroupName] ),
            CROSSFILTER ( Data[IdCountry], Country[IdCountry], BOTH ),
            CountryGroup[IdCountryGroup] = 1
        )
)

 1.PNG

Hello Antriksh,

I tried your latest DAX solution in my real 'Data model' (what I showed is a scale down and translated version).

Had to adapt it a bit for DAX studio in french and checked several times to match your code!

The filter on IdCountryGroup do not seem to have any effect... The country group name changes accordingly but the returned values are always the same. It's the sum of all values of the Data table for the filtered year.  Guess I will have to change my data model.

 

Thank you for your time and knowledge!

 

 

 

@JFGrenier  Sure, that's the best decision, you may write a complex DAX that might break tomorrow if not written considering every possible detail, but a data model that's perfect would always give you the required soution with minimal DAX!

I personally avoid leveraging CROSSFILTER as much as possible, especially in a complex data model.

As to your model, in order to propagate filters on CountryGroup(*) to Country(1), expanded table is an excellent choice,

 

 

CALCULATETABLE ( VALUES ( Country[IdCountry] ), CountryGroup )

 

Furthermore, filters on Country(1) or Month(1) propagate in a natual way to Data(*) for any calculations; thus, I'd author a measure this way,

 

Total Values =
CALCULATE (
    SUM ( Data[Value] ),
    CALCULATETABLE ( VALUES ( Country[IdCountry] ), CountryGroup )
)

 

 

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Greg_Deckler
Super User
Super User

@JFGrenier - Not quite sure I understand the full situation here. If you have the same data model in both places, Excel and Power BI the DAX should be the same. 

 

Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
amitchandak
Super User
Super User

@JFGrenier , Dax is the same at both places, should work.

Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

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.