How can I perform the equivalent of AddHours to a DateTime in Power Query?

Craig picture Craig · Feb 15, 2015 · Viewed 10.4k times · Source

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]

Answer

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.