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

Calculate with and without variables difference

I have 2 calculate formulas: 

Formula 1

Emp Exits =
        CALCULATE(
            SUM(Emp_Exits[Employee Exits]),
            Emp_Exits[MonthNo]<=MAX(Emp_Exits[MonthNo]),
            ALL(Emp_Exits[Month Name])
            )
Formula 2:
Emp Exits with variable =
    VAR EmpExits = SUM(Emp_Exits[Employee Exits])
    RETURN
        CALCULATE(
            EmpExits,
            Emp_Exits[MonthNo]<=MAX(Emp_Exits[MonthNo]),
            ALL(Emp_Exits[Month Name])
            )
 
Both the formulas are same except that in Formula 2, i used variable.  See the below output screenshot.
Formula 1 is giving correct result and formula 2 is not giving the correct result. Why is this behaviour?
seefadeeb_0-1684566793509.png
1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Anonymous 
The measure

Emp Exits with variable =
VAR EmpExits =
    SUM ( Emp_Exits[Employee Exits] )
RETURN
    CALCULATE (
        EmpExits,
        Emp_Exits[MonthNo] <= MAX ( Emp_Exits[MonthNo] ),
        ALL ( Emp_Exits[Month Name] )
    )

is exactly the same as 

Emp Exits with variable =
SUM ( Emp_Exits[Employee Exits] )

The reason is that variables are only evaluated once therefore cannot be recalculated as measures inside a CALCULATE statement. Thus only the value of the first measure is always returned.

View solution in original post

4 REPLIES 4
tamerj1
Super User
Super User

Hi @Anonymous 
The measure

Emp Exits with variable =
VAR EmpExits =
    SUM ( Emp_Exits[Employee Exits] )
RETURN
    CALCULATE (
        EmpExits,
        Emp_Exits[MonthNo] <= MAX ( Emp_Exits[MonthNo] ),
        ALL ( Emp_Exits[Month Name] )
    )

is exactly the same as 

Emp Exits with variable =
SUM ( Emp_Exits[Employee Exits] )

The reason is that variables are only evaluated once therefore cannot be recalculated as measures inside a CALCULATE statement. Thus only the value of the first measure is always returned.

Ajendra
Resolver I
Resolver I

Hey,

 

Please try this one!!

 

1.JPG

Anonymous
Not applicable

The MonthName column is derived from Month column using formula FORMAT([Month],"MMM").

DATESYTD is working fine with Month but not working with MonthName column which is derived from Month column:

 
Formula:
CALCULATE(
        (1-DIVIDE(SUM(Emp_Exits[Employee Exits]),SUM(Emp_Exits[Approved Headcount]))),
        DATESYTD(Emp_Exits[Month])
        )

Try to create a seperate date table as shown below then apply your DAX accordingly.

 

DateMaster =
    ADDCOLUMNS(CALENDAR(MIN(FactTable[Date]),MAX(FactTable[Date]))
    ,"Month",FORMAT([Date],"MMMM")
    ,"Sorting",FORMAT([Date],"YYYYMM")
    )

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.