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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Alex_Ooi
Helper IV
Helper IV

Shortcut on M's conditional columns (if then else)

Hi folks, I'll give  you some background story to my case.

  • I am doing financial modelling from my General Ledger
  • The descriptions (column title [Description]) is unfortunately not standardized and I have to do a lot of guesswork - obviously this is not something scaleable to clean up data, having to replace text for every single occurence
  • So I came up with another column that is supposed to standardize the names (field name [Entity Name])
  • The problem is the formula is super long, and I don't know if I could do something sleek about it and make the code block more readable since I am talking about over 150 lines of conditional

if else.png

the code line that is on repeat has this syntax:

 

if Text.Contains([Description], "keyword from string", Comparer.OrdinalIgnoreCase) then "Standard Name" else if
...
else null, type text

 

I read something similar in this link (https://stackoverflow.com/questions/54368921/find-and-replace-using-wildcard-in-power-bi) but I am not sure if I can reproduce the similar results. I am also not familiar about the use of => in M. If anyone here can show and explain to me where and how I could use => in my M script, it would be much appreciated!

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @Alex_Ooi 

Yes, there could be a misunderstanding, but my solution was expected to work on the original data table. Pls check enclosed file.

 

If that doesn't work, you can send the file to info at.. thebiccountant.com.

 

 

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

6 REPLIES 6
MFelix
Super User
Super User

Hi @Alex_Ooi ,

 

The best option is the M champion @ImkeF .

 

Can you please check this @ImkeF , 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



ImkeF
Community Champion
Community Champion

Hi @Alex_Ooi ,

How is the desired behaviour if multiple items match: Do you only want to replace the first match or subsequent as well?

Is it all the same transformation, just with different "if" and "then" strings?

 

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

@ImkeF thank you so much for reaching out to me!

 

  • Desired behaviour for multiple items match - I am only expecting to replace first match
  • It is all the same conditional transformation (if text contains "xyz" then "do this" else ...)

Looking forward to hearing from you soon.

ImkeF
Community Champion
Community Champion

Hi @Alex_Ooi 

you can create a "Translation"-table with columns "Old String" (contains the string to search for) and "New String" (the replacement).

Then add a column with this formula:

Table.SelectRows(Translation, (t) => Text.Contains([Column1], t[Old String], Comparer.OrdinalIgnoreCase)) {0}[New String]

It will return the correct values for matches and an error when no match is found.

You can use try .. otherwise to fetch the error or simply replace errors afterward with the desired default-value.

 

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

I'm sorry @ImkeF I think I wasn't making myself clear. I want to create a column in my current data table. Can I replicate this by using Table.AddColumns?

 

Also, I think I have misunderstood your question on "multiple matches". I thought you meant multiple matches in the column I am searching for using Text.Contains. Can I know how I could send my script to you? I guess my script would be a better explanation than what I am trying to tell you with words.

 

Once again, apologies for the confusion.

ImkeF
Community Champion
Community Champion

Hi @Alex_Ooi 

Yes, there could be a misunderstanding, but my solution was expected to work on the original data table. Pls check enclosed file.

 

If that doesn't work, you can send the file to info at.. thebiccountant.com.

 

 

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

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors