Forum Discussion

KennT's avatar
KennT
Frequent Visitor
4 years ago
Solved

UNION + UNIQUE does not return UNIQUE table

Hi I have 3 tables (imported from 3 different files). I need Part number (UNIQUE) and a Partnumber description from those 3 tables merged into 1 table, which I plan to use as a support table for 1:m...
  • v-kkf-msft's avatar
    v-kkf-msft
    4 years ago

    Hi KennT ,

     

    If you want to get the description of the highest count, please try the following code.

     

    let
        Source = Table.Combine({Stock2, Backlog2, Sales2}),
        #"Removed Columns" = Table.RemoveColumns(Source,{"QTY"}),
        #"Grouped Rows" = Table.Group(#"Removed Columns", {"Part Number", "Description"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
        #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"Part Number"}, {{"MaxCount", each List.Max([Count]), type number}, {"All", each _, type table [Part Number=nullable number, Description=nullable text, Count=number]}}),
        #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows1", "All", {"Description", "Count"}, {"Description", "Count"}),
        #"Added Custom" = Table.AddColumn(#"Expanded All", "Custom", each if [MaxCount] = [Count] then [Description] else null),
        #"Grouped Rows2" = Table.Group(#"Added Custom", {"Part Number"}, {{"Description", each List.Max([Custom]), type nullable text}})
    in
        #"Grouped Rows2"

     

     

    And this code gets the description of the maximum ordinal number when sorted by string.

     

    let
        Source = Table.Combine({Stock2, Backlog2, Sales2}),
        #"Removed Columns" = Table.RemoveColumns(Source,{"QTY"}),
        #"Grouped Rows" = Table.Group(#"Removed Columns", {"Part Number"}, {{"Description", each List.Max([Description]), type nullable text}})
    in
        #"Grouped Rows"


    If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
    Best Regards,
    Winniz
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

  • v-kkf-msft's avatar
    v-kkf-msft
    4 years ago

    Hi KennT ï¼Œ

     

    Please try the following code.  Take the description with the highest number of characters per Part Number, and if the strings are the same length then take maximum.

     

    let
        Source = Table.Combine({Backlog2, Sales2, Stock2}),
        #"Added Custom" = Table.AddColumn(Source, "Length", each Text.Length([Description])),
        #"Grouped Rows" = Table.Group(#"Added Custom", {"Part Number"}, {{"MaxLength", each List.Max([Length]), type number}, {"Allrows", each _, type table [Part Number=nullable number, Description=nullable text, Length=number]}}),
        #"Expanded Allrows" = Table.ExpandTableColumn(#"Grouped Rows", "Allrows", {"Description", "Length"}, {"Description", "Length"}),
        #"Filtered Rows" = Table.SelectRows(#"Expanded Allrows", each ([Length] = [MaxLength])),
        #"Grouped Rows1" = Table.Group(#"Filtered Rows", {"Part Number"}, {{"Description", each List.Max([Description]), type nullable text}})
    in
        #"Grouped Rows1"

     

    Best Regards,
    Winniz