如何删除列中的重复项并保留最后一次出现的内容?

问题描述 投票:0回答:2

我的输入是这张表:

category    id
YYY          A
XXX          B
XXX          A
XXX          C
ZZZ          A

当我选择

id
列并选择删除重复项时,它会保留第一次出现的 ids :

category    id
YYY          A
XXX          B
XXX          C

但我需要保留最后一次出现的情况。 PS:顺序很重要。

category    id
XXX          B
XXX          C
ZZZ          A

你们知道怎么做吗?

这是我使用的代码和示例表:

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}}
    ),
    #"Doublons supprimés" = Table.Distinct(#"Type modifié", {"id"})
in
    #"Doublons supprimés"
excel powerquery data-analysis data-cleaning m
2个回答
0
投票
let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WMjDUByIjAyMTJR0lR6VYHaCQEZKQE6YQFlXOECFjVFWxAA==",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [date = _t, id = _t]
    ),
    #"Type modifié" = Table.TransformColumnTypes(Source, {{"date", type date}, {"id", type text}}),
    #"Grouped Rows" = Table.Group(#"Type modifié", {"id"}, {{"All", each _, type table [date=nullable date, id=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each List.Last([All][date]))
in
    #"Added Custom"


0
投票

根据上面的评论 - 建议您添加一个索引列,然后按降序排序,然后删除重复项。

© www.soinside.com 2019 - 2024. All rights reserved.