Forum Discussion

Sofinobi's avatar
Sofinobi
Helper IV
2 years ago
Solved

delete duplicate in a column without deleting the rows

Hi all, please i need help, i have a sales table, that has in "Stock" column duplicate values, i'm looking if there is a solution to delete duplicate values without deleting all the row. info; dup...
  • ronrsnfld's avatar
    ronrsnfld
    2 years ago

    When tested using the Power BI tools, this code seems to execute much more rapidly than yours.

    • Group by Product ID
    • Add a Shifted stock column to the sub-table
    • If the contents of Stock and Shifted Stock are the same, then replace Stock with a null
    • Remove the Shifted column
    • Re-expand the table

     

    let
        Source = Excel.CurrentWorkbook(){[Name="Table9"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer ID", type text}, {"Product ID", Int64.Type}, {"Sales", Int64.Type}, {"Stock", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Product ID"}, {
            {"all", (t)=> 
                let 
                    #"Add Shifted" = Table.FromColumns( 
                            Table.ToColumns(t) & 
                            {{null} & List.RemoveLastN(t[Stock],1)},
                            Table.ColumnNames(t) & {"Shifted"}),
                    #"Null Dup Stock" = Table.ReplaceValue(
                        #"Add Shifted",
                        each [Stock],
                        each [Shifted],
                        (x,y,z)=> if y = z then null else y,
                        {"Stock"}),
                    #"Remove Shifted" = Table.RemoveColumns(#"Null Dup Stock","Shifted")
                in 
                    #"Remove Shifted",
                type table[Customer ID=text, Product ID=Int64.Type, Sales=Int64.Type, Stock=Int64.Type]}
            
            
            }),
        #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Product ID"}),
        #"Expanded all" = Table.ExpandTableColumn(#"Removed Columns", "all", Table.ColumnNames(#"Changed Type"))
    in
        #"Expanded all"

    Results from your data

     

     

     

     

     

  • dufoq3's avatar
    2 years ago

    Hi Sofinobi, different approach here.

     

    Result

     

    let
        Source = Excel.Workbook(File.Contents("C:\Downloads\2024 Test duplicate.xlsx"), null, true),
        Sales_Table = Source{[Item="Sales",Kind="Table"]}[Data],
        GroupedRows = Table.Group(Sales_Table, {"Product ID"}, {{"All", each 
            [ a = Table.RemoveColumns(_, {"Stock"}),
              b = Table.FromColumns(Table.ToColumns(a) & {{[Stock]{0}?} & List.Repeat({null}, Table.RowCount(a)-1)}, Value.Type(_))
            ][b], type table}}),
        CombinedAll = Table.Combine(GroupedRows[All])
    in
        CombinedAll