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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
freelensia
Advocate II
Advocate II

How to union items from two columns of same table

I have a table with 2 columns that I want to union like this:

 

Column 1Column 2Union
apple, orange, apleapple, lemonapple, orange, lemon

 

Is there a custom formula I can write? Or do I need to make a PQ function?

Thanks for any of your ideas, @Jimmy801 @Greg_Deckler @amitchandak @parry2k @Mariusz @ImkeF 

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

You can add a custom column with this function.  Prior to this step, you can Replace Values and put a space and no value in the 2nd box to get rid of the spaces first if needed.

 

= Text.Combine(List.Distinct(Text.Split([Column1], ",") & Text.Split([Column2], ",")), ", ")

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

6 REPLIES 6
Anonymous
Not applicable

image.png

Simple method, but this does not give me distinct lists. Thanks though!

Anonymous
Not applicable

Sorry, I didn't read the specs right 🙂 however, starting from this result you can use functions

Text.Split

List.Distinct

and

Text.Combine

mahoneypat
Employee
Employee

You can add a custom column with this function.  Prior to this step, you can Replace Values and put a space and no value in the 2nd box to get rid of the spaces first if needed.

 

= Text.Combine(List.Distinct(Text.Split([Column1], ",") & Text.Split([Column2], ",")), ", ")

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hi @mahoneypat your solution looks simple. Works. I added Text.Lower to ignore upper vs. lowercase differences. I also changed the delimiter to ", "

freelensia_0-1612426602170.png

 

 

Btw where is the duplicate of my post?

smpa01
Super User
Super User

@freelensiatry this

let
    Source = Web.BrowserContents("https://community.powerbi.com/t5/Power-Query/How-to-union-items-from-two-columns-of-same-table/m-p/1642958#M50238"),
    #"Extracted Table From Html" = Html.Table(Source, {{"Column1", "TABLE:nth-child(3) > * > TR > :nth-child(1)"}, {"Column2", "TABLE:nth-child(3) > * > TR > :nth-child(2)"}, {"Column3", "TABLE:nth-child(3) > * > TR > :nth-child(3)"}}, [RowSelector="TABLE:nth-child(3) > * > TR"]),
    #"Changed Type" = Table.TransformColumnTypes(#"Extracted Table From Html",{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Column 1", type text}, {"Column 2", type text}, {"Union", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type1","apple, orange, aple","apple,orange,apple",Replacer.ReplaceText,{"Column 1"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","apple, lemon","apple,lemon",Replacer.ReplaceText,{"Column 2"}),
    #"Trimmed Text" = Table.TransformColumns(#"Replaced Value1",{{"Column 1", Text.Trim, type text}, {"Column 2", Text.Trim, type text}}),
    #"Added Custom2" = Table.AddColumn(#"Trimmed Text", "Custom.2", each List.Distinct(List.Combine({List.Distinct(Text.Split([Column 1],",")),List.Distinct(Text.Split([Column 2],","))}))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom2", {"Custom.2", each Text.Combine(List.Transform(_, Text.From), ","), type text})
in
    #"Extracted Values"

 

Capture.PNG

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors