You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Power Query中移除ID列重复值并保留最后出现的记录(维持原有顺序)

如何在Power Query中移除ID列重复值并保留最后出现的记录(维持原有顺序)

嘿,我来帮你搞定这个问题!Power Query自带的Table.Distinct函数确实只会保留每个ID的第一次出现,要实现保留最后出现的记录同时维持原顺序,我给你分享两种实用的方法,都是纯Power Query内部操作,不用跳转到别的工具~

方法一:反向倒序去重法(简单直观)

这个方法的核心思路是“反向操作”:先把整个表倒过来,此时原来最后出现的记录就变成了每组ID的第一条,用默认的去重方法保留它,最后再把表倒回原顺序,这样就完美匹配你要的结果了!

修改后的完整M代码如下:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WioyMVNJRclSK1YlWioiIALKdkNjI4s5gdlRUFEQ8FgA=",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [category = _t, id = _t]
    ),
    #"Type modifié" = Table.TransformColumnTypes(
        Source,
        {{"category", type text}, {"id", type text}}
    ),
    // 步骤1:将表倒序排列
    #"Table Inversed" = Table.ReverseRows(#"Type modifié"),
    // 步骤2:按ID去重(此时保留的是原表最后出现的记录)
    #"Removed Duplicates Reversed" = Table.Distinct(#"Table Inversed", {"id"}),
    // 步骤3:再次倒序,还原为原表的顺序逻辑
    #"Restored Original Order" = Table.ReverseRows(#"Removed Duplicates Reversed")
in
    #"Restored Original Order"

方法二:索引分组筛选法(灵活可控)

如果需要更精细的控制(比如后续要扩展其他筛选条件),可以用“添加索引+分组取最大索引”的方式,精准定位每个ID最后出现的记录:

修改后的完整M代码如下:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WioyMVNJRclSK1YlWioiIALKdkNjI4s5gdlRUFEQ8FgA=",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [category = _t, id = _t]
    ),
    #"Type modifié" = Table.TransformColumnTypes(
        Source,
        {{"category", type text}, {"id", type text}}
    ),
    // 步骤1:添加索引列,标记每条记录的原始位置
    #"Added Index" = Table.AddIndexColumn(#"Type modifié", "Index", 0, 1, Int64.Type),
    // 步骤2:按ID分组,每组保留索引值最大的记录(即原表最后出现的那条)
    #"Grouped by ID" = Table.Group(
        #"Added Index",
        {"id"},
        {{"Last Occurrence", each Table.Max(_, "Index"), type table [category=text, id=text, Index=Int64.Type]}}
    ),
    // 步骤3:展开分组后的记录
    #"Expanded Last Occurrence" = Table.ExpandTableColumn(
        #"Grouped by ID",
        "Last Occurrence",
        {"category", "Index"},
        {"category", "Index"}
    ),
    // 步骤4:按原始索引排序,还原原顺序
    #"Sorted by Index" = Table.Sort(#"Expanded Last Occurrence",{{"Index", Order.Ascending}}),
    // 步骤5:移除临时的索引列(可选,根据你的需求保留)
    #"Removed Index Column" = Table.RemoveColumns(#"Sorted by Index",{"Index"})
in
    #"Removed Index Column"

两种方法都能得到你想要的结果:

categoryid
XXXB
XXXC
ZZZA

你可以根据自己的习惯选其中一种,第一种代码更简洁,第二种扩展性更强~

备注:内容来源于stack exchange,提问作者VERBOSE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 09:03:06