Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
henkka
Helper II
Helper II

explanation wished: Two seamlesly same dax measures giving different results

Hi,

 

can anyone explain why these two different aproaches are giving different results ( first givin correct result)

Approach 1 - using three measures:


LatestAgreement =

 var latest = MAX ( 'WorkAgreement FACT'[Workagreement.id] )
 return
 latest

all latest agreement =
 CALCULATE ( [LatestAgreement] , ALL ( 'WorkAgreement FACT'[Workagreement.id] )
    )

Valid Agreements =
 VAR allagreements =
     VALUES ( 'WorkAgreement FACT'[Employee.id] )
 VAR agreementids =
     VALUES ( 'WorkAgreement FACT'[Workagreement.id] )
 RETURN
     COUNTX (
         allagreements,
         CALCULATE (
             COUNT ( 'WorkAgreement FACT'[Workagreement.id] ),
             FILTER ( agreementids, [LatestAgreement] = [all latest agreement] ),
             'WorkAgreement FACT'[Employee.id] <> BLANK (),
             StartDatesDim[Date] < TODAY () , 
             EndDatesDim[Date] > TODAY () || EndDatesDim[Date] = BLANK () )
     ) + 0


Approach number 2 (givin worng result):
 var latest = MAX ( 'WorkAgreement FACT'[Workagreement.id] ) + 0
 Var allLatest = CALCULATE ( latest, ALL ( 'WorkAgreement FACT'[Workagreement.id] ) )

 VAR allagreements =
     VALUES ( 'WorkAgreement FACT'[Employee.id] )
 VAR agreementids =
     VALUES ( 'WorkAgreement FACT'[Workagreement.id] )
     RETURN
     COUNTX (
         allagreements,
         CALCULATE (
             COUNT ( 'WorkAgreement FACT'[Workagreement.id] ),
              'WorkAgreement FACT'[Employee.id] <> BLANK (),
               FILTER ( agreementids, latest = allLatest ) ,
               StartDatesDim[Date] < TODAY (),
             EndDatesDim[Date] > TODAY () || EndDatesDim[Date] = BLANK ()          
         )
    ) + 0
1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @henkka ,

 

Believe the main reason is the way you have the second calculation using variables, variables are calculated in the scope of the context so depedning on how you make use of them the calcuations changes.

 

When you use the 3 different metric approach you are making use of the Calculate argument even if not called directly so the calculation changes.

 

The context transiction is very tricky.

 

Check this blog post https://radacad.com/caution-when-using-variables-in-dax-and-power-bi


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

3 REPLIES 3
MFelix
Super User
Super User

Hi @henkka ,

 

Believe the main reason is the way you have the second calculation using variables, variables are calculated in the scope of the context so depedning on how you make use of them the calcuations changes.

 

When you use the 3 different metric approach you are making use of the Calculate argument even if not called directly so the calculation changes.

 

The context transiction is very tricky.

 

Check this blog post https://radacad.com/caution-when-using-variables-in-dax-and-power-bi


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi, @MFelix and thanks for pointing out the reason behind that behaviour :)! I needed this understanding as I'm trying to create something a bit more efficient way to do this calculation 🙂

So the issue with the scenario 1 is that the variable is calculated and stored before the filtering and thus the filter is working as it should but the values for the filter are incorrect. The blog you linked was great!

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.

Users online (644)