从 date/time table 计算 "age"
Calculate "age" from date/time table
我有一个格式为“13.10.2020 06:08:43”的 Date/Time 列,我需要从当前时间计算“年龄”。输出应该类似于给定的示例:“4h”,因为它的 10:08 atm - Date/Time 列是 4 小时前。我需要用 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.
'.
只需使用以下代码创建一个新的自定义列-
= Text.From(
(Duration.Days(DateTime.LocalNow()-[date]) * 24) +
Duration.Hours(DateTime.LocalNow()-[date])
) & "h"
这是示例输出-
我有一个格式为“13.10.2020 06:08:43”的 Date/Time 列,我需要从当前时间计算“年龄”。输出应该类似于给定的示例:“4h”,因为它的 10:08 atm - Date/Time 列是 4 小时前。我需要用 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.
'.
只需使用以下代码创建一个新的自定义列-
= Text.From(
(Duration.Days(DateTime.LocalNow()-[date]) * 24) +
Duration.Hours(DateTime.LocalNow()-[date])
) & "h"
这是示例输出-