Forum Discussion

samc_26's avatar
samc_26
Helper IV
2 years ago
Solved

Multiple If statements using columns

Hi, I've been researching this all day and had no success so thought I would try here! I have a table which shows values and then three sets of targets to which I want to do some kind of if else stat...
  • sevenhills's avatar
    2 years ago

    I believe you need is switch statement.

     

    Check this link for more details: https://www.mssqltips.com/sqlservertip/7574/dax-case-statement-dax-if-switch/

    and search for text "DAX SWITCH(TRUE())" and read from there.

     

    I tried to understand and came with below syntax. Not sure whether it works correctly or not, give a try...

     

    Your Column = 
    SWITCH (
        TRUE (),
        Table1[Value] < Table1[Target], "Orange",
        Table1[Value] >= Table1[Target] && Table1[Value] < Table1[High], "Green",
        Table1[Value] >= Table1[High] && Table1[Value] < Table1[Purple], "Red",
        Table1[Value] >= Table1[Purple], "Purple",
        "0"
    )

     

    Output:

     

     

     

  • AlexisOlson's avatar
    2 years ago

    A SWITCH is often neater than nested IFs. 

     

    I think you could write it like this:

     

    SWITCH (
        TRUE (),
        T[Value] < T[Target], "Orange",
        T[Value] < T[High], "Green",
        T[Value] < T[Purple], "Red",
        "Purple"
    )

     

    (Replace "T" with whatever your table name is.)

    Edit: It looks like sevenhills got there first. The only difference with mine is that I've eliminated the redundant conditions. For example, if you get past the "Orange" line, we already know that T[Value] >= T[Target], so we don't need to recheck it.