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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

July 2024 Power BI Update

Power BI Monthly Update - July 2024

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

July Newsletter

Fabric Community Update - July 2024

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

Top Solution Authors
Top Kudoed Authors