Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

How can I choose which duplicates to remove?

Hello

 

I want to remove rows that have duplicates in Column1. But I want to choose which duplicates to remove, i.e. remove the duplicate where Column2 has a specific value or is null or any other criterion.

 

How can I do that?

 

I was thinking to use some sorting, but not sure if PowerBI removes duplicates in order.

 

Thanks!

  • Hi Anonymous ,

    Try to create a query like this:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUVKK1YEwjAzATCeYGJzhDGQYmiKYEHUuQKYxhOkKZJoAFcQCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
        a = Table.AddColumn(#"Changed Type","count", each let Column1=[Column1] in Table.RowCount(Table.SelectRows(#"Changed Type",each Column1 = [Column1]))),
        #"Filtered Rows" = Table.SelectRows(a, each [count] = 1 or [Column2] = null or [Column2] = 15),
        #"Removed Duplicates" = Table.Distinct(#"Filtered Rows", {"Column1"}),
        #"Removed Columns" = Table.RemoveColumns(#"Removed Duplicates",{"count"})
    in
        #"Removed Columns"

    Attached a sample file in the below, hopes to help you.

     

    Best Regards,
    Community Support Team _ Yingjie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • v-yingjl's avatar
    v-yingjl
    Community Support

    Hi Anonymous ,

    Try to create a query like this:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUVKK1YEwjAzATCeYGJzhDGQYmiKYEHUuQKYxhOkKZJoAFcQCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
        a = Table.AddColumn(#"Changed Type","count", each let Column1=[Column1] in Table.RowCount(Table.SelectRows(#"Changed Type",each Column1 = [Column1]))),
        #"Filtered Rows" = Table.SelectRows(a, each [count] = 1 or [Column2] = null or [Column2] = 15),
        #"Removed Duplicates" = Table.Distinct(#"Filtered Rows", {"Column1"}),
        #"Removed Columns" = Table.RemoveColumns(#"Removed Duplicates",{"count"})
    in
        #"Removed Columns"

    Attached a sample file in the below, hopes to help you.

     

    Best Regards,
    Community Support Team _ Yingjie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Anonymous's avatar
    Anonymous
    Not applicable

    You could use table.group on the key column and then apply the Table.SelectRows with the criteria you want to each group. Finally expand the column.

     

    PS

    If you provide a sample table that is easily copied, I can try writing code that realizes this idea.