Forum Discussion

POSPOS's avatar
POSPOS
Post Partisan
1 year ago

Append two tables using DAX

Hi All,

I have two tables, I want to append data from table 1 and table 2 using DAX, Please note this cannot be done in Power Query as there are other logics applies using DAX and row level security and hence we want to do this using DAX.

Note : This is sample data, actual data has many ID's (Col 1). Below table is sample for one ID.

Table 1: 

IDSIDHier1Hier2
UN189600000005303251953032519
UN189600000015303251953054608
UN189601050005303251953054609
UN189601030005303251953054612
UN189602000005303251953054613
UN189601000005303251953054614
UN189601070005303251953054615
UN189601080005303251953054616
UN189601013005303251953054617
UN189601010005303251953054618
UN189601011005303251953054619
UN189601060005303251953054620

Table 2:

This table may have more values when compared to Table 1. Requirement is to append thes together with Table 1. The ones in bold has to be added in the final table

Hier1Hier2
5303251953032519
5303251953054608
5303251953054609
5303251953054612
5303251953054613
5303251953054614
5303251953054615
5303251953054616
5303251953054617
5303251953054618
5303251953054619
5303251953054620
5303251953054621
5303251953054622
5303251953054623
5303251953054624

Final Output:

ID col has to populate the same ID value for the additional rows.

IDSIDHier1Hier2
UN189600000005303251953032519
UN189600000015303251953054608
UN189601000005303251953054614
UN189601010005303251953054618
UN189601011005303251953054619
UN189601013005303251953054617
UN189601030005303251953054612
UN189601050005303251953054609
UN189601060005303251953054620
UN189601070005303251953054615
UN189601080005303251953054616
UN189602000005303251953054613
UN1 5303251953054621
UN1 5303251953054622
UN1 5303251953054623
UN1 5303251953054624

 

Can somone please suggest on how this can be achieved?

Thank you.

 

22 Replies

    • POSPOS's avatar
      POSPOS
      Post Partisan

      djurecic  - Union will not work in my case as both the tables are not having same columns

  • Hi POSPOS ,

    Can you summarize one of the tables so it does have the same fields? 

     

    NewTableName = SUMMARIZE(ExistingTableName, ExistingTableName[column1], ExistingTableName[column2])

    • POSPOS's avatar
      POSPOS
      Post Partisan

      Hi djurecic  - 
      Based on the syntax provided, I tried with "Table 2" in my case. It is still having two columns and is not matching with the columns in "Table1".
      Can you please provide steps matching my scenario. That will be helpful and much appreciated.

      Thank you.

  • This should get you there

     

    Table 3 = 
    var u = maxx('Table 1',[ID])
    return SELECTCOLUMNS(NATURALLEFTOUTERJOIN('Table 2','Table 1'),"ID",u,"SID",'Table 1'[SID],"Hier1",'Table 2'[Hier1],"Hier2",'Table 2'[Hier2])
    • POSPOS's avatar
      POSPOS
      Post Partisan

      Hi lbendlin  - Thank you for your response. I tried to implement this in the actual report and I get the below issue:
      Can you pls advise on this?

       

      FC_Org_AllOrgLevel1 = 
      VAR U = maxx('FC_Org (Based on user security)',[User Name])
      RETURN SELECTCOLUMNS(NATURALLEFTOUTERJOIN('Sec_Budget_Department','FC_Org (Based on user security)'),"User Name",U,"Funds Center",'FC_Org (Based on user security)'[Funds Center],"Org Unit Level 1",'FC_Org (Based on user security)'[Org Unit Level 1],"Org Unit",'FC_Org (Based on user security)'[Organizational Unit])

       

    • POSPOS's avatar
      POSPOS
      Post Partisan

      lbendlin  -  The maxx condition in the DAX is bringing in the maximum value from the list. The sample provided has only one ID Value, in this case maxx is working fine, in the actual dataset, there are multiple ID's. in this case, the ID value is not coming up as expected.

      Can you please suggest if there is an alternate for this?

      var u = maxx('Table 1',[ID])

       

      • lbendlin's avatar
        lbendlin
        Super User

        Please provide sample data that fully covers your issue.
        Please show the expected outcome based on the sample data you provided.

  • Relay9's avatar
    Relay9
    Frequent Visitor

    To solve the issue described (appending two tables using DAX in Power BI), here’s how you can approach it:

     

    Problem Description

    1. Table 1: Contains existing rows with ID, SID, Hier1, and Hier2.

    2. Table 2: Contains new rows that need to be appended to Table 1.

    3. The ID column in the resulting table should maintain the same value (UN1) for all appended rows as per the existing structure in Table 1.

     

    Solution Using DAX

     

    You cannot directly append tables using DAX as DAX is for creating calculated columns and measures. However, you can achieve this by creating a calculated table that appends the two tables together.

     

    Steps:

    1. Create a Calculated Table:

    Use the UNION function in DAX to combine Table1 and Table2.

     

    FinalTable = 

    UNION(

        Table1,

        ADDCOLUMNS(

            Table2,

            "ID", "UN1", -- Assign the fixed ID for rows from Table2

            "SID", BLANK() -- Optional: Handle any missing columns

        )

    )

     

     

    2. Explanation of the Formula:

    UNION: Combines rows from Table1 and Table2.

    ADDCOLUMNS: Adds the ID column with a fixed value of UN1 for rows in Table2 if it’s not already there.

    3. Load Resulting Table:

    This will generate a combined table (FinalTable) in your Power BI model, which includes all rows from both tables, with the required ID formatting.

    4. Custom Adjustments:

    If Table2 already has the ID column and you want to overwrite it, use RENAMECOLUMNS before applying UNION.

    Ensure column names and data types in Table1 and Table2 align.

     

    Note

     

    If row-level security or complex filtering is involved, consider combining this approach with measures or calculated columns for dynamic logic.

     

    Let me know if you need further clarifications!

    • POSPOS's avatar
      POSPOS
      Post Partisan

      Relay9  - I tried to use the code provided and I get an error as below :