Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
shoebhakeem123
Frequent Visitor

DAX compare data from different tables

I am trying to find differences between two tables carrying the same data but from different sources. I need to find differences. Can anyone please support!

 

Table 1:
Employee_Code
Salary_Month
Net_Salary

 

Table 2:
Employee_Code
Salary_Month
Net_Salary


Custom Calendar:
Date {from 1st Jan 2018 till 31st Jan 2018}
Month

 

Direct Relationship:
Table 1[Employee_code], Table 2[Employee_code]

Table 1[Salary_month], Calendar[Month]


Indirect Relationship:
Table 2[Salary_month], Calendar[Month]


I am trying to find the difference was in which month, unfortunately, both the below DAX does not calculate.

 

DAX Formula used:
Difference1 = CALCULATE(SUM('Table 1'[Net_Salary]))- CALCULATE(SUM('Table 2'[Net_Salary]))

Difference2 = CALCULATE(SUM('Table 1'[Net_Salary]))- CALCULATE(SUM('Table 2'[Net_Salary]), USERELATIONSHIP('Table2'[Salary_Month], 'Calendar'[Month])

 

 

Required output:

Employee_CodeMonthTable1[Net_salary] Table2[Net_salary] Difference
10011500050000
100290008900100
1003280009500-1500
2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

While this can be solved in DAX, the cleaner way to do it is to transform your data so that you have a snowflake schema with two fact tables. What you want to do is create a tables with distinct customer code then link this to both tables, as you have done with the calendar tables. You will also need to delete the relationship between the two fact tables. 

 

This is by far the best way to solve this, it is possible with measures or with a different model. 

View solution in original post

v-cherch-msft
Employee
Employee

Hi @shoebhakeem123

 

You may change the relationships and then use the measure Difference1. Attached the sample file for your reference.

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-cherch-msft
Employee
Employee

Hi @shoebhakeem123

 

You may change the relationships and then use the measure Difference1. Attached the sample file for your reference.

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Team,

 

Thank you for the post. This works when we have distinct employee codes in both tables.

 

In my case, I have payroll comparison of the same data coming from two different sources, for some reason.

 

To be more precise, I have 1000 plus employees each month. All employees are repeated every month except for a few termination cases and new hires.

 

When I apply your solution, the table 2 salary is getting aggregated and if I select do not summarize, POWER BI sends out a relationship error.

 

Please see the screenshots.

 

 

Relationship errorRelationship errorAuto AggregationAuto AggregationModelModel

 

Hi @shoebhakeem123

 

You may try to change the relationship to 'Many to Many'.

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I tried but no change in result.Smiley Sad

I guess this would work.. Thank you so much.

Anonymous
Not applicable

While this can be solved in DAX, the cleaner way to do it is to transform your data so that you have a snowflake schema with two fact tables. What you want to do is create a tables with distinct customer code then link this to both tables, as you have done with the calendar tables. You will also need to delete the relationship between the two fact tables. 

 

This is by far the best way to solve this, it is possible with measures or with a different model. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.