Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
KennT
Frequent Visitor

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:many relationships for the rest of the data model.

I have tried a UNION + UNIQUE code (as sample file) to generate a new table but since the descriptions vary from file to file, the table naturally ends up with multiple part numbers with the associated description. Like this:

KennT_1-1646910862546.png

I need the part numbers to be UNIQUE with ANY description from either file. So a single line for PN 11 with either A1 OR A2 as description. I have shared a sample data set here: https://1drv.ms/u/s!ArDn3OiIpCOZh9k9zES3jqjX6zPsUA?e=E4CqaW. IF the description could be prioritized between the 3 files it would be an added bonus, but I am happy just to get any description as long as the partnumber is unique.

 

Unsure if the solution should be found in PowerQuery as a join or as a DAX code.

 

Hope someone can help me out 🙂

2 ACCEPTED SOLUTIONS

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"

vkkfmsft_0-1647419601950.png


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.

 

View solution in original post

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

View solution in original post

8 REPLIES 8
amitchandak
Super User
Super User

@KennT , Try like

distinct(union(distinct(Table1[Part Number]),distinct(Table2[Part Number])))

 

if there are two description then use summarize

 

union(

summarize(Table1, Table1[Part Number],"Desc" ,Table1[Part Desc]) ,

summarize(Table2, Table1[Part Number],"Desc" ,Table2[Part Desc]) )

 

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Thank you for your suggestion @amitchandak , however not sure I follow.

"distinct(union(distinct(Table1[Part Number]),distinct(Table2[Part Number])))" would just create a unique list of PNs but WITHOUT any description?

Also the summarize is not working - assume it is because it is text fields.

Would appreciate it if you could demonstrate in the sample data.

Hi @KennT ,

 

Please create the new column:

 

Column = 
CALCULATE (
    CONCATENATEX ( 'PN Table', [PN Descr], " or " ),
    ALLEXCEPT ( 'PN Table', 'PN Table'[Part Number] )
)

vkkfmsft_2-1647247150761.png

 

Or try the code in Power Query.

 

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

vkkfmsft_1-1647247138272.png

 

 

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.

Thank you @v-kkf-msft. Absolutely a step towards the desired solution🙂 Prefer the Power Query approach.

Any way to approach - "Description", each Text.Combine([Description] - a bit more dynamically, as data is updated weekly with new duplicates appearing from time to time?

 

I could add "replace value" for each combined description I encounter, but an automated replacement for future duplet descriptions would be the preferred scenario. 


Could the query be coded to return the description with the highest count or at least just return 1 description at random or first/last encounter?

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"

vkkfmsft_0-1647419601950.png


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 my sincere apologies for not answering sooner, but have been absolutely swamped with work for the past week.

Your solution worked perfectly! Pure magic. I have applied it to a larger data set and added more columns to the query you sent. Thank you @v-kkf-msft, been struggling with this for quite some time.

If you dont mind a last follow-up question. I have another dataset I want to apply this solution to, but instead a picking the description based on count, I want the query to pick the longest description (with the most characters) with duplets. Would appreciate your input if you have the time.

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)😊

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.