Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
Hi community,
I'm working on a data but im unable to find a solution to the result i have a sick day leave of start date and end date but the dates have broken the current period and have created a new row. i want to create one single row for one period removing excess rows by consilidating the rows. Can you please let me know if there's a solution on how i can do it on power query please?
here's how the source data look like
| Source Data | ||||
| EMP ID | Nom | Libellé motif | Date début | Date fin |
| 1 | Person AAA | MALADIE | 2/1/2024 | 2/3/2024 |
| 1 | Person AAA | MALADIE | 6/14/2024 | 6/15/2024 |
| 1 | Person AAA | MALADIE | 6/16/2024 | 6/21/2024 |
| 2 | Person CCC | MALADIE | 1/10/2024 | 2/18/2024 |
| 2 | Person CCC | MALADIE | 2/19/2024 | 3/20/2024 |
| 2 | Person CCC | MALADIE | 3/21/2024 | 4/1/2024 |
| 3 | Person EEE | MALADIE | 1/29/2024 | 2/16/2024 |
| 3 | Person EEE | MALADIE | 2/17/2024 | 3/15/2024 |
| 3 | Person EEE | MALADIE | 3/16/2024 | 4/12/2024 |
| 3 | Person EEE | MALADIE | 4/13/2024 | 5/17/2024 |
| 3 | Person EEE | MALADIE | 5/18/2024 | 6/14/2024 |
| 3 | Person EEE | MALADIE | 6/15/2024 | 7/12/2024 |
| 3 | Person EEE | MALADIE | 7/13/2024 | 8/16/2024 |
Expected result
| Result | ||||
| EMP ID | Nom | Libellé motif | Date début | Date fin |
| 1 | Person AAA | MALADIE | 2/1/2024 | 2/3/2024 |
| 1 | Person AAA | MALADIE | 6/14/2024 | 6/21/2024 |
| 2 | Person CCC | MALADIE | 1/10/2024 | 4/1/2024 |
| 3 | Person EEE | MALADIE | 1/29/2024 | 8/16/2024 |
Solved! Go to Solution.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
combine = Table.CombineColumns(
Source,
{"Date début", "Date fin"},
(z) => List.Generate(() => z{0}, (x) => x <= z{1}, (x) => Date.AddDays(x, 1)),
"dates"
),
xpand = Table.ExpandListColumn(combine, "dates"),
sort = Table.Sort(xpand, {"EMP ID", "dates"}),
idx = Table.AddIndexColumn(sort, "idx"),
group = Table.Group(
idx,
Table.ColumnNames(idx),
{
{"Date début", (x) => List.Min(x[dates])},
{"Date fin", (x) => List.Max(x[dates])}
},
GroupKind.Local,
(s, c) => Number.From(s[EMP ID] <> c[EMP ID] or (c[dates] - s[dates]) > #duration(c[idx] - s[idx], 0, 0, 0))
),
select_columns = Table.SelectColumns(group, Table.ColumnNames(Source))
in
select_columns
v2
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
continuous = (tbl) =>
[lst = List.Buffer(Table.ToRows(Table.Sort(tbl[[Date début], [Date fin]], "Date début"))),
gen = List.Generate(
() => [i = 0, combine = false, c = lst{0}, result = {}],
(x) => x[i] < List.Count(lst),
(x) =>
[
i = x[i] + 1,
combine = (lst{i}{0} - x[c]{1}) = #duration(1, 0, 0, 0),
c = if combine then {x[c]{0}, lst{i}{1}} else lst{i},
result = x[result] & (if combine then {} else {x[c]})
],
(x) => if x[i] = List.Count(lst) - 1 then x[result] & {x[c]} else null
),
z = #table({"Date début", "Date fin"}, List.Last(gen))][z],
group = Table.Group(Source, {"EMP ID", "Nom", "Libellé motif"}, {{"x", continuous}}),
xpand = Table.ExpandTableColumn(group, "x", {"Date début", "Date fin"})
in
xpand
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
combine = Table.CombineColumns(
Source,
{"Date début", "Date fin"},
(z) => List.Generate(() => z{0}, (x) => x <= z{1}, (x) => Date.AddDays(x, 1)),
"dates"
),
xpand = Table.ExpandListColumn(combine, "dates"),
sort = Table.Sort(xpand, {"EMP ID", "dates"}),
idx = Table.AddIndexColumn(sort, "idx"),
group = Table.Group(
idx,
Table.ColumnNames(idx),
{
{"Date début", (x) => List.Min(x[dates])},
{"Date fin", (x) => List.Max(x[dates])}
},
GroupKind.Local,
(s, c) => Number.From(s[EMP ID] <> c[EMP ID] or (c[dates] - s[dates]) > #duration(c[idx] - s[idx], 0, 0, 0))
),
select_columns = Table.SelectColumns(group, Table.ColumnNames(Source))
in
select_columns
v2
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
continuous = (tbl) =>
[lst = List.Buffer(Table.ToRows(Table.Sort(tbl[[Date début], [Date fin]], "Date début"))),
gen = List.Generate(
() => [i = 0, combine = false, c = lst{0}, result = {}],
(x) => x[i] < List.Count(lst),
(x) =>
[
i = x[i] + 1,
combine = (lst{i}{0} - x[c]{1}) = #duration(1, 0, 0, 0),
c = if combine then {x[c]{0}, lst{i}{1}} else lst{i},
result = x[result] & (if combine then {} else {x[c]})
],
(x) => if x[i] = List.Count(lst) - 1 then x[result] & {x[c]} else null
),
z = #table({"Date début", "Date fin"}, List.Last(gen))][z],
group = Table.Group(Source, {"EMP ID", "Nom", "Libellé motif"}, {{"x", continuous}}),
xpand = Table.ExpandTableColumn(group, "x", {"Date début", "Date fin"})
in
xpand
I hope i'm clear 🙂
Hello Phil,
Thank you for your question.
Employee 1 took 3 days leave on 1st until 3rd Feb so its one period for me. How ever the same employee 1 took 14 to 21 June which has been broken into 2 rows, 14th June as a start date and again 16th June as a start date. but logically it should be 14-June-2024 to 21-June2024. you can notice the same for the rest of the employees.
@Anonymous
How are you calcuating periods?
Why does 1 have 2 rows but 2 and 3 only have 1 row?
Phil
Proud to be a Super User!
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
| User | Count |
|---|---|
| 17 | |
| 9 | |
| 8 | |
| 7 | |
| 7 |