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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
amay
New Member

Power Query change column text based on a condition

Hello,

I need to replace values in power query based on a condition:

If Column B contains 31456 or 42987 or 45028 change Column A to "Direct sales" otherwise leave column A as is.  I've tried every example of code I can find on here and none work.

1 ACCEPTED SOLUTION
jbwtp
Memorable Member
Memorable Member

Hi @amay,

 

if you want to actually replace  rather then add column and then delete/rename (which I think just as equally Ok performance-wise), you can try this code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcfLFYAgDATAXvbMARJQOPq3h7z034a6Zm5jhgUJWmqb4Mmwvqsy+sxt31qWzu2xwR3/NHNnrHBXTLg7pnB/AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [A = _t, B = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"A", type text}, {"B", Int64.Type}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [B], "Direct Sales", (x, y, z)=> if List.Contains({31456, 42987, 45028}, y) then z else x,{"A"})
in
    #"Replaced Value"

 

 

Cheers,

John

 

View solution in original post

2 REPLIES 2
jbwtp
Memorable Member
Memorable Member

Hi @amay,

 

if you want to actually replace  rather then add column and then delete/rename (which I think just as equally Ok performance-wise), you can try this code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcfLFYAgDATAXvbMARJQOPq3h7z034a6Zm5jhgUJWmqb4Mmwvqsy+sxt31qWzu2xwR3/NHNnrHBXTLg7pnB/AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [A = _t, B = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"A", type text}, {"B", Int64.Type}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [B], "Direct Sales", (x, y, z)=> if List.Contains({31456, 42987, 45028}, y) then z else x,{"A"})
in
    #"Replaced Value"

 

 

Cheers,

John

 

sudhav
Helper V
Helper V

you can try this. add a conditional column

sudhav_0-1676482943388.png

sudhav_1-1676483086522.png

refer two pics.

If I answered your question, please accept it as solution and give kudos.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.