Forum Discussion

Applicable88's avatar
Applicable88
Icon for Impactful Individual rankImpactful Individual
5 years ago
Solved

Can several nested if-statements in parenthesis can be evaluated in a superior? (Power Query -M) an

 

 

I have question regarding M-Language to make a calculated column.

Can this structure be evaluated with a double nested If-statement be evaluated?:

if  [Column100] = A then (if [Column1]= X  and [Column2]=Y then "First" else if [Column1]= Z or  [Column2]=b then "Second") else

if [Column200] =B then  (if [Column1]= X  and [Column2]=Y then "Three" else if [Column1]= Z or  [Column2]=b then "Fourth") else "FIVE"

Thanks in advance. 

 

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Applicable88 

     

    Yes, SWITCH is a DAX function, I don't think there is one in M now, but you can write one. I have this one used in different cases, modify accordingly

    fnSwitch = (input as text) => [
            values = {
            {"China", "China"},
            {"United Kingdom", "UK"},
            {"Japan", "Japan"},
            {"Turkey", "Turkey"},
            {input, "Generic"}
            },
            Result = List.First(List.Select(values, each _{0}=input)){1}][Result]

     

6 Replies

  • Yes, you can have nested if statements in M. Or were you asking if there's a better way to write this particular logical statement?

    • Applicable88's avatar
      Applicable88
      Icon for Impactful Individual rankImpactful Individual

      Hi AlexisOlson , thanks for clarifying.  Nested if's in Nested if's getting quite long. I would like to know more about a alternative way. 

      Best.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Applicable88 

         

        Sometimes it can't avoid long IFs as the logic is there, I probably will write like this...not saying it is better, just for your reference

        if List.AllTrue({ [Column100] = A, [Column1]= X,[Column2]=Y }) then "First" 
        else if [Column100] = A and List.AnyTrue({[Column1]= Z,  [Column2]=b})  then "Second" 
        else if List.AllTrue({ [Column100] = B, [Column1]= X,[Column2]=Y }) then "Three" 
        else if [Column100] = B and List.AnyTrue({[Column1]= Z,  [Column2]=b}) then "Fourth"
        else "FIVE"