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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Steve_L
Frequent Visitor

var different result

Hi 

 

I have a problem of using VAR in DAX recently that I try to use VAR to make my formula more readable, but finally it returns totally different result, is there anyone can help me with this? Thanks a lot!!

 

Original Code, this code works well for me that ranks the products according to the quantity sold in current month:

=
RANKX (
    ALL ( 'Sales'[Product Name] ),
    CALCULATE (
        SUM ( 'Sales'[Value] ),
        'Sales'[Value Type] = "QTY",
        LASTDATE ( 'Sales'[Month/Year] )
    ),
    ,
    DESC,
    DENSE
)

 

Modified Code, this code reutrns 1 for all of my products:

=VAR QTY1M =
 CALCULATE (
        SUM ( 'Sales'[Value] ),
        'Sales'[Value Type] = "QTY",
        LASTDATE ( 'Sales'[Month/Year] ))
return

RANKX (
    ALL ( 'Sales'[Product Name] ),
   QTY1M,
    ,
    DESC,
    DENSE
)

 

1 ACCEPTED SOLUTION
erik_tarnvik
Solution Specialist
Solution Specialist

In the first example CALCULATE is evaluated within the iteration context of the RANKX function. In the second example, the CALCULATE function is evaluated first (within whatever context you place the whole expression) and the result QTY1M is used within the RANKX function. 

View solution in original post

2 REPLIES 2
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @Steve_L,

 

You can create a new measure instead of a var formula. It looks like this:

QTY1M =
 CALCULATE (
        SUM ( 'Sales'[Value] ),
        'Sales'[Value Type] = "QTY",
        LASTDATE ( 'Sales'[Month/Year] ))
FinalMeasure =
RANKX ( ALL ( 'Sales'[Product Name] ), [QTY1M], , DESC, DENSE )

 

Best Regards!

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
erik_tarnvik
Solution Specialist
Solution Specialist

In the first example CALCULATE is evaluated within the iteration context of the RANKX function. In the second example, the CALCULATE function is evaluated first (within whatever context you place the whole expression) and the result QTY1M is used within the RANKX function. 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.