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
Magnus-CPH-DK
Helper I
Helper I

DAX measure - Return single value matching shared columns between two related tables + slicer value

Hi all,

 

My task is pretty straight forward, yet I cannot figure out the solution:

 

I want to create a measure that finds (CALCULATE?) the Contract Accountable in Dim_Contract_table given the sliced value Contract Name in Fact_Control_Table and by matching Control ID A which exists in both tables (And which is the unique PK in Dim_Contract_Table).

 

This is a mockup of the data model:

 

DAX 2 matching columns values + slicer.drawio (1).png

 

I have searched for different functions that I thought could be useful such as RELATED and LOOKUPVALUE but I wasn't able to get them to work. 

I thought of something like this:

 

Contract Accountable =
VAR Con_Acc =

CALCULATE(
   MAX(Dim_Contract_Table[Contract Accountable]),
      FILTER(
         Fact_Control_Table,
         Fact_Control_Table[Contract Name] = SELECTEDVALUE(Fact_Control_Table[Contract Name]))
            FILTER(
               Dim_Contract_Table,
               Dim_Contract_Table[Control ID A] = LOOKUPVALUE(
               Fact_Control_Table[Control ID A], 
               Dim_Contract_Table[Control ID A], Dim_Contract_Table[Control ID A])
               )
            )
RETURN
Con_Acc


Any ideas?

 

Best Regards,

Magnus

1 ACCEPTED SOLUTION
daXtreme
Solution Sage
Solution Sage

@Magnus-CPH-DK 

 

Since there's a one-to-many between the tables, how do you know which Contract Accountable you want to retrieve for a Contract Name? I can see you want to use MAX, but is it really what you want to do? There might be many of these for a single Contract Name. Your formula is too convoluted and, in fact, will be very, very slow on even moderately sized data sets. The principle of DAX is never to put an entire table as a filter. This is dangerous and leads to poor performance. A good formula uses only the columns it absolutely needs to. Something like:

 

MAX Contract Accountable = 
MAXX(
    summarize(
        Fact_Control_Table,
        Dim_Contract_Table[Contract Accountable]
    ),
    Dim_Contract_Table[Contract Accountable]
)

View solution in original post

3 REPLIES 3
Magnus-CPH-DK
Helper I
Helper I

Hi @daXtreme 
Thank you very much for your reply and explanation. I tried out your measure, and it solved the job. In the meantime I managed to find a solution as well which gives the same result as yours once the Contract name slicer is applied.
I think it would be super interesting to look into what the differences are and how the influence performance.

This is the measure I used:

Contract Accountable = 

VAR Contract_Accountable = 
CALCULATE(
    MAX(Dim_Contract_Table[Contract Accountable]),
        FILTER(
            'Fact_Control_Table',
            'Fact_Control_Table'[Contract Name] = _Measures[Selected Contract Name])
)
               
Return
IF(
    ISBLANK(Contract_Accountable), "No registrations", Contract_Accountable)

 
I used MAX in the first place since I need to use an aggregator when using CALCULATE on a column in a measure. I don't know other workarounds or best practices for acheiving the same result.

Regarding your question concerning the one-to-many relationship it shouldn't be an issue looking up the contract accountable in the Dim table, because it only contains one unique row per contract name. 

@Magnus-CPH-DK 

 

The measure you've presented is bad for the simple reason that you've put the full extended fact table in FILTER and then use it as an argument in CALCULATE. This has many very unpleasant consequences. In fact, so many that it would take an entire chapter in a book to explain. Please, don't do this.

daXtreme
Solution Sage
Solution Sage

@Magnus-CPH-DK 

 

Since there's a one-to-many between the tables, how do you know which Contract Accountable you want to retrieve for a Contract Name? I can see you want to use MAX, but is it really what you want to do? There might be many of these for a single Contract Name. Your formula is too convoluted and, in fact, will be very, very slow on even moderately sized data sets. The principle of DAX is never to put an entire table as a filter. This is dangerous and leads to poor performance. A good formula uses only the columns it absolutely needs to. Something like:

 

MAX Contract Accountable = 
MAXX(
    summarize(
        Fact_Control_Table,
        Dim_Contract_Table[Contract Accountable]
    ),
    Dim_Contract_Table[Contract Accountable]
)

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.