Forum Discussion
UNION + UNIQUE does not return UNIQUE table
- 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. - 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
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
Thank you so much for your input v-kkf-msft . That char count worked as intended too. 2 very useful Query lines (for me at least)😊