电源 Query/Excel:DateTime.ToText() 出错

Power Query/Excel: Error with DateTime.ToText()

2016 年 Excel - 查询编辑器 - 高级编辑器。

这是我的代码:

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"

我得到了 table 与 DateTimes 的预期结果。

如果我完成 ... #"testTable" = { 从,到,FormattedFrom,FormattedFrom} 在 #"testTable"

我得到 table 显示 1 清单 2 清单 3 错误 4 错误

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

我也尝试过不使用 DateFormatString,如

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

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

我应该如何格式化日期?

Edit: Error says: Expression.Error: We cannot convert a value of type List to type DateTime. Details: Value=List Type=Type

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

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

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