Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Case Statement to DAX

Hi

 

How can we create measure or column in power bi with case statement as below.

Select column 1,column2,

CASE
                     WHEN a.appt = 1
                           THEN 0
                     ELSE 1
                     END AS Column3

From table 1

where Condition  = 'stop'

 

Thanks in Advance

  • If there are just two conditions, use the IF() function. If there will be more, use the SWITCH() function. If you have multiple things to test for each condition, you can either use AND() for 2 items, or && for multiple items. For example:

     

    =IF([Item1] > 100 && [Item2] <> 1 && [Item3] = "Garage", Do this, else do that.)

    That will only "do this" if all 3 condtions are true. Otherwise it does the else part, or "else do that"

  • edhans's avatar
    edhans
    7 years ago

    Anonymous wrote:

    This is the error i am getting while using IF() function

    Column = IF(table1[appt]="1" && table1[condition]="stop" && table1[condition]="0",0,1)

     

    Errot: DAX comparison operations do not support comparing values of type Integer with values of type Text. Consider using the VALUE or FORMAT function to convert one of the values.


    If [appt] and [condition] are values, then you need to compare as values, not text. So try:

     

    Column = IF(table1[appt]=1 && table1[condition]="stop" && table1[condition]=0,0,1)

    Your comparison type needs to match the data type. Dates to dates, text to text, numbers to numbers, boolean/logical to boolean. DAX is very strict here. You cannot get away with comparing 1 to "1" and getting a match. Not only will it not match, it returns the error you got.

3 Replies

  • edhans's avatar
    edhans
    Community Champion

    If there are just two conditions, use the IF() function. If there will be more, use the SWITCH() function. If you have multiple things to test for each condition, you can either use AND() for 2 items, or && for multiple items. For example:

     

    =IF([Item1] > 100 && [Item2] <> 1 && [Item3] = "Garage", Do this, else do that.)

    That will only "do this" if all 3 condtions are true. Otherwise it does the else part, or "else do that"

    • Anonymous's avatar
      Anonymous
      Not applicable

      This is the error i am getting while using IF() function

      Column = IF(table1[appt]="1" && table1[condition]="stop" && table1[condition]="0",0,1)

       

      Errot: DAX comparison operations do not support comparing values of type Integer with values of type Text. Consider using the VALUE or FORMAT function to convert one of the values.

      • edhans's avatar
        edhans
        Community Champion

        Anonymous wrote:

        This is the error i am getting while using IF() function

        Column = IF(table1[appt]="1" && table1[condition]="stop" && table1[condition]="0",0,1)

         

        Errot: DAX comparison operations do not support comparing values of type Integer with values of type Text. Consider using the VALUE or FORMAT function to convert one of the values.


        If [appt] and [condition] are values, then you need to compare as values, not text. So try:

         

        Column = IF(table1[appt]=1 && table1[condition]="stop" && table1[condition]=0,0,1)

        Your comparison type needs to match the data type. Dates to dates, text to text, numbers to numbers, boolean/logical to boolean. DAX is very strict here. You cannot get away with comparing 1 to "1" and getting a match. Not only will it not match, it returns the error you got.