Power Query / Excel:DateTime.ToText()

时间:2016-04-21 12:57:16

标签: excel business-intelligence powerquery

在Excel 2016中 - 查询编辑器 - 高级编辑器。

这是我的代码:

let
    SettingsSheet = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"TimeRange" = Table.TransformColumnTypes(SettingsSheet,{{"From", type datetime}, {"To", type datetime}}),
    From = #"TimeRange"[From],
    To = #"TimeRange"[To],
    DateFormatString = "yyyy-MM-dd-THH:mm:ssZ",
    FormattedFrom = DateTime.ToText(#"TimeRange"[From], DateFormatString ),
    FormattedTo = DateTime.ToText(To, DateFormatString ),
    ...
    (Further in the code, I will need to concart formatted datetimes in a URL string.)

如果我以

结束
...
in
   #"TimeRange"

我按预期获得了一个包含DateTimes的表。

如果我完成了     ...     #“testTable”= {From,To,FormattedFrom,FormattedFrom}     在        # “TestTable的”

我得到一张桌子 1名单 2名单 3错误 4错误

我预料到了 3和4的日期格式为DateFormatString建议。

我也试过没有DateFormatString,如

FormattedFrom = DateTime.ToText(#"TimeRange"[From]),

DateFormatString = "YYYYMMDD",,如https://msdn.microsoft.com/en-us/library/mt253497.aspx上的示例所示 但我得到了同样的结果。

我该如何格式化日期?

  

编辑:错误说:Expression.Error:我们无法转换类型的值   列出以键入DateTime。细节:       值=名单       类型=类型

1 个答案:

答案 0 :(得分:2)

DateTime.FromText要求将单元格作为第一个参数而不是列。

这个添加的自定义列将创建一个文本字符串,用于将2个日期与所需格式连接,并将“ - ”连接为分隔符:

   String = Table.AddColumn(#"TimeRange", "String", each DateTime.ToText([From], DateFormatString)&"-"&DateTime.ToText([To], DateFormatString))