Forum Discussion

shoebhakeem123's avatar
shoebhakeem123
Frequent Visitor
7 years ago
Solved

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
  • Anonymous's avatar
    Anonymous
    7 years ago

    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. 

6 Replies

    • shoebhakeem123's avatar
      shoebhakeem123
      Frequent Visitor

      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 errorAuto AggregationModel

       

  • Anonymous's avatar
    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.