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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
RZAU
Frequent Visitor

Variable Help

Hi Power BI experts

 

I am still learning Power BI. And I am confused about this variable concepts.  Could you pls help explain a little bit?  Thank you!

 

Example:

 

Sales[Order Position]=
1 VAR CurrentOrderNumber= Sales[Order Number]

2 Var Postion=
    Calculate (

               DISTINCTCOUNT(Sales[Order Number]),

               Sales[Order Number]<=CurrentOrderNumber  //My confusion is here.  Is this the same column?  Both refer to same Sales[OrderNumber] since CurrentOrderNumber=Sales[Order Number]? //
)
Return  Position

2 ACCEPTED SOLUTIONS
FreemanZ
Super User
Super User

once defined, the variable is stored as constant value.  so in expression like:

Sales[Order Number]<=CurrentOrderNumber

the left part is a real variable row by row, the right part is actually a constant defined before. 

View solution in original post

v-binbinyu-msft
Community Support
Community Support

Hi @RZAU ,

For understand VAR statement, you should understand the context concept in Power BI, because it is influence by context.

VAR: Stores the result of an expression as a named variable, which can then be passed as an argument to other measure expressions. Once resultant values have been calculated for a variable expression, those values do not change, even if the variable is referenced in another expression.

Please refer below notes for your dax formula:

Sales[Order Position] =
VAR CurrentOrderNumber = Sales[Order Number] //get current value for [Order Number] column according to the line context
VAR Postion =
    CALCULATE (
        DISTINCTCOUNT ( Sales[Order Number] ),
        Sales[Order Number] <= CurrentOrderNumber //use the CurrentOrderNumber to filter the [Order Number]column, now CurrentOrderNumber is just a value not a column
    )
RETURN
    Position

 For more details, you can read related document: VAR keyword (DAX) - DAX | Microsoft Learn , Variables in DAX - SQLBI

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-binbinyu-msft
Community Support
Community Support

Hi @RZAU ,

For understand VAR statement, you should understand the context concept in Power BI, because it is influence by context.

VAR: Stores the result of an expression as a named variable, which can then be passed as an argument to other measure expressions. Once resultant values have been calculated for a variable expression, those values do not change, even if the variable is referenced in another expression.

Please refer below notes for your dax formula:

Sales[Order Position] =
VAR CurrentOrderNumber = Sales[Order Number] //get current value for [Order Number] column according to the line context
VAR Postion =
    CALCULATE (
        DISTINCTCOUNT ( Sales[Order Number] ),
        Sales[Order Number] <= CurrentOrderNumber //use the CurrentOrderNumber to filter the [Order Number]column, now CurrentOrderNumber is just a value not a column
    )
RETURN
    Position

 For more details, you can read related document: VAR keyword (DAX) - DAX | Microsoft Learn , Variables in DAX - SQLBI

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

FreemanZ
Super User
Super User

once defined, the variable is stored as constant value.  so in expression like:

Sales[Order Number]<=CurrentOrderNumber

the left part is a real variable row by row, the right part is actually a constant defined before. 

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.