Forum Discussion

powerbiexpert22's avatar
powerbiexpert22
Impactful Individual
2 years ago
Solved

variable in DAX expression

what standards or best practices we should follow while defining the variables (including variable name) in DAX expression

  • Hello powerbiexpert22 ,  SQLBI covers the topic well in their article titled Variables in DAX, a part of the DAX101 Series.

     

    If your requirement is solved, please make THIS ANSWER a SOLUTION ✔️ and help other users find the solution quickly. Please hit the LIKE 👍 button if this comment helps you.  Proud to be a Super User!

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi  powerbiexpert22 ,

     

    There are no standardized rules for defining variable names. Personally, I usually start my variable names with two "_" so I can quickly determine which one is the variable. This also makes it easier to call them quickly.

    VAR __variablename = ...


    It is also recommended that variable names describe this step as much as possible. Avoid using meaningless characters such as "a", "b", "c", etc. as variable names.

    The advantage of variables is that they are calculated only once for each variable (it is important to note here that variables are calculated at the time of definition, not at the time of use), and can be replaced with variables for frequently used parts.

    The use of variables after RETURN instead of expressions is useful when checking the results of intermediate steps, and you can always replace the variables after RETURN to check the results.

    Hope these help.

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

2 Replies

  • foodd's avatar
    foodd
    Community Champion

    Hello powerbiexpert22 ,  SQLBI covers the topic well in their article titled Variables in DAX, a part of the DAX101 Series.

     

    If your requirement is solved, please make THIS ANSWER a SOLUTION ✔️ and help other users find the solution quickly. Please hit the LIKE 👍 button if this comment helps you.  Proud to be a Super User!

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi  powerbiexpert22 ,

     

    There are no standardized rules for defining variable names. Personally, I usually start my variable names with two "_" so I can quickly determine which one is the variable. This also makes it easier to call them quickly.

    VAR __variablename = ...


    It is also recommended that variable names describe this step as much as possible. Avoid using meaningless characters such as "a", "b", "c", etc. as variable names.

    The advantage of variables is that they are calculated only once for each variable (it is important to note here that variables are calculated at the time of definition, not at the time of use), and can be replaced with variables for frequently used parts.

    The use of variables after RETURN instead of expressions is useful when checking the results of intermediate steps, and you can always replace the variables after RETURN to check the results.

    Hope these help.

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group