Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 months ago
Solved

How to join 2 tables using DAX without losing any rows from either table

Hello,

 

I have 2 calculated tables that I need to merge into 1 table without losing any rows from either table. As they are calculated tables, I can't use Power Query. How can I achieve this in DAX please?

 

For example:

TableA:

LinkIDDateCompanyNumberCompanyNameBranch Number
114_11/11/2025_211/11/2025114ABC2
114_11/11/2025_211/11/2025114ABC2
114_11/11/2025_111/11/2025114ABC1

 

TableB:

LinkIDTestATestB
114_11/11/2025_212311
114_11/11/2025_232122
114_11/11/2025_3985425

 

Merge A and B together to get:

LinkIDDateCompanyNumberCompanyNameBranch NumberTestATestB
114_11/11/2025_211/11/2025114ABC212311
114_11/11/2025_211/11/2025114ABC232122
114_11/11/2025_111/11/2025114ABC1nullnull
114_11/11/2025_3nullnullnullnull985425

 

Thank you.

  • Anonymous To extend what ExcelMonke mentioned, you can do something like this:

     

    Table New = 
    --create table A
    VAR __T1 =
        SELECTCOLUMNS ( 
            TableA,
            "LinkID", TableA[LinkID] & "",
            "Date", TableA[Date],
            "CompanyName", TableA[CompanyName],
            "CompanyNumber", TableA[CompanyNumber],
            "BranchNumber", TableA[Branch Number]
        )
    --create table b
    VAR __T2 = 
        SELECTCOLUMNS (
            TableB,
            "LinkID", TableB[LinkID] & "",
            "TestA", TableB[TestA],
            "TestB", TableB[TestB]
        )
    --get matching rows from tablea and tableb, keep only distinct rows to avoid duplicates
    --because of many to many
    VAR __T3 = DISTINCT ( NATURALLEFTOUTERJOIN ( __T1, __T2 ) )
    
    --find out which LinkID are already part of the output
    VAR __T4 = DISTINCT ( SELECTCOLUMNS ( __T3, [LinkID] ) )
    
    --get remaining ids from tableb, and add missing column with the blank values
    VAR __T5 = 
        SELECTCOLUMNS ( 
            FILTER ( __T2, NOT [LinkID] IN __T4 ),
            "LinkID", [LinkID],
            "Date", BLANK (),
            "CompanyName", BLANK (),
            "CompanyNumber", BLANK (),
            "BranchNumber", BLANK (),
            "TestA", [TestA],
            "TestB", [TestB]
        )
    
    RETURN
    --combine tables to get the final output
    UNION ( __T3, __T5 )

     

     

5 Replies

  • Anonymous To extend what ExcelMonke mentioned, you can do something like this:

     

    Table New = 
    --create table A
    VAR __T1 =
        SELECTCOLUMNS ( 
            TableA,
            "LinkID", TableA[LinkID] & "",
            "Date", TableA[Date],
            "CompanyName", TableA[CompanyName],
            "CompanyNumber", TableA[CompanyNumber],
            "BranchNumber", TableA[Branch Number]
        )
    --create table b
    VAR __T2 = 
        SELECTCOLUMNS (
            TableB,
            "LinkID", TableB[LinkID] & "",
            "TestA", TableB[TestA],
            "TestB", TableB[TestB]
        )
    --get matching rows from tablea and tableb, keep only distinct rows to avoid duplicates
    --because of many to many
    VAR __T3 = DISTINCT ( NATURALLEFTOUTERJOIN ( __T1, __T2 ) )
    
    --find out which LinkID are already part of the output
    VAR __T4 = DISTINCT ( SELECTCOLUMNS ( __T3, [LinkID] ) )
    
    --get remaining ids from tableb, and add missing column with the blank values
    VAR __T5 = 
        SELECTCOLUMNS ( 
            FILTER ( __T2, NOT [LinkID] IN __T4 ),
            "LinkID", [LinkID],
            "Date", BLANK (),
            "CompanyName", BLANK (),
            "CompanyNumber", BLANK (),
            "BranchNumber", BLANK (),
            "TestA", [TestA],
            "TestB", [TestB]
        )
    
    RETURN
    --combine tables to get the final output
    UNION ( __T3, __T5 )

     

     

  • ExcelMonke's avatar
    ExcelMonke
    Icon for Impactful Individual rankImpactful Individual

    Hello,

    Whilst the recommendation would be to do this in Power Query. Otherwise, consider using a function like CROSSJOIN or ADDCOLUMNS. 


     

  • ExcelMonke's avatar
    ExcelMonke
    Icon for Impactful Individual rankImpactful Individual

    Hello,

    Whilst the recommendation would be to do this in Power Query. Otherwise, consider using a function like CROSSJOIN or ADDCOLUMNS. 


     

  • Hi, the easiest and most stable solution is to recreate the two tables in Power Query and perform a Full Outer Join there. If you could share the logic of how your 2 Calculated tables were created, other users would also be able to provide you with an alternative solution so that you can compare. 

  • I am not sure about stable approach doing in PQ vs DAX but surely as a best practice if posdible

    one should do it in PQ. Although both approaches are stable.