Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Create a table with unique item from another table with duplicate

I have a table called Item as shown below:

Note this table has duplicated ID and Item but Status is not.

I want to create a table from the Item table to have a unique ID, Item with Status, However, Item with the Sold Status is important than Not Sold and not sold is important than -- . for example, Coffee has three status but I want the table to pick only Coffee with Sold status. Another example, Monitor has 2 status "--" and "Not Sold", hence "Not Sold" should be returned.

The screenshot below shows the return table:

 

How can I achieve this?

 

 

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Anonymous - I would approach this by adding a new column with a sort key.  Sold = 1, Not Sold = 2, -- = 3.  The table can now be sorted by ID and Sort Key, and then use the Duplicate function.  This would retain the first version of the ID with the Sold row.  The key to making this work is the Buffer step.  This make sure that the sort in applied before the distinct.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXLOT0tLTQUydHWVYnXQxILzc1IwRf3ySxTgMkZAAd/MnGxk1UhiKGqNgQLhiSWpRQjrTIDMgNQ8dJWmQAGfxIKS/AKEUhQx3KrhomYgV+TnZZbkI9mHKogwJRYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Item = _t, Status = _t]),
        #"Added Custom" = Table.AddColumn(Source, "Sort Key", each if [Status] = "Sold" then 1 else 
    if [Status] = "Not Sold" then 2 else 3, Int64.Type),
        #"Sorted Rows" = Table.Sort(#"Added Custom",{{"ID", Order.Ascending}, {"Sort Key", Order.Ascending}}),
        #"Buffer Table" = Table.Buffer( #"Sorted Rows" ),
        #"Removed Duplicates" = Table.Distinct(#"Buffer Table", {"ID"})
    in
        #"Removed Duplicates"

     

     

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous - I would approach this by adding a new column with a sort key.  Sold = 1, Not Sold = 2, -- = 3.  The table can now be sorted by ID and Sort Key, and then use the Duplicate function.  This would retain the first version of the ID with the Sold row.  The key to making this work is the Buffer step.  This make sure that the sort in applied before the distinct.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXLOT0tLTQUydHWVYnXQxILzc1IwRf3ySxTgMkZAAd/MnGxk1UhiKGqNgQLhiSWpRQjrTIDMgNQ8dJWmQAGfxIKS/AKEUhQx3KrhomYgV+TnZZbkI9mHKogwJRYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Item = _t, Status = _t]),
        #"Added Custom" = Table.AddColumn(Source, "Sort Key", each if [Status] = "Sold" then 1 else 
    if [Status] = "Not Sold" then 2 else 3, Int64.Type),
        #"Sorted Rows" = Table.Sort(#"Added Custom",{{"ID", Order.Ascending}, {"Sort Key", Order.Ascending}}),
        #"Buffer Table" = Table.Buffer( #"Sorted Rows" ),
        #"Removed Duplicates" = Table.Distinct(#"Buffer Table", {"ID"})
    in
        #"Removed Duplicates"