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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
StuartPQY
Frequent Visitor

Faster M code at execution time for nested if statement

Hello

 

I have the following code in a nested if statment format

 

if GetVar("BU") = "UK" and [Cost Centre] >= 17000 and [Cost Centre] < 10826 then "EUR"
else if GetVar("BU") = "UK" and [Payment Currency Code] = null
then "GBP"
else if GetVar("BU") = "LM" and [Legal Entity] = "aaaaa" then "USD"
else if GetVar("BU") = "LM" and [Legal Entity] = "bbbbb" then "USD"
else if GetVar("BU") = "LM" and [Legal Entity] = "ccccc" then "GBP"
else if GetVar("BU") = "LM" and [Legal Entity] = "ddddd" then "GBP"
else if GetVar("BU") = "LM" and [Payment Currency Code] = null then "USD"
else [Payment Currency Code]

 

I am wondering if there is a faster way to write this in M code.  I mean faster at execution time.

 

Thank you

 

Stuart

1 ACCEPTED SOLUTION
Omid_Motamedise
Super User
Super User

replace your if conditions with the below code

if GetVar("BU") = "UK" then
    if [Cost Centre] >= 17000 and [Cost Centre] < 10826 then "EUR"
    else if [Payment Currency Code] = null then "GBP"
    else [Payment Currency Code]
else if GetVar("BU") = "LM" then
    if [Legal Entity] = "aaaaa" or [Legal Entity] = "bbbbb" then "USD"
    else if [Legal Entity] = "ccccc" or [Legal Entity] = "ddddd" then "GBP"
    else if [Payment Currency Code] = null then "USD"
    else [Payment Currency Code]
else 
    [Payment Currency Code]

 

 

in the if using and/or  will evaluate every time but replaced them by a nested if can increase the efficiency.

 

another tips which is related to the data structure, use the condition wich can filter more rows in the first condition series.


If my answer helped solve your issue, please consider marking it as the accepted solution.

View solution in original post

4 REPLIES 4
Omid_Motamedise
Super User
Super User

replace your if conditions with the below code

if GetVar("BU") = "UK" then
    if [Cost Centre] >= 17000 and [Cost Centre] < 10826 then "EUR"
    else if [Payment Currency Code] = null then "GBP"
    else [Payment Currency Code]
else if GetVar("BU") = "LM" then
    if [Legal Entity] = "aaaaa" or [Legal Entity] = "bbbbb" then "USD"
    else if [Legal Entity] = "ccccc" or [Legal Entity] = "ddddd" then "GBP"
    else if [Payment Currency Code] = null then "USD"
    else [Payment Currency Code]
else 
    [Payment Currency Code]

 

 

in the if using and/or  will evaluate every time but replaced them by a nested if can increase the efficiency.

 

another tips which is related to the data structure, use the condition wich can filter more rows in the first condition series.


If my answer helped solve your issue, please consider marking it as the accepted solution.

Thank you to all replies.

 

Regards

 

Stuart

AlienSx
Super User
Super User

calculate GetVar("BU") beforehand

lbendlin
Super User
Super User

Use List.Contains()

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.