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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
micjensen
Frequent Visitor

Consecutively dates. Merge multiple lines to one based on multiple criterias

 
Hi Community
 

i have an issue that i can't solve myself.

My data table looks like this. I have a lot of rows that determines the work period for each employee. There are scenarios where an employee has several rows, but the dates are consecutive - so in these cases i want to tell my data model that these should be combined into 1 row, that indicates the start and end date for that trip.

Data Table:

micjensen_0-1667996806156.png

 

 

Illustration of my issue / Request:

micjensen_1-1667996805681.png

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @micjensen ,

 

According to your description, my suggestion is to implement it in power query.

(1)This is my test data.

vtangjiemsft_0-1668072660421.png

(2)Add the following code in the advanced editor.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ndHNCsIwDADgVxk9D9pm/T2qLyB4HDsUV7zMDjp9fzPcTw9Oth4KCSEfTVLXxBptaSUqZUhJzj68ilNoo3cDpjcXWh8fLraYcHySgqbAADA2VHzDpjyoGCpXhNtMxVI7K0uYgXC2KtxkMFNr8hlg2QzwhJHZQ4FKmJyhRgY3w8XsjGtKIaYEQkJqrF36+PRd0fUBk2vXD3cX3xOilraSaMohg8C26vel9wp/rnyA2LrwXmJzn80H", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Payroll No." = _t, #"First Name" = _t, Surname = _t, #"Sum of Count" = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type2" = Table.TransformColumnTypes(Source,{{"Sum of Count", Int64.Type}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type2",{{"Start Date", type date}, {"End Date", type date}}),
    test = Table.TransformColumnTypes(#"Changed Type1",{{"Payroll No.", type text}, {"First Name", type text}, {"Surname", type text}, {"Sum of Count", Int64.Type}, {"Start Date", type date}, {"End Date", type date}}),
    Custom1 = Table.Group(test,{"Payroll No.","First Name","Surname"},{"test",(x)=>List.Sort(x[Start Date] & List.Transform(x[End Date],(y)=>Date.AddDays(y,1)))            }),
    #"Expanded test" = Table.ExpandListColumn(Custom1, "test"),
    #"Added Custom" = Table.AddColumn(#"Expanded test", "Count", (x)=>Table.RowCount(Table.SelectRows(#"Expanded test",(y)=> y[test]=x[test] and y[#"Payroll No."]=x[#"Payroll No."]          ))  ),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Count] = 1)),
    aa = Table.RemoveColumns(#"Filtered Rows",{"Count"}),
    Custom2 = Table.AddColumn(Table.Group(   aa,{"Payroll No.","First Name","Surname"},{{"test",(x)=> List.Alternate(x[test],1,1,1)   } ,{"test2",(x)=>List.Transform(List.Alternate(x[test],1,1),(y)=>Date.AddDays(y,-1))          }}     ) ,"tt",(x)=> Table.FromColumns({x[test],x[test2]})       ),
    #"Removed Columns" = Table.RemoveColumns(Custom2,{"test", "test2"}),
    #"Expanded tt" = Table.ExpandTableColumn(#"Removed Columns", "tt", {"Column1", "Column2"}, {"Start Date", "End Date"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded tt",{{"Start Date", type date}, {"End Date", type date}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Custom", (x)=>List.Sum(Table.SelectRows(#"Changed Type1",(y)=>y[Start Date]>=x[Start Date] and y[End Date]<=x[End Date] and y[#"Payroll No."]=x[#"Payroll No."]   )[Sum of Count] )            )
in
    #"Added Custom1"

(3) Then the result is as follows.

vtangjiemsft_1-1668072660426.png

 

Best Regards,

Neeko Tang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

 

View solution in original post

2 REPLIES 2
micjensen
Frequent Visitor

Hi Community


i have an issue that i can't solve myself.

My data table looks like this. I have a lot of rows that determines the work period for each employee. There are scenarios where an employee has several rows, but the dates are consecutive - so in these cases i want to tell my data model that these should be combined into 1 row, that indicates the start and end date for that trip.

Data Table:

micjensen_0-1667996587827.png

 

Illustration of my issue / Request:

micjensen_1-1667996639496.png

 

 





Anonymous
Not applicable

Hi @micjensen ,

 

According to your description, my suggestion is to implement it in power query.

(1)This is my test data.

vtangjiemsft_0-1668072660421.png

(2)Add the following code in the advanced editor.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ndHNCsIwDADgVxk9D9pm/T2qLyB4HDsUV7zMDjp9fzPcTw9Oth4KCSEfTVLXxBptaSUqZUhJzj68ilNoo3cDpjcXWh8fLraYcHySgqbAADA2VHzDpjyoGCpXhNtMxVI7K0uYgXC2KtxkMFNr8hlg2QzwhJHZQ4FKmJyhRgY3w8XsjGtKIaYEQkJqrF36+PRd0fUBk2vXD3cX3xOilraSaMohg8C26vel9wp/rnyA2LrwXmJzn80H", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Payroll No." = _t, #"First Name" = _t, Surname = _t, #"Sum of Count" = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type2" = Table.TransformColumnTypes(Source,{{"Sum of Count", Int64.Type}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type2",{{"Start Date", type date}, {"End Date", type date}}),
    test = Table.TransformColumnTypes(#"Changed Type1",{{"Payroll No.", type text}, {"First Name", type text}, {"Surname", type text}, {"Sum of Count", Int64.Type}, {"Start Date", type date}, {"End Date", type date}}),
    Custom1 = Table.Group(test,{"Payroll No.","First Name","Surname"},{"test",(x)=>List.Sort(x[Start Date] & List.Transform(x[End Date],(y)=>Date.AddDays(y,1)))            }),
    #"Expanded test" = Table.ExpandListColumn(Custom1, "test"),
    #"Added Custom" = Table.AddColumn(#"Expanded test", "Count", (x)=>Table.RowCount(Table.SelectRows(#"Expanded test",(y)=> y[test]=x[test] and y[#"Payroll No."]=x[#"Payroll No."]          ))  ),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Count] = 1)),
    aa = Table.RemoveColumns(#"Filtered Rows",{"Count"}),
    Custom2 = Table.AddColumn(Table.Group(   aa,{"Payroll No.","First Name","Surname"},{{"test",(x)=> List.Alternate(x[test],1,1,1)   } ,{"test2",(x)=>List.Transform(List.Alternate(x[test],1,1),(y)=>Date.AddDays(y,-1))          }}     ) ,"tt",(x)=> Table.FromColumns({x[test],x[test2]})       ),
    #"Removed Columns" = Table.RemoveColumns(Custom2,{"test", "test2"}),
    #"Expanded tt" = Table.ExpandTableColumn(#"Removed Columns", "tt", {"Column1", "Column2"}, {"Start Date", "End Date"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded tt",{{"Start Date", type date}, {"End Date", type date}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Custom", (x)=>List.Sum(Table.SelectRows(#"Changed Type1",(y)=>y[Start Date]>=x[Start Date] and y[End Date]<=x[End Date] and y[#"Payroll No."]=x[#"Payroll No."]   )[Sum of Count] )            )
in
    #"Added Custom1"

(3) Then the result is as follows.

vtangjiemsft_1-1668072660426.png

 

Best Regards,

Neeko Tang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.