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

Top Solution Authors
Top Kudoed Authors