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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

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
Community Champion
Community Champion

@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
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.