Forum Discussion

smpa01's avatar
smpa01
Community Champion
4 years ago
Solved

Constructing table in Dax from Variables

I am trying to figure out how can I end with a table from at least two variables

My code is following

 

Table = 
VAR first = "a"
VAR second = "b"
VAR x=  CROSSJOIN(DATATABLE("column1",STRING,{{first}}),DATATABLE("column2",STRING,{{second}}))
VAR y = CROSSjoin({first},{second})
RETURN _y

 

I want to end up with a table containg two columns with a and b that is quivalent of what the following can do

 

DATATABLE("first",string,"second",string,{{"a","b"}})

 

I can't get to where I need to as it is generating the following error in both instances

 

what is the workaround here?

 

 

 

 

 

  • smpa01 Not sure if this is what you are looking for? You can do crossjoin these two variables and rename the column name, or use selectcolumns and then cross join

     

    Table = 
    VAR a = {"a","b","c"}
    VAR b = {"e", "f","g"}
    RETURN
    CROSSJOIN ( SELECTCOLUMNS ( a, "First", [Value] ), SELECTCOLUMNS ( b, "Second", [Value] ) )

     

    or you want row 1 of each table to  join and then row 2 to row 2 and so on...

    what is your end goal?

     

6 Replies

  • az38's avatar
    az38
    Community Champion

    Hi smpa01 

    Maybe it's better to use ROW() instead of DATATABLE()?

    VAR x = CROSSJOIN( ROW("column1",first),ROW("column2",second) )
    • smpa01's avatar
      smpa01
      Community Champion

      az38  Wow !!! very elegant !!! Thanks

       

      I do have a follow up Q

       

      If

      VAR a = {"a","b","c"}

      VAR b = {"e", "f","g"}

       

      How can I still achieve the end goal without manually writing a ROW reference cause VAR a and VAR b are getting dynamically generated

  • smpa01 Not sure if this is what you are looking for? You can do crossjoin these two variables and rename the column name, or use selectcolumns and then cross join

     

    Table = 
    VAR a = {"a","b","c"}
    VAR b = {"e", "f","g"}
    RETURN
    CROSSJOIN ( SELECTCOLUMNS ( a, "First", [Value] ), SELECTCOLUMNS ( b, "Second", [Value] ) )

     

    or you want row 1 of each table to  join and then row 2 to row 2 and so on...

    what is your end goal?

     

    • smpa01's avatar
      smpa01
      Community Champion

      parry2k  It worked mate !!! thanks a lot.

       

      It was a very small part of a very useful huge calcuation and pieces are aligining.

       

      Thanks again. 

  • In addition to CROSSJOIN, you can also use GENERATE or GENERATEALL.

    Table = 
    VAR a = { "a", "b", "c" }
    VAR b = { "e", "f", "g" }
    VAR aTbl = SELECTCOLUMNS ( a, "First",  [Value] )
    VAR bTbl = SELECTCOLUMNS ( b, "Second", [Value] )
    RETURN
        GENERATE ( aTbl, bTbl )