Forum Discussion
Help comparing dates between tables
rdops If I understand you correctly, you've calculated the hours difference between two tables based on name only. You need to base this on Name AND Date (and type too I think?)
So my Power Query Editor solution (similar to Greg_Deckler 's solution but using Power Query instead of DAX):
- Click on Transform Data to open the Power Query Editor.
- Select Sample A and use the Shift key to select all four columns. Click Add Column tab in the ribbon and choose Merge.
- Repeate Step 2 with Sample B.
- In the Home tab in the ribbon, select the down arrow near Merge Queries and choose Merge Queries as New.
- Select the Sample A and Sample B for the tables (order does matter, the top table should be the one that has all the results and the bottom table should be the one that only has some of the results)
- Click on the 'Merged' column in both.
- Choose Left Anti as the Join Kind. This will take the top table (Sample A) and remove any rows from it that are also in Sample B, but keep everything else. (see this post for an example: https://www.poweredsolutions.co/2019/01/10/merge-operations-in-power-bi-power-query-part-3-left-anti-join/)
- Click OK.
- You'll be left with a table that contains a funny looking column full of 'table'. You can delete that column and load this to the report.
For my DimDate solution:
- Create a DimDate table. Here's one option for how to do that: https://www.mattmasson.com/2014/02/creating-a-date-dimension-with-a-power-query-script/
- Do you already have an Employee table and a Leave Type table? If not, you should now that you are working with comparing two source tables. You can do this in Excel by just creating a simple table that has each employee listed exactly once, and another table that has each leave type listed exactly once.
- Load all 5 tables into Power BI (DimDate, SourceA, SourceB, Employee, LeaveType)
- Check the relationships:
- Relate DimDate to SourceA using [Date] column.
- Relate Employee to SourceA using [Name]
- Relate LeaveType to SourceA using [LeaveType]
- Relate DimDate to SourceB using [Date] column.
- Relate Employee to SourceB using [Name]
- Relate LeaveType to SourceB using [LeaveType]
- All relationships should be 1 to many.
- Create a few MEASURES:
- SouceATotalHours=SUM(SourceA[Hours])
- SourceBTotalHours=SUM(SourceB[Hours])
- DifferenceHours=[SourceATotalHours]-[SourceBTotalHours]
- Last step is to get the visualization right, because MEASURES change value depending on the context of the visualization they're place in. So:
- Put Employee[Name], LeaveType[Type], DimDate[Date] into Rows for a Matrix visualization.
- Put DifferenceHours in the values for that same matrix.
- You should only see rows that are not equal to 0. Note if the number hours is negative: this method will also display rows that are only in SourceB but not in SourceA if that somehow were to happen (though I believe you said that's not possible).
Has this post solved your problem? Please mark it as a solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos.
I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query.
Thank you so much for that, Allison.
Really appreciate you taking the time to being so detailed.
I tried the merge, but still cant make it work. Spent the entire morning trying 😞
I looked at DimDate solution and is way too advanced for me to even attempt.
I will stick to my basic way of visualising it (I just have two separate tables (graphs) one for each system with a single slicer, by name) and will look for what is missing. This is super frustrating for me but one has to acknowledge its limitations. I dont think i will ever be a power bi Advanced user, that's the sad truth (pitty because I love the tool).
Thank you so much and I iam sure your post will help many other people with the same query.
Cheers