Select to view content in your preferred language

Displaying symbology dynamically based on date Expression Builder

155
2
Jump to solution
2 weeks ago
OmoOyaks
New Contributor

I am fairly new to Arcade and having trouble understanding why the symbology expression builder to display the date attribute isn't working. I created an Arcade expression to display attributes based on their expiry date attribute.

Feature name - Defib, Feature attribute - Expdate
It does seem to display the different if cases, but the dates displayed are all wrong. 

For example, a map feature is displayed as expired but the actual expiry date is in 7 months. The date format is mm/dd/yyyy

See attached expression, I am at a loss what the problem is.

----------------------------------------------------------------------------------------------------

if (DateDiff(Now(), Date($feature.Expdate), 'days') <= 0) {
return "Expired" }
else if (DateDiff(Now(), Date($feature.Expdate), 'days') <= 30) {
return "Expires within 30 days" }
else if (DateDiff(Now(), Date($feature.Expdate), 'days') > 30 &&
DateDiff(Now(), Date($feature.Expdate), 'days') < 100) {
return "Expires between 30 to 100 days" }
else if (DateDiff(Now(), Date($feature.Expdate), 'days') >= 100) {
return "Expires in more than 100 days" }
else {
return "Expiry Date Unavaliable" }

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You have the order of the DateDiff parameters reversed if the ExpDate is in the future. For those dates, your expression returns a negative number.

Here's an easier way to write your expression, using the When function. This assumes that the ExpDate field is a date field.

if (IsEmpty($feature.Expdate) return "Expiry Date Unavailable"
var diff = DateDiff($feature.Expdate, Now(), "days");
When(diff <= 0, "Expired",
     diff <= 30, "Expires within 30 days",
     diff < 100, "Expires between 30 to 100 days",
     "Expires in more than 100 days"
);

 

View solution in original post

2 Replies
KenBuja
MVP Esteemed Contributor

You have the order of the DateDiff parameters reversed if the ExpDate is in the future. For those dates, your expression returns a negative number.

Here's an easier way to write your expression, using the When function. This assumes that the ExpDate field is a date field.

if (IsEmpty($feature.Expdate) return "Expiry Date Unavailable"
var diff = DateDiff($feature.Expdate, Now(), "days");
When(diff <= 0, "Expired",
     diff <= 30, "Expires within 30 days",
     diff < 100, "Expires between 30 to 100 days",
     "Expires in more than 100 days"
);

 

OmoOyaks
New Contributor

Thank you, that worked. 

0 Kudos