【问题标题】:Power query, increment column value based on change in value in another column电源查询,根据另一列中值的变化增加列值
【发布时间】:2021-04-02 20:22:00
【问题描述】:

在电源查询中,我将使用 Excel 公式,如下面的屏幕截图链接所示。

在 Power Query M 代码中是否有一种简单的方法可以做到这一点?好像不是这样!

Excel formula

【问题讨论】:

    标签: powerquery


    【解决方案1】:

    您似乎想在 Type 上按组添加索引

    右键单击类型列,然后分组

    使用新的列名:数据,操作:所有行并点击确定

    添加列..索引列..自定义并输入您的起始数字和增量

    使用数据列顶部的箭头展开到新行

    删除多余的列

    示例代码:

    let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Type"}, {{"data", each _, type table}}),
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 23, 1),
    #"Expanded data" = Table.ExpandTableColumn(#"Added Index", "data", {"Type"}, {"Type.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded data",{"Type.1"})
    in  #"Removed Columns"
    

    另一种需要编辑代码的方法

    右键单击类型列并删除重复项(或组,以更容易者为准) 添加索引 将该结果合并回原始数据并展开

    示例代码

    let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    // remove dupes and create index
    #"Removed Duplicates" = Table.Distinct(Source),
    #"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Index", 23, 1),
    //merge the numbered table back into original
    #"Merged Queries" = Table.NestedJoin(Source,{"Type"},#"Added Index",{"Type"},"TT",JoinKind.LeftOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "TT", {"Index"}, {"Index"})
    in #"Expanded Table1"
    

    【讨论】:

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