【问题标题】:Power Query: Add multiple custom columns with List.AccumulatePower Query:使用 List.Accumulate 添加多个自定义列
【发布时间】:2020-08-09 20:09:03
【问题描述】:

在 Excel Power Query 中,我尝试使用 List.Accumulate 添加多个自定义列,并希望使用 currentvalue 中的值填充每一列。

这是我的示例代码:

let
    Query1 = let
    Source = Excel.CurrentWorkbook(),
    #"Expanded Content" = Table.ExpandTableColumn(Source, "Content", {"Region", "Industry", "Product type", "Series", "2000", "2001", "2002", "2003", "2004", "2005"}, {"Region", "Industry", "Product type", "Series", "2000", "2001", "2002", "2003", "2004", "2005"})
in
    #"Expanded Content",
    #"Changed Type" = Table.TransformColumnTypes(Query1,{{"2000", type number}, {"2001", type number}, {"2002", type number}, {"2003", type number}, {"2004", type number}, {"2005", type number}}),
    #"Added Custom" = List.Accumulate({2000..2005}, #"Changed Type", (state, current) => Table.AddColumn(state, "P_" & Number.ToText(current), each [current]))
in
    #"Added Custom"

一切正常,我得到了 6 个名为 P_2000、P_2001 等的新列。 例如,我想用“旧列”中的相应数据填充每个新列。因此,在“P_2000”列中,我希望与现有“2000”列中的数字相同。

each [current] 出现错误,因为它无法识别列名。有什么解决方法吗?我试图将“当前”值转换为文本,但这对我也不起作用。

欢迎任何想法!

【问题讨论】:

    标签: excel powerquery


    【解决方案1】:

    未经测试,但我认为您需要更改此行:

    #"Added Custom" = List.Accumulate({2000..2005}, #"Changed Type", (state, current) => Table.AddColumn(state, "P_" & Number.ToText(current), each [current]))
    

    到:

    #"Added Custom" = List.Accumulate({2000..2005}, #"Changed Type", (state, current) => Table.AddColumn(state, "P_" & Number.ToText(current), each Record.Field(_, current)))
    

    在处理记录和访问字段时,我相信您在 [ 和 ] 之间放置的任何内容都会按字面意思处理。简单地说,区别在于:

    • [current] = 获取字面上命名为“当前”的字段
    • Record.Field(_, [current]) = 获取名为 current 的任何值的字段

    不相关,但由于您添加的列与复制的列相同,因此在您的 @987654332 中使用 Table.DuplicateColumn (details) 而不是 Table.AddColumn (details) 可能更有意义@代码。

    【讨论】:

    • 太棒了!非常感谢 - Record.Field 是我问题的答案。我唯一需要更改的是“当前”给了我一个数字值,我必须将其转换为文本。最后的代码如下所示:#"Added Custom" = List.Accumulate({2000..2005}, #"Changed Type", (state, current) => Table.AddColumn(state, "P_" & Number .ToText(current), 每个 Record.Field(_, Number.ToText(current))))
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 2022-01-24
    • 2018-01-25
    • 2020-06-04
    相关资源
    最近更新 更多