Forum Discussion

laciodrom_80's avatar
laciodrom_80
Helper IV
7 years ago
Solved

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 :smileyfrustrated:

 

Thanks a lot in advance for any hint

 

 

  • 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

1 Reply

  • ImkeF's avatar
    ImkeF
    Community Champion

    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