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
PC2022
Frequent Visitor

Nested IF - dates

Hello,

I need to create DAX for date KPI but i am practicing in excel first, so i could create formula in Power BI later.

 

Green - today is greater than 45 day period leading up to the finish date (please note that it should be 45 day time period, not just one date 45 days prior)

Yellow - today is within 45 days period leading up to the finish date (same note as above, it should be if today falls onto any date within 45 days time period).

Red - today is past finish date

 

My formula below doesn't seem to be right.

PC2022_2-1655732263086.png

 

 

 

2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@PC2022 , Create a new column like 

 

=

var _diff = datediff([finish date], today(), day)

return

Switch(True(),

_diff >=45 , "Green",

_diff > 0 , "Yellow",

"Red"

)

 

you can use this column in conditional formatting using the field value option

View solution in original post

Hi @PC2022,

In fact, you can use one calculated column to achieve these, you only need to add a variable to store the first step result and use the switch function to check the diff and return color code.

Color code =
VAR _diff =
    DATEDIFF ( [finish date], TODAY (), DAY )
RETURN
    SWITCH ( TRUE (), _diff >= 45, "Green", _diff > 0, "Yellow", "Red" )

After these steps, you can right-click on your field that needs to be formatted with 'field value' mode based on the above 'color code' column field values.

Apply conditional table formatting in Power BI - Power BI | Microsoft Docs

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

3 REPLIES 3
amitchandak
Super User
Super User

@PC2022 , Create a new column like 

 

=

var _diff = datediff([finish date], today(), day)

return

Switch(True(),

_diff >=45 , "Green",

_diff > 0 , "Yellow",

"Red"

)

 

you can use this column in conditional formatting using the field value option

Thank you! Sorry, I am new to Power BI and a little confused on your answer:

Do you mean to create 2 columns?

 

1) 

PC2022_2-1655736694028.png

 

2)

PC2022_0-1655735144426.png

 

Hi @PC2022,

In fact, you can use one calculated column to achieve these, you only need to add a variable to store the first step result and use the switch function to check the diff and return color code.

Color code =
VAR _diff =
    DATEDIFF ( [finish date], TODAY (), DAY )
RETURN
    SWITCH ( TRUE (), _diff >= 45, "Green", _diff > 0, "Yellow", "Red" )

After these steps, you can right-click on your field that needs to be formatted with 'field value' mode based on the above 'color code' column field values.

Apply conditional table formatting in Power BI - Power BI | Microsoft Docs

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

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.

Top Solution Authors