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
jcastr02
Post Prodigy
Post Prodigy

merging columns with rules

How can I merge two columns into new column (col C) where if col A and B are equal then it should only give me the value of what is first column.  If they are not equal then merge two columns together?

 

Col ACol BNew Column  (Col C)
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Example MatchExample MatchExample Match
Muscle Shoals Muscle Shoals
Junuea Junuea
New CityFloridaNew CityFlorida
New CityJupiterNew CityJupiter
New CityJupiterNew CityJupiter
Fort Lauderdale Fort Lauderdale
Miami Miami
1 ACCEPTED SOLUTION
jdbuchanan71
Super User
Super User

You could add this as a new calculated column:

Col C = IF ( [Col A] = [Col B], [Col A], [Col A] & [Col B] )

View solution in original post

3 REPLIES 3
edhans
Super User
Super User

@jcastr02 

If you do this in Power Query, you can use this formula in a custom column. You could then remove the other two columns and onlhy bring in the final column to Power BI if you wish.

if [Col A] = [Col B] then [Col A] 
else Text.Trim([Col A] & " " & [Col B])

It generates the 3rd column here:

edhans_0-1602528023794.png

Full M code is here:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcq1IzC3ISVXwTSxJzlDSQePH6oyqGKwqfEuLk4ECwRn5iTnFQBVgQa/SvNLURBjPL7VcwTmzpBLId8vJL8pMSUQX9iotyCxJLcIn7JZfVKLgk1iaklqUkpiTCjPbNzMxNxPMiQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Col A" = _t, #"Col B" = _t]),
    #"Added New Column" = Table.AddColumn(Source, "New Column", each if [Col A] = [Col B] then [Col A] 
else Text.Trim([Col A] & " " & [Col B])),
    #"Changed Type" = Table.TransformColumnTypes(#"Added New Column",{{"New Column", type text}})
in
    #"Changed Type"

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
jdbuchanan71
Super User
Super User

You could add this as a new calculated column:

Col C = IF ( [Col A] = [Col B], [Col A], [Col A] & [Col B] )

@jdbuchanan71 Thank you, that worked!

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.