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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Consolidation of sick leaves with start and end date are consecutive and overlapping

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 IDNomLibellé motifDate débutDate fin
1Person AAAMALADIE2/1/20242/3/2024
1Person AAAMALADIE6/14/20246/15/2024
1Person AAAMALADIE6/16/20246/21/2024
2Person CCCMALADIE1/10/20242/18/2024
2Person CCCMALADIE2/19/20243/20/2024
2Person CCCMALADIE3/21/20244/1/2024
3Person EEEMALADIE1/29/20242/16/2024
3Person EEEMALADIE2/17/20243/15/2024
3Person EEEMALADIE3/16/20244/12/2024
3Person EEEMALADIE4/13/20245/17/2024
3Person EEEMALADIE5/18/20246/14/2024
3Person EEEMALADIE6/15/20247/12/2024
3Person EEEMALADIE7/13/20248/16/2024

 

Expected result 

Result
EMP IDNomLibellé motifDate débutDate fin
1Person AAAMALADIE2/1/20242/3/2024
1Person AAAMALADIE6/14/20246/21/2024
2Person CCCMALADIE1/10/20244/1/2024
3Person EEEMALADIE1/29/20248/16/2024
1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

 

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

 

View solution in original post

6 REPLIES 6
AlienSx
Super User
Super User

 

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

 

Anonymous
Not applicable

Hello @AlienSx ,

 

This solution worked for me!! Thank you so much for your help 🙂 You're amazing!!!! 

Anonymous
Not applicable

Anonymous
Not applicable

I hope i'm clear 🙂 

Anonymous
Not applicable

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. 

  
PhilipTreacy
Super User
Super User

@Anonymous 

 

How are you calcuating periods?

 

Why does 1 have 2 rows but 2 and 3 only have 1 row?

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.