Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

DAX: Is it possible to initialize existing columns as variables in a new table?

Hi, I am new to DAX. I have 2 questions on initializing variables, specifically initializing columns as variables (VAR) : 
1. Is it possible to initialize columns (from existing tables) as variables in a new table?

2. Is it possible to create new columns as variables in a new table and the variable is then used to be referred to other calculations in the same table?

 

P/S: I may not good in explaining things since I am new to this, sorry about that. You can just reply me if you don't understand my questions. Thank you

  • Hi, 

    According to your description, here’s my opinion on your two questions:

    For the first one, I think it’s not possible if the column you used in the table has a relationship with your current table, if so, you can simply use the Related() function in the DAX to create a calculated column like this:

    Column= Related (‘Table2’[Column2])

    https://docs.microsoft.com/en-us/dax/related-function-dax

     

    For the second one, I think the example posted by Greg is a very great example, you can achieve this through his suggestions.

     

    Thank you very much!

     

    Best Regards,

    Community Support Team _Robert Qin

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

5 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    Anonymous Not sure what you are going for but this is definitely possible if I understand correctly, like:

    Table2 = 
      VAR __Column = SELECTCOLUMNS('Table',"ID",[ID])
      VAR __Table = ADDCOLUMNS(__Column,"One",1)
    RETURN
      __Table
    • Anonymous's avatar
      Anonymous
      Not applicable

      hi Greg_Deckler 
      thank you for your time! i've tried using your method but there is an error. do you know why this happen? thank you

       

       

      • Greg_Deckler's avatar
        Greg_Deckler
        Icon for Community Champion rankCommunity Champion

        Anonymous In your first VAR columnstep1 you add a Date and Country column. But then in your second VAR tablestep1 you base this on columnstep1 and then try to add Date and Country columns again. They are already there, you do not need to add them again.

  • v-robertq-msft's avatar
    v-robertq-msft
    Icon for Community Support rankCommunity Support

    Hi, 

    According to your description, here’s my opinion on your two questions:

    For the first one, I think it’s not possible if the column you used in the table has a relationship with your current table, if so, you can simply use the Related() function in the DAX to create a calculated column like this:

    Column= Related (‘Table2’[Column2])

    https://docs.microsoft.com/en-us/dax/related-function-dax

     

    For the second one, I think the example posted by Greg is a very great example, you can achieve this through his suggestions.

     

    Thank you very much!

     

    Best Regards,

    Community Support Team _Robert Qin

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.