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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
lf963
Frequent Visitor

Replace null with if statement

Hello,

 

I have a table shown in the following screenshot:

table.PNG

 

What I want to do is: if column Color is Red AND column Like is null, replace the null in the Like column with "N".

Therefore, the Like column of Apple, Cherry and Dragon fruit should be N.

 

However, the following syntax does not work:

 

= Table.ReplaceValue(#"Changed Type", each [LIKE], each if [Color] = "Red" and [Like] = null then "N" else [Like],Replacer.ReplaceText,{"Like"})

 

 

Does anyone know how to do this?

 

Thanks

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

OK, now the nulls are real nulls (as they are in italics now).
So you have to go back to without quotes and use a different replacer function (ReplaceValue instead of ReplaceText) like so:

Table.ReplaceValue(#"Replaced Value", each [Like], each if [Color] = "Red" and [Like] = null then "N" else [Like],Replacer.ReplaceValue,{"Like"})

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

9 REPLIES 9
Rickmaurinus
Helper V
Helper V

Hi @lf963 ,

 

You can try the following code and use the Replacer.ReplaceValue function. Hope that helps!

 

 

 

 

 

= Table.ReplaceValue(#"Replaced Value1", each [Like], each if [Color] = "Red" and [Like] = null then "Y" else null, Replacer.ReplaceValue,{"Like"})

 

 

 

--------------------------------------------------

@ me in replies or I'll lose your thread

 

Master Power Query M? -> https://powerquery.how

Read in-depth articles? -> BI Gorilla

Youtube Channel: BI Gorilla

 

If this post helps, then please consider accepting it as the solution to help other members find it more quickly.

ImkeF
Super User
Super User

OK, now the nulls are real nulls (as they are in italics now).
So you have to go back to without quotes and use a different replacer function (ReplaceValue instead of ReplaceText) like so:

Table.ReplaceValue(#"Replaced Value", each [Like], each if [Color] = "Red" and [Like] = null then "N" else [Like],Replacer.ReplaceValue,{"Like"})

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

lf963
Frequent Visitor

This works!!! Thank you!!!

ImkeF
Super User
Super User

Did you also try putting null in quotes?

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

lf963
Frequent Visitor

Yes I put the null in quotes in the "Replaced Value1" step in the following screenshot but it doesn't help.

table.PNG

ImkeF
Super User
Super User

..and the M-language is case sensitive, so you have to adjust to this:

Table.ReplaceValue(#"Changed Type", each [Like], each if [Color] = "Red" and [Like] = "null" then "N" else [Like],Replacer.ReplaceText,{"Like"})

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

lf963
Frequent Visitor

Tried lower case Like but no luck

ImkeF
Super User
Super User

Hi @lf963 ,
your null does look like simple text and not the "real" null. So this would probably work:

 

Table.ReplaceValue(#"Changed Type", each [LIKE], each if [Color] = "Red" and [Like] = "null" then "N" else [Like],Replacer.ReplaceText,{"Like"})

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

lf963
Frequent Visitor

Hello @ImkeF,

 

The screenshot shown in my original post was manually created. I should've made them real "null". The following screenshot is the correct one:

table.PNG

 

The following syntax doesn't work:

= Table.ReplaceValue(#"Replaced Value", each [Like], each if [Color] = "Red" and [Like] = null then "N" else [Like],Replacer.ReplaceText,{"Like"})

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Solution Authors
Top Kudoed Authors