Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

How to convert SQL case statement to dax

case
when (COALESCE(sum(ABC),0) - COALESCE(sum(ABCD),0)) < 0 then 0
else (COALESCE(sum(ABC),0) - COALESCE(sum(ABCD),0)) / sum(XYZ)

  • can you please show me the full measure how you wrote it

    or please try 

    Measure = 
    VAR result = SUM('Table'[ABC]) - SUM('Table'[ABCD])
    RETURN
    IF(
        result < 0,
        0,
        DIVIDE(
            result,
            SUM('Table'[XYZ]),
            0 
        )
    )

3 Replies

  • eliasayyy's avatar
    eliasayyy
    Memorable Member

    hello please try 

    IF(
        CALCULATE(SUM('Table'[ABC])) - CALCULATE(SUM('Table'[ABCD])) < 0,
        0,
        DIVIDE(
            CALCULATE(SUM('Table'[ABC])) - CALCULATE(SUM('Table'[ABCD])),
            SUM('Table'[XYZ])
        )
    )
    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you for the reply. Somehow the last bit which is

      SUM('Table'[XYZ])

      is actually a measure and DAX is not picking up with this syntax and also coming with this error 

      so incase if you can help me further thank you 

      • eliasayyy's avatar
        eliasayyy
        Memorable Member

        can you please show me the full measure how you wrote it

        or please try 

        Measure = 
        VAR result = SUM('Table'[ABC]) - SUM('Table'[ABCD])
        RETURN
        IF(
            result < 0,
            0,
            DIVIDE(
                result,
                SUM('Table'[XYZ]),
                0 
            )
        )