Forum Discussion

tchiang7's avatar
tchiang7
Frequent Visitor
1 year ago
Solved

NATURALLEFTOUTERJOIN not finding common join columns

I have three tables. 1. One is directQuery table "QuantifiedSelves Welln" which has direct query data from SQL database, 2. One is custom "CalendarDate" table created with Calendar() function -- Ca...
  • AntrikshSharma's avatar
    1 year ago

    tchiang7 You can join those tables by breaking the lineage for numeric columns use  +0 for text use & ""

    Join Sales & Product = 
    VAR SalesTable = 
        SELECTCOLUMNS ( 
            Sales, 
            "ProductKey", Sales[ProductKey] + 0, 
            Sales[Quantity], Sales[Net Price] 
        )
    VAR ProductsTable = 
        SELECTCOLUMNS ( 
            'Product', 
            "ProductKey", 'Product'[ProductKey] + 0, 
            'Product'[Brand], 'Product'[Color] 
        )
    VAR Result = 
        NATURALLEFTOUTERJOIN ( SalesTable, ProductsTable )
    RETURN
        Result

     

  • AntrikshSharma's avatar
    AntrikshSharma
    1 year ago

    There is another way of solving this, since JOIN functions expect same column names and same lineage but 2 Key column from different tables do not have the same lineage, even if they are connected through a relationship.

     

    In this scenario adding 0 or "" disconnects their lineage from the model and they both act as columns that don't exists in the model so JOIN functions work fine.

     

    However to make the lineage of both columns same we can use TREATAS which actually Treats one or more columns as the columns of some other tables.

     

    Join Sales & Products TREATAS = 
    VAR SalesTable = 
        SELECTCOLUMNS ( 
            Sales, 
            Sales[ProductKey], Sales[Quantity], Sales[Net Price] 
        )
    VAR ProductsTable = 
        TREATAS ( 
            SELECTCOLUMNS ( 
                'Products', 
                'Products'[ProductKey], 'Products'[Brand], 'Products'[Color]
            ),
            Sales[ProductKey], Products[Brand], Products[Color] 
        ) 
    VAR Result = 
        NATURALLEFTOUTERJOIN ( SalesTable, ProductsTable )
    RETURN
        Result

     

    So for the duration of the calculation Products[ProductKey] will be treated as Sales[ProductKey].