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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
gigotomo
Frequent Visitor

Date Increment based on Start Date and End Date

Hello All,

 

I have the following Table 

 

EventStart DateEnd Date
A1/3/2023 15:351/4/2023 17:15
B1/6/2023 13:10

1/8/2023 16:35

 

I need to transform this in powerquery,  by incrementing the records if the difference between dates > 24 hrs.

 

Such that this table would look like this

 

EventStart DateEnd Date
A1/3/2023 15:351/3/2023 23:59
A1/4/2023 0:001/4/2023 17:15
B1/6/2023 13:101/6/2023 23:59
B1/7/2023 0:001/7/2023 23:59
B1/8/2023 0:001/8/2023 16:35
1 ACCEPTED SOLUTION
v-xinruzhu-msft
Community Support
Community Support

Hi @gigotomo 

You can refer to the following example

Put the code to Advanced Editor in power query

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTLUN9Y3MjAyVjA0tTI2BQuYQAXMrQxNlWJ1opWcwMJmUGFjK0MDsIAFVMAMpDE2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Event = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Event", type text}, {"Start Date", type datetime}, {"End Date", type datetime}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Custom", each Duration.Days([End Date]-[Start Date])),
    #"Added Custom4" = Table.AddColumn(#"Added Custom1", "Custom.3", each List.Dates(Date.From([Start Date]),[Custom]+1, #duration(1, 0, 0, 0))),
    #"Expanded Custom.3" = Table.ExpandListColumn(#"Added Custom4", "Custom.3"),
    #"Added Custom" = Table.AddColumn(#"Expanded Custom.3", "Custom.1", each if [Custom.3] >= Date.From([Start Date]) and [Custom.3]<Date.From([End Date]) then [Custom.3] & #time(23,59,0) else [Custom.3]&DateTime.Time([End Date])),
    #"Added Custom2" = Table.AddColumn(#"Added Custom", "Custom.2", each if [Custom.3]=Date.From([Start Date]) then [Start Date] else if [Custom.3]=Date.From([End Date]) then Date.From([End Date]) & #time(0,0,0) else [Custom.3] & #time(0,0,0)),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom2",{"Event", "Start Date", "End Date", "Custom", "Custom.3", "Custom.2", "Custom.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Start Date", "End Date", "Custom", "Custom.3"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom.2", "Start Date"}, {"Custom.1", "End Date"}})
in
    #"Renamed Columns"

Output

vxinruzhumsft_0-1675669170757.png

Best Regards!

Yolo Zhu

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

3 REPLIES 3
v-xinruzhu-msft
Community Support
Community Support

Hi @gigotomo 

You can refer to the following example

Put the code to Advanced Editor in power query

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTLUN9Y3MjAyVjA0tTI2BQuYQAXMrQxNlWJ1opWcwMJmUGFjK0MDsIAFVMAMpDE2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Event = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Event", type text}, {"Start Date", type datetime}, {"End Date", type datetime}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Custom", each Duration.Days([End Date]-[Start Date])),
    #"Added Custom4" = Table.AddColumn(#"Added Custom1", "Custom.3", each List.Dates(Date.From([Start Date]),[Custom]+1, #duration(1, 0, 0, 0))),
    #"Expanded Custom.3" = Table.ExpandListColumn(#"Added Custom4", "Custom.3"),
    #"Added Custom" = Table.AddColumn(#"Expanded Custom.3", "Custom.1", each if [Custom.3] >= Date.From([Start Date]) and [Custom.3]<Date.From([End Date]) then [Custom.3] & #time(23,59,0) else [Custom.3]&DateTime.Time([End Date])),
    #"Added Custom2" = Table.AddColumn(#"Added Custom", "Custom.2", each if [Custom.3]=Date.From([Start Date]) then [Start Date] else if [Custom.3]=Date.From([End Date]) then Date.From([End Date]) & #time(0,0,0) else [Custom.3] & #time(0,0,0)),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom2",{"Event", "Start Date", "End Date", "Custom", "Custom.3", "Custom.2", "Custom.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Start Date", "End Date", "Custom", "Custom.3"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom.2", "Start Date"}, {"Custom.1", "End Date"}})
in
    #"Renamed Columns"

Output

vxinruzhumsft_0-1675669170757.png

Best Regards!

Yolo Zhu

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

This worked perfectly... Thanks for the solution

ppm1
Solution Sage
Solution Sage

Please see this video for an approach to handle this scenario.

(3) Simple Model, Simple DAX - Line Time Example - YouTube

 

This one may help too.

Calculate and Format Durations in DAX – Hoosier BI

 

Pat

 

Microsoft Employee

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.