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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Marija_JC
New Member

Help with M code conditional column

Hello, 

I woluld like to create an coditional column based on the text values from another column. So, it should be like:

if column B has value " x", then text in column A sholud start from 4th character, else

if column B has valu"y", then text  in column A should be extracted befor delimiter, else

if column B has value"z", then in column A should be in the middle of certain delimiters.

How should M code look like in Custom column?

Thanks in advance

5 REPLIES 5
v-agajavelly
Community Support
Community Support

Hi @Marija_JC ,

Did you get a chance to try the solution above? Just checking in to see if it worked as expected for your "X", "Y", and "Z" conditions, or if you’ve run into any edge cases where it behaves differently.

Regards,
Akhil.

v-agajavelly
Community Support
Community Support

Hi @Marija_JC ,

Thanks @MarkLaf  for the clean and accurate solution  your logic perfectly aligns with the expected output shared by OP. 

Hey @Marija_JC  did you get a chance to look at the solution above? It seems to cover all your conditions for "X", "Y" and "Z"exactly as described and shown in your screenshot. Let us know if it works as expected on your end or if you ran into any edge cases happy to help further.

Regards,
Akhil.

Omid_Motamedise
Super User
Super User

use the following formula

= 
if [ColumnB] = "x" then
    Text.Middle([ColumnA], 3) 
else if [ColumnB] = "y" then
    Text.BeforeDelimiter([ColumnA], "-") 
else if [ColumnB] = "z" then
    Text.BetweenDelimiters([ColumnA], "(", ")") 
else
    null
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
burakkaragoz
Community Champion
Community Champion

Hi @Marija_JC ,

 

The solution( @MarkLaf ) looks solid, but I'd tweak a couple things to make it more bulletproof:

if [Column B] = "x" then 
    if Text.Length([Column A]) > 3 then Text.RemoveRange([Column A], 0, 3) else [Column A]
else if [Column B] = "y" then 
    Text.BeforeDelimiter([Column A], "_")  // assuming underscore based on your sample
else if [Column B] = "z" then 
    Text.BetweenDelimiters([Column A], "_", "-")
else 
    [Column A]  // keep original instead of null

Main improvements:

  • Length check for "x" case (what if your text is only 2 characters?)
  • Return original text instead of null for unmatched cases
  • Delimiter choice for "y" - space vs underscore depends on your actual data

The original answer works fine if your data is clean, but real-world data usually has edge cases that break simple logic.

What delimiters are you actually working with? That'll determine whether you need space or underscore for the "y" condition.


If my response resolved your query, kindly mark it as the Accepted Solution to assist others. Additionally, I would be grateful for a 'Kudos' if you found my response helpful.
This response was assisted by AI for translation and formatting purposes.

MarkLaf
Super User
Super User

Given a table like below:

 

Column A Column B
text1 text2_text3-text4 x
text1 text2_text3-text4 y
text1 text2_text3-text4 z
text5 text6_text7-text8 x
text5 text6_text7-text8 y
text5 text6_text7-text8 z

 

I think the following code in a custom column would get what you want:

 

if [Column B] = "x" 
    then Text.RemoveRange( [Column A], 0, 3 ) else 
if [Column B] = "y" 
    then Text.BeforeDelimiter( [Column A], " " ) else
if [Column B] = "z" 
    then Text.BetweenDelimiters([Column A], "_", "-") else
null

 

Output:

 

MarkLaf_0-1753996423810.png

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors