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

元气小坏坏 提交于 2019-12-07 15:02:06

问题


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))



来源:https://stackoverflow.com/questions/28531701/how-can-i-perform-the-equivalent-of-addhours-to-a-datetime-in-power-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!