0
votes

I try to join 2 tables using Power Query/PowerBI: Absence and dimDate to create a result table below:

Absence table

+------------+--------------+--------------+-----------+-----------+
| EmployeeId | EmployeeName | AbsenceType  | StartDate |  EndDate  |
+------------+--------------+--------------+-----------+-----------+
|          1 | A            | Annual Leave | 2/01/2017 | 5/01/2017 |
|          2 | B            | Sick Leave   | 4/01/2017 | 6/01/2017 |
+------------+--------------+--------------+-----------+-----------+

dimDate table

+------------+
|  FullDate  |
+------------+
| 1/01/2017  |
| 2/01/2017  |
| 3/01/2017  |
| 4/01/2017  |
| 5/01/2017  |
| 6/01/2017  |
| 7/01/2017  |
| 8/01/2017  |
| 9/01/2017  |
| 10/01/2017 |
+------------+

Result

+------------+--------------+--------------+-----------+
| EmployeeId | EmployeeName | AbsenceType  |   Date    |
+------------+--------------+--------------+-----------+
|          1 | A            | Annual Leave | 2/01/2017 |
|          1 | A            | Annual Leave | 3/01/2017 |
|          1 | A            | Annual Leave | 4/01/2017 |
|          1 | A            | Annual Leave | 5/01/2017 |
|          2 | B            | Sick Leave   | 4/01/2017 |
|          2 | B            | Sick Leave   | 5/01/2017 |
|          2 | B            | Sick Leave   | 6/01/2017 |
+------------+--------------+--------------+-----------+

I usually use SQL to create this result, however I don't know how to efficiently do it in PowerQuery.

SELECT A.EmployeeId
        ,A.EmployeeName
        ,A.AbsenceType
        ,D.FullDate
FROM Absence AS A
INNER JOIN dimDate AS D ON (
                D.FullDate >= A.StartDate
                AND D.FullDate <= A.EndDate
                )

Note: I have tried the Full Join between 2 tables Absence and dimDate, then filter true value if dimDate.FullDate >= StartDate and dimDate.FullDate <= EndDate. However this approach seems to be ineffective with large table, and it creates redundant records before filtering so it's quite slow.

Please give me some advice.

1

1 Answers

3
votes

No need to merge. You can create a column with embedded lists of all dates between StartDate and EndDate. And then expand that column.

let
    Source = Table1,
    #"Added Custom" = Table.AddColumn(Source, "Date", each List.Dates([StartDate],1+Duration.Days([EndDate]-[StartDate]),#duration(1,0,0,0))),
    #"Expanded Date" = Table.ExpandListColumn(#"Added Custom", "Date"),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Date",{{"Date", type date}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"StartDate", "EndDate"})
in
   #"Removed Columns"