Forum Discussion
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:
| ID | SID | Hier1 | Hier2 |
| UN1 | 8960000000 | 53032519 | 53032519 |
| UN1 | 8960000001 | 53032519 | 53054608 |
| UN1 | 8960105000 | 53032519 | 53054609 |
| UN1 | 8960103000 | 53032519 | 53054612 |
| UN1 | 8960200000 | 53032519 | 53054613 |
| UN1 | 8960100000 | 53032519 | 53054614 |
| UN1 | 8960107000 | 53032519 | 53054615 |
| UN1 | 8960108000 | 53032519 | 53054616 |
| UN1 | 8960101300 | 53032519 | 53054617 |
| UN1 | 8960101000 | 53032519 | 53054618 |
| UN1 | 8960101100 | 53032519 | 53054619 |
| UN1 | 8960106000 | 53032519 | 53054620 |
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
| Hier1 | Hier2 |
| 53032519 | 53032519 |
| 53032519 | 53054608 |
| 53032519 | 53054609 |
| 53032519 | 53054612 |
| 53032519 | 53054613 |
| 53032519 | 53054614 |
| 53032519 | 53054615 |
| 53032519 | 53054616 |
| 53032519 | 53054617 |
| 53032519 | 53054618 |
| 53032519 | 53054619 |
| 53032519 | 53054620 |
| 53032519 | 53054621 |
| 53032519 | 53054622 |
| 53032519 | 53054623 |
| 53032519 | 53054624 |
Final Output:
ID col has to populate the same ID value for the additional rows.
| ID | SID | Hier1 | Hier2 |
| UN1 | 8960000000 | 53032519 | 53032519 |
| UN1 | 8960000001 | 53032519 | 53054608 |
| UN1 | 8960100000 | 53032519 | 53054614 |
| UN1 | 8960101000 | 53032519 | 53054618 |
| UN1 | 8960101100 | 53032519 | 53054619 |
| UN1 | 8960101300 | 53032519 | 53054617 |
| UN1 | 8960103000 | 53032519 | 53054612 |
| UN1 | 8960105000 | 53032519 | 53054609 |
| UN1 | 8960106000 | 53032519 | 53054620 |
| UN1 | 8960107000 | 53032519 | 53054615 |
| UN1 | 8960108000 | 53032519 | 53054616 |
| UN1 | 8960200000 | 53032519 | 53054613 |
| UN1 | 53032519 | 53054621 | |
| UN1 | 53032519 | 53054622 | |
| UN1 | 53032519 | 53054623 | |
| UN1 | 53032519 | 53054624 |
Can somone please suggest on how this can be achieved?
Thank you.
22 Replies
- djurecicSuper User
Hi POSPOS ,
You can create a new table using the UNION function to achieve this. Here is additional information:
https://learn.microsoft.com/en-us/dax/union-function-dax
- lbendlinSuper User
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])- POSPOSPost 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]) - POSPOSPost 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])- lbendlinSuper User
Please provide sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.
- Relay9Frequent 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!
- POSPOSPost Partisan