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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
Anonymous
Not applicable

if condition for M code

Hi,

 

I'm using this dax but how can I write M code in query when create new custom column?

Also is there any way change the values directly without adding new column? 

 

 

IF([MRBTS ID] < 113000, "A",
IF([MRBTS ID >= 113000 && [MRBTS ID] < 125401, "B",
IF([MRBTS ID >= 125401 && [MRBTS ID] < 130000, "C",
IF([MRBTS ID >= 130000 && [MRBTS ID] < 160000, "D",
Blank()

 

 

 

jeongkim_0-1749714122588.png

 

1 ACCEPTED SOLUTION
hnam_2006
Frequent Visitor

Hi

 

You can use the "conditional column" option in add column ribbon option. Make sure you define each condition in seperate line

 

hnam_2006_0-1749715610345.png

 

Or if you want to write the code, you can use below code option

 

= Table.AddColumn(YourPreviousStepName, "New Column", each 
    if [MRBTS ID] < 113000 then "A" 
    else if [MRBTS ID] >= 113000 and [MRBTS ID] < 125401 then "B" 
    else if [MRBTS ID] >= 125401 and [MRBTS ID] < 130000 then "C" 
    else if [MRBTS ID] >= 130000 and [MRBTS ID] < 160000 then "D" 
    else null)

 

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

 

View solution in original post

3 REPLIES 3
Elena_Kalina
Solution Sage
Solution Sage

Hi @Anonymous 

You can replace values in an existing column using Transform > Replace Values or with M code:

  1. Right-click your query and select Advanced Editor

  2. Add a step like this after your source:

= Table.ReplaceValue(
    PreviousStepName,
    each [MRBTS ID],
    each if _ < 113000 then "A"
         else if _ >= 113000 and _ < 125401 then "B"
         else if _ >= 125401 and _ < 130000 then "C"
         else if _ >= 130000 and _ < 160000 then "D"
         else null,
    Replacer.ReplaceValue,
    {"MRBTS ID"}
)
hnam_2006
Frequent Visitor

Hi

 

You can use the "conditional column" option in add column ribbon option. Make sure you define each condition in seperate line

 

hnam_2006_0-1749715610345.png

 

Or if you want to write the code, you can use below code option

 

= Table.AddColumn(YourPreviousStepName, "New Column", each 
    if [MRBTS ID] < 113000 then "A" 
    else if [MRBTS ID] >= 113000 and [MRBTS ID] < 125401 then "B" 
    else if [MRBTS ID] >= 125401 and [MRBTS ID] < 130000 then "C" 
    else if [MRBTS ID] >= 130000 and [MRBTS ID] < 160000 then "D" 
    else null)

 

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

 

Cookistador
Super User
Super User

Hi @Anonymous 

 

This is the M code for your Dax code

if [MRBTS ID] < 113000 then "A"
else if [MRBTS ID] >= 113000 and [MRBTS ID] < 125401 then "B"
else if [MRBTS ID] >= 125401 and [MRBTS ID] < 130000 then "C"
else if [MRBTS ID] >= 130000 and [MRBTS ID] < 160000 then "D"
else null // Equivalent to BLANK() in DAX

 

For your second question, you can use replace, but probably not the best approach

What I would sugget, would be to remove your main column once the calculated column has been added and then renammed the calculated column by the name of the source one

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.