Forum Discussion
Marija_JC
1 year agoNew 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 cha...
- 1 year ago
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
burakkaragoz
1 year agoSuper User
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 nullMain 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.