How to structure time and date in PowerQuery

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
I want to calculate time differences in PowerPivot/PowerQuery/Power BI but I can't get it to work between days.

In excel I could use 1904 dates and it somewhat works.

How should I structure date & time data to correctly calculate time differences between dates and hours with dates and hours.

Arrival timeExit timeTime
2020-03-24 21:15:592020-03-25 04:14:00##### is displayed instead of actual time difference
2020-03-23 07:00:002020-03-23 09:00:0002:00:00 hours

I have tried with date, date and time but it doesn't work.

So I would be glad if someone could show me how to calculate time differences correctly using DAX or PowerQuery?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi, I believe it is doable with the UI in PQ.
Select both columns (Exit before Arrival while holding CTRL key) and in the Add Column Ribbon select Date and from the dropdown Subtract Days or for time difference use Time drop down instead.
Make sure your columns are defined as date/datetime data types.
1585158251193.png


1585158353861.png
 
Upvote 0
Hi,

thank you for your reply.

I found the subtract and duration functions in the time, duration menu.

It says "-hours" for values that are between days and I am not sure how to read the - sign? ( I also think that the time after the minus sign is wrong)

I want to be able to subtract the dates and get the correct number of hours between two dates with different times in them.

Should I try to divide the date and times into smaller pieces or attack this problem some other way?
 
Upvote 0
is that what you want?

Arrival timeExit timeSubtractionTotal Hours
24/03/2020 21:1525/03/2020 04:140.06:58:016.966944444
23/03/2020 07:0023/03/2020 09:000.02:00:002
23/02/2020 07:0025/03/2020 04:1430.21:14:00741.2333333

Subtraction format is: d.hh:mm:ss
Rich (BB code):
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Arrival time", type datetime}, {"Exit time", type datetime}}),
    Subtract = Table.AddColumn(Type, "Subtraction", each [Exit time] - [Arrival time], type duration),
    TotalHours = Table.AddColumn(Subtract, "Total Hours", each Duration.TotalHours([Subtraction]), type number)
in
    TotalHours
you can round TotalHours as you wish
or choose one as the end result
totalh.png
 
Last edited:
Upvote 0
Hi,
I believe you selected your columns in the wrong order if you have a "-" (minus) sign in your result.

See what sandy posted. I wanted to suggest the same :-)
 
Upvote 0
How about a custom column with this simple formula?

= [Exit Time]-[Arrival Time]
 
Upvote 0
Hi,

thank you for your replies.

Sandy666's method does what I want to do.
 
Upvote 0
:cool:
you can do that with a custom column
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Hours = Table.SelectColumns(Table.TransformColumns(Table.TransformColumnTypes(Table.AddColumn(Source, "Hours", each ([Exit time]-[Arrival time])*24),{{"Hours", type number}}),{{"Hours", each Number.Round(_, 2), type number}}),{"Hours"})
in
    Hours
Arrival timeExit timeHours
24/03/2020 21:1525/03/2020 04:146.97
23/03/2020 07:0023/03/2020 09:002
23/02/2020 07:0025/03/2020 04:14741.23
 
Last edited:
Upvote 0
or just Custom Column with:
Number.Round(Duration.TotalHours([Exit time]-[Arrival time]),2)
 
Upvote 0

Forum statistics

Threads
1,223,796
Messages
6,174,655
Members
452,575
Latest member
Fstick546

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top