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, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
JoaoMS
Helper III
Helper III

Replace value in Power Query based on condition

Dear all,

 

I have a table ("POs") with a list of POs which have some fields that in very specifics moments have wrong entry inputs. For example in the following table the PO "1006" should have the Category "C" instead of the Category "D":

 

PO Category Amount
1001 A 1050
1002 A 1327
1003 B 1276
1004 B 1383
1005 C 1967
1006 D 1288
1007 E 1707
1008 F 1233

 

I tried to use the ReplaceValue option in Power Query but it replaces all the the Categories "D" of all the "POs". I just want to replace the category of the PO "1006".

Thanks

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

HI @JoaoMS,

You can use replace value function with custom expression to add conditions to replace value steps:

    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Category],each if [PO]=1006 and [Category]="D" then "C" else [Category],Replacer.ReplaceText,{"Category"})

15.png

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

9 REPLIES 9
ajinkyacgaikwad
Advocate I
Advocate I

 

Please watch this video for conditional replace values : https://youtu.be/U5SzfXeuM5k

szabooimre
New Member

This is a simpler solution:

#"Replaced Value" = Table.ReplaceValue(#"Changed Type","D",each if [PO]=1006 then "C" else "D",Replacer.ReplaceText,{"Category"})
Anonymous
Not applicable

Dear community, I've got also issue with the creation of the "replace" code.

I need to replace value based on multiple condition.

I've attached and extract of the data that I want to manage.

They are the conditions that I would like to apply :

If [Billing code - description] = "--Unknown billing code--" AND [User - name] = "svMedIS_App Compte de service RadOffice" AND [Location] = "Rennaz" then [Billing code] = "A" else "No change"

If [Billing code - description] = --Unknown billing code-- AND [User - name] = svSIMop Compte de service Logimen AND [Location] = Rennaz then [Billing code] = "B" else "No change"

If [Billing code - description] = --Unknown billing code-- AND [User - name] = svSIMop Compte de service Logimen AND [Location] = Providence then [Billing code] = "C" else "No change"

 

here the dataset I used for my test 

Thanks for your reply and help

 

v-shex-msft
Community Support
Community Support

HI @JoaoMS,

You can use replace value function with custom expression to add conditions to replace value steps:

    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Category],each if [PO]=1006 and [Category]="D" then "C" else [Category],Replacer.ReplaceText,{"Category"})

15.png

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

I think you have an error in the syntax, should be Replacer.ReplaceValue instead of Replacer.ReplaceText. The whole line is thus:

#"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Category],each if [PO]=1006 and [Category]="D" then "C" else [Category],Replacer.ReplaceValue,{"Category"})

 

This worked for me, not the original answer

 

Thank you, this is what I was looking for without creating a new column. I just made a small change "each if [PO]=1006 then "C" else [Category]", considering that there is just one PO named "1006" Regards
Anonymous
Not applicable

Try this way : 

 

Step1 combine column " PO" and " Category" , 

Step2 replace"1006D" into "1006C" 

Step3 Split the combined column to " PO" and " Category"

amitchandak
Super User
Super User

@JoaoMS , Something like this

if [PO] = "1006" then Text.Replace([Category], "C", "D") else [Category]

 

This is new column script

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

Helpful resources

Announcements
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.

October NL Carousel

Fabric Community Update - October 2024

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