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
Anonymous
Not applicable

Replace value of a column IF NOT equal to

Hi everyone,

 

I would like to give as an example the following query:

 

Index

Main CodeSub Code

1

CPCP1
2CPCP2
3ARAR1
4CPCP3
5QSQS1

 

I was wondering whether it would be possible to replace the values of Sub Code with "Other" if the values in Main Code are not equal to  "CP". The table should look like this:

 

Index

Main CodeSub Code

1

CPCP1
2CPCP2
3AROther
4CPCP3
5QSOther

 

Is this possible and what statement in Power Query could do the trick?

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @Anonymous ,

 

Yes, you can do this in Power Query by editing the M code generated by the Replace Value GUI.

 

Select your [Sub Code] field then go to Replace Values in the Transform tab.

When the dialog opens, just type any text (e.g. udsw etc.) in both the Find and Replace With boxes (don't need to be the same, just need to fill arguments).

Then edit the arguments where you can see the gobbledegook in the formula bar like this:

 

= Table.ReplaceValue(#"Changed Type", each [Sub Code], each if [Main Code] <> "CP" then "Other" else [Sub Code], Replacer.ReplaceText,{"Sub Code"})

 

 

This gives me the following ouput:

BA_Pete_0-1630509558871.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

3 REPLIES 3
BA_Pete
Super User
Super User

Hi @Anonymous ,

 

Yes, you can do this in Power Query by editing the M code generated by the Replace Value GUI.

 

Select your [Sub Code] field then go to Replace Values in the Transform tab.

When the dialog opens, just type any text (e.g. udsw etc.) in both the Find and Replace With boxes (don't need to be the same, just need to fill arguments).

Then edit the arguments where you can see the gobbledegook in the formula bar like this:

 

= Table.ReplaceValue(#"Changed Type", each [Sub Code], each if [Main Code] <> "CP" then "Other" else [Sub Code], Replacer.ReplaceText,{"Sub Code"})

 

 

This gives me the following ouput:

BA_Pete_0-1630509558871.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Hello!

How to say -->  if not equal to "string1" or "string2" then "string3" please ? Thanks!

 

Hi @cher90 ,

 

It would be something like this:

if [yourField] <> "String 1" and [yourField] <> "String 2"
then "String 3"
else //your escape value e.g. null, "Error" etc.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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