【问题标题】:Expression.Error: We cannot convert a value of type Function to type TextExpression.Error:我们无法将 Function 类型的值转换为 Text 类型
【发布时间】:2022-12-22 14:39:47
【问题描述】:

此 M(Power Query)函数给出此错误“Expression.Error:我们无法将类型 Function 的值转换为类型 Text。” 功能目标是有两个文本输入并将它们减去持续时间(如列表或 tex)

例如: 1/20/2020 下午 1:49:46、1/20/2020 下午 1:58:03、1/20/2020 下午 2:09:42

1/20/2020 下午 1:49:41、1/20/2020 下午 1:57:58、1/20/2020 下午 2:09:38

let
    Source = (txtCheckInTime as text, txtCheckOutTime as text) as text=> let
        lstCheckinTimeLst = Text.Split(txtCheckInTime, ", "),
        lstCheckOutTimeLst = Text.Split(txtCheckOutTime, ", "),
        txtCheckInEvent = each lstCheckinTimeLst,
        txtCheckOutEvent = each  lstCheckOutTimeLst,
        EventDuration = DateTime.FromText(txtCheckInEvent) - DateTime.FromText(txtCheckOutEvent) ,
        Source = Text.Combine(Duration.ToText(EventDuration),", ")
    in
        Source
in
    Source

无论如何可以指出错误吗?

【问题讨论】:

  • 以下两行是罪魁祸首:txtCheckInEvent = each lstCheckinTimeLst, txtCheckOutEvent = each lstCheckOutTimeLst,

标签: formula powerquery m


【解决方案1】:

这里有几个问题。

  1. 主要问题是因为 each 关键字是一个占位符迭代函数,而不是迭代器本身(有关详细信息,请参阅here)。
  2. DateTime.FromText() 函数需要单个元素,而不是列表或函数对象(有关详细信息,请参阅 here)。
  3. 在您的示例数据中,CheckInTime 似乎是CheckOutTime 的?

    如果我要重写你的函数,这就是它的样子:

    let fun_GetDurations = (txt_CheckInTime as text, txt_CheckOutTime as text) as text =>
      let
        lst_CheckInTime = Text.Split(txt_CheckInTime, ", "),
        lst_CheckOutTime = Text.Split(txt_CheckOutTime, ", "),
        tbl_Times = Table.FromColumns({lst_CheckInTime, lst_CheckOutTime}, {"CheckInTime", "CheckOutTime"}),
        fix_Formatting = Table.TransformColumns(tbl_Times, {{"CheckInTime", each DateTime.FromText(_, "en-US"), type nullable text}, {"CheckOutTime", each DateTime.FromText(_, "en-US"), type nullable text}}),
        add_Duration = Table.AddColumn(fix_Formatting, "Duration", each Duration.ToText([CheckOutTime] - [CheckInTime]), type text),
        get_Durations = Text.Combine(Table.Column(add_Duration, "Duration"), ", ")
      in
        get_Durations
    in fun_GetDurations
    

    然后当你检查它时:

    let
      txt_CheckInTime = "1/20/2020 1:49:41 PM, 1/20/2020 1:57:58 PM, 1/20/2020 2:09:38 PM",
      txt_CheckOutTime = "1/20/2020 1:49:46 PM, 1/20/2020 1:58:03 PM, 1/20/2020 2:09:42 PM",
      Durations = fun_GetDurations(txt_CheckInTime, txt_CheckOutTime)
    in
      Durations
    

    你得到预期的结果:

    00:00:05, 00:00:05, 00:00:04
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2020-04-28
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多