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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
laciodrom_80
Helper IV
Helper IV

if statement in M language and Query Editor

Hi all,

 

I'd like to use an if statement in the power bi query editor to choose what lines of m-language code to execute based on a certain variable:

 

For example:

 

 

if myVariable = 0 then
      #"Expanded {0}" = Table.ExpandRecordColumn(#"aaaa", 
      "Column1", {"id", "name", "type"}, {"id", "name", "type"}),
      #"Expanded {0}1" = Table.ExpandListColumn(#"Expanded {0}", "type")
      in
          #"Expanded {0}1"
else
      #"Expanded {0}" = Table.ExpandRecordColumn(#"bbbb", 
      "Column1", {"id", "name", "type"}, {"id", "name", "type"}),
      #"Expanded {0}1" = Table.ExpandListColumn(#"Expanded {0}", "type")
      in
          #"Expanded {0}1"

is it possible somehow?  I seem it doesn't Smiley Frustrated

 

Thanks a lot in advance for any hint

 

 

Luca
1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Yes, you're already there: Just add some let-expressions. This is the pattern:

 

let
    myVariable = 1,
    Result = if myVariable = 1 
                then let 
                        DoSth = "DoSth", 
                        DoSthElse = "myVariable is 1"
                    in
                        DoSthElse
                else let
                        ElseDoSth = "Here is my else",
                        Else2DoSth = "Here is my other else"
                    in
                        Else2DoSth
in
    Result

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

1 REPLY 1
ImkeF
Super User
Super User

Yes, you're already there: Just add some let-expressions. This is the pattern:

 

let
    myVariable = 1,
    Result = if myVariable = 1 
                then let 
                        DoSth = "DoSth", 
                        DoSthElse = "myVariable is 1"
                    in
                        DoSthElse
                else let
                        ElseDoSth = "Here is my else",
                        Else2DoSth = "Here is my other else"
                    in
                        Else2DoSth
in
    Result

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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