从日期/时间表计算“年龄”

时间:2020-10-13 07:10:39

标签: visual-studio m

我有一个日期/时间列,格式为“ 13.10.2020 06:08:43”,我需要从当前时间计算“年龄”。输出应类似于给定示例:“ 4h”,因为其10:08 atm-日期/时间列为4h之前。我需要使用M语言才能在Visual Studio 2019中使用它。

我用过:

let

        Source = Sql.Database("xxxx", "xxxxx", [Query="SELECT#(lf)#(tab)storeid as 'Shop'#(lf)#(tab), concat('SCO', posnr) as POS#(lf)#(tab),  concat(datediff(hour,[LastTransactionDate]#(lf)#(tab), CURRENT_TIMESTAMP),' h') as 'Age'#(lf)#(tab), POSIPaddress as 'IP'#(lf)#(tab),[PosNr]#(lf)    ,[AdapterPosOrGroupId]#(lf)    ,[UpdatedOn]#(lf)    ,[LastTransactionDate]#(lf)    ,[LastStartDate]#(lf)    ,[Open]#(lf)    ,[Locked]#(lf)    ,[CashDisabled]#(lf)    ,[IsCashEnabled]#(lf)    ,[CashDevicesTraficLight]#(lf)    ,[POSIPAddress]#(lf)    ,[ScoPosServiceVersion]#(lf)    ,[WinScoVersion]#(lf)    ,[StoreType]#(lf)FROM [LaneEventDatabase ].[dbo].[POS.LaneCurrentState]"]),
        #"Added Conditional Column" = Table.AddColumn(Source, "Custom", each if [CashDevicesTraficLight] = "green" then 1 else if [CashDevicesTraficLight] = "yellow" then 2 else if [CashDevicesTraficLight] = "red" then 3 else 4),
        #"Changed Type" = Table.TransformColumnTypes(#"Added Conditional Column",{{"Custom", type number}})
    in
        #"Changed Type"

效果很好,但是Visual Studio不喜欢处理源的方式,而在部署时却给我错误:

Failed to save modifications to the server. Error returned: 'An M partition uses a data function which results in access to a data source different from those defined in the model.

'.

1 个答案:

答案 0 :(得分:1)

只需使用以下代码创建一个新的自定义列-

= Text.From(
    (Duration.Days(DateTime.LocalNow()-[date]) * 24) +
    Duration.Hours(DateTime.LocalNow()-[date])
) & "h"

这是示例输出-

enter image description here