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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

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
Memorable Member
Memorable Member

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.

View solution in original post

4 REPLIES 4
Omid_Motamedise
Memorable Member
Memorable Member

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.

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
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.