How can I perform the equivalent of AddHours to a

2019-05-17 03:41发布

问题:

I've seen PowerQuery expressions that can subtract days from a datetime, like DateTimeZone.UtcNow.AddDays(-1). However, I'd like to subtract hours from a DateTime, not days.

As I don't see a function like AddHours() in the Power Query specification, is there a robust way to go back a number of hours? For instance, would something like DateTimeZone.UtcNow + #duration(0, -1, 0, 0) give me a DateTime that's one hour earlier?

[Note: I'd try this myself but I can't get Power Query installed on my local box and I can't find an independent editor to try it, hence the community ask]

回答1:

Yes, adding #duration(0, -1, 0, 0) would give you a DateTime that is one hour earlier. You could also subtract #duration(0, 1, 0, 0) from the DateTime value. These methods are robust.



回答2:

yourtime + 3/24 will add three hours



回答3:

This link has a good image detailing subtraction of hours.

https://community.powerbi.com/t5/Desktop/How-to-subtract-hours-from-DateTime-data-using-DAX/m-p/137444#M59029

I used it to subtract seven hours from dates ([End] column) when it was entered as midnight. See below:

(if Time.Hour([End]) <> 0 then [End] else [End] - #duration(0,7,0,0))