Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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

2 ACCEPTED SOLUTIONS
edhans
Super User
Super User

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"



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post


@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.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

3 REPLIES 3
edhans
Super User
Super User

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"



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
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.


@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.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.