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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

DAX Nested IF with AND

I wish to add column in the query based on flag in the incoming data.
To achieve is:
based on flag value :

if flag < 1, then Red
else if flag is >0 and flag < 3 then yellow
else green

I tried to do it with DAX function as below : ([D] is my flag columns

 

IF (AND([D] >0,[D]<3),"Yellow",IF([D] < 1,"Red","Green"))

 

Error:

 

DAX If Error.jpg

 

Tried Switch function too:

 

= SWITCH(TRUE(),
   [D] <= 0, "Red",
   AND([D] >0, [D] < 3), "Yellow",
   "Green")

 

Error:

 

Switch.jpg

1 ACCEPTED SOLUTION
PattemManohar
Community Champion
Community Champion

@Anonymous In Power Query, make sure you are following case-sensitive rules. As M-code is case-sensitive and IF is not same as if.

 

Please change the logic in Power Query as below

 

= Table.AddColumn(#"Added Index1", "Custom", each if [Flag]<1 then "Red" else if [Flag]>0 and [Flag]<3 then "Yellow" else "Green")

In DAX, it will be

 

IF(Test154[Flag]<1,"Red",IF(AND(Test154[Flag]>0,Test154[Flag]<3),"Yellow","Green"))




Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




View solution in original post

2 REPLIES 2
PattemManohar
Community Champion
Community Champion

@Anonymous In Power Query, make sure you are following case-sensitive rules. As M-code is case-sensitive and IF is not same as if.

 

Please change the logic in Power Query as below

 

= Table.AddColumn(#"Added Index1", "Custom", each if [Flag]<1 then "Red" else if [Flag]>0 and [Flag]<3 then "Yellow" else "Green")

In DAX, it will be

 

IF(Test154[Flag]<1,"Red",IF(AND(Test154[Flag]>0,Test154[Flag]<3),"Yellow","Green"))




Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




Anonymous
Not applicable

Ola..!! It workd for me. Thanks

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.