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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Ak14pbi
New Member

Need Assistance with M Code - Changing Values in a Column based on a Condition in Another Column

There are two columns, CLASS and DOC_TEXT. Where rows under DOC_TEXT contain (not equal to) the word "sale", values under column CLASS must change to "Direct Sale". I used TransformColumns and I believe I have everything correct. No errors occur when applying this M code in the advanced editor and closing out. However, in the "edit query" page, after this step is applied, the entire CLASS column - all rows now say "error" .. I can't figure out what I'm doing wrong. Should I be using the ReplaceValues function? 

 

My code (previous step is called Replaced Value1) :

 

#"Updated Column" = Table.TransformColumns(#"Replaced Value1", {{"CLASS", each

     if Text.Contains([DOC_TEXT], "sale", Comparer.OrdinalIgnoreCase) then "Direct Sale"

     else [CLASS], type text}})

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @Ak14pbi, check this:

 

Output

dufoq3_1-1739285072688.png

 

If you want to seach for "sale" case insensitive, replace Comparer.Ordinal with Comparer.OrdinalIgnoreCase

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUSrOz01VKEmtKFGK1YlWcgKKJCYlKxQn5qQqpKalgwWdQcqAAmCOC5CTX5KRWgTVFAsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CLASS = _t, DOC_TEXT = _t]),
    ReplacedClass = Table.ReplaceValue(Source,
        each Text.Contains([DOC_TEXT], "sale", Comparer.Ordinal),
        each "Direct Sale",
        (x,y,z)=> if y then z else x,
        {"CLASS"} )
in
    ReplacedClass

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

8 REPLIES 8
v-mdharahman
Community Support
Community Support

Hi @Ak14pbi,

Thanks for reaching out to the Microsoft fabric community forum.

 

It looks like you are trying to transfrom a column when there's a certain value (sale) in it. As @Omid_Motamedise@dufoq3 and @ZhangKun provided solutions from their side. Please go through the solutions provided and mark the helpful reply as solution.

 

I would also take a moment to thank @Omid_Motamedise, @dufoq3 and @ZhangKun, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.

 

If I misunderstand your needs or you still have problems on it, please feel free to let us know.  

Best Regards,
Hammad.
Community Support Team

 

If this post helps then please mark it as a solution, so that other members find it more quickly.

Thank you.

Omid_Motamedise
Super User
Super User

In transforming the column, you are not allwed to refer to other column like  [CLASS], so as mentioned use replace command or add a new column, or merge the both column.

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

Thank you so much!

dufoq3
Super User
Super User

Hi @Ak14pbi, check this:

 

Output

dufoq3_1-1739285072688.png

 

If you want to seach for "sale" case insensitive, replace Comparer.Ordinal with Comparer.OrdinalIgnoreCase

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUSrOz01VKEmtKFGK1YlWcgKKJCYlKxQn5qQqpKalgwWdQcqAAmCOC5CTX5KRWgTVFAsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CLASS = _t, DOC_TEXT = _t]),
    ReplacedClass = Table.ReplaceValue(Source,
        each Text.Contains([DOC_TEXT], "sale", Comparer.Ordinal),
        each "Direct Sale",
        (x,y,z)=> if y then z else x,
        {"CLASS"} )
in
    ReplacedClass

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Thank you so much for your help!

You're welcome.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

ZhangKun
Super User
Super User

Yes, you should use ReplaceValues.

One very important difference is that TransformColumns cannot access other columns, but ReplaceValue can do it.

Thank you for your help!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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