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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Serdet
Post Patron
Post Patron

Calculate hour difference per day

Hi,

 

I have a dataset similar to the below.

 

DateDate  - Time
01/01/202301/01/2023 09:00:00
01/01/202301/01/2023 09:30:00
01/01/202301/01/2023 10:30:00
03/01/202303/01/2023 11:00:00
03/01/202303/01/2023 14:30:00

 

I need to calculcate the hours difference between the first and last time but only for each day. 

Looking at the above example the time difference will be 5 as that is the difference between first and last time of each day. This calculation should not span across multiple days.

 

Your help would be much appreciated.

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyNjJR0kjoKBpZWBARApxergVWRMUJGhAbIiY2RFxghFhsjW4VJkAjMpFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Date  - Time" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Date  - Time", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Date"}, {{"Min Time", each List.Min([#"Date  - Time"]), type nullable datetime}, {"Max Time", each List.Max([#"Date  - Time"]), type nullable datetime}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Difference", each [Max Time]-[Min Time],type duration),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Date", "Difference"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

 

 

 

View solution in original post

1 REPLY 1
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyNjJR0kjoKBpZWBARApxergVWRMUJGhAbIiY2RFxghFhsjW4VJkAjMpFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Date  - Time" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Date  - Time", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Date"}, {{"Min Time", each List.Min([#"Date  - Time"]), type nullable datetime}, {"Max Time", each List.Max([#"Date  - Time"]), type nullable datetime}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Difference", each [Max Time]-[Min Time],type duration),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Date", "Difference"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

 

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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