How to use Arcade Expression to dynamically change layer symbol based on time attribute

2471
9
Jump to solution
05-07-2019 09:44 AM
DamonNelton
Occasional Contributor

I have a point layer used for work permits. When the field crew pulls a new work permit they create a point via collector and complete attribute fields to document date permit was pulled and date permit will expire. I would like to have the ability to automate the change in symbology based on "time/date". Example, if permit is good for 14 days I want the symbol to be a green circle for the first 9 days and then change to an orange circle from days 10-14 and then change to a red circle days >14.

Any help is appreciated. 

0 Kudos
1 Solution

Accepted Solutions
DamonNelton
Occasional Contributor

Thanks, I was able to get that to work. Below is how I tweaked my use case if it helps anyone else who reads this.

var days = Floor(DateDiff($feature["expired_on"], Today(), "days"),0) //Replace "expiration_date" with your field name

if (days > 4){
return 'Beyond 4 Days';
}
else if (days <= 4 && days >= 1){
return '1-4 Days';
}
else if (days < 1){
return 'Expired';
}

View solution in original post

9 Replies
XanderBakker
Esri Esteemed Contributor

This will not be visible in Collector, but in a web map you can use symbology based on the time passed. See example below:

// I assume you register the date the permit was created
var date_permit_created = $feature.LASTUPDATE; // change this for your date field
var days_dif = DateDiff(Now(), date_permit_created, "days");

var result = "Unknown";
if (days_dif <= 9) {
    result = "Green";
} else if (days_dif <= 14) {
    result = "Orange";
} else {
    result = "Red";
}

return result;

There is a catch... you will have to have some dummy data in order to define the different symbology classes. After defining the symbology the points could be removed from the layer. 

DamonNelton
Occasional Contributor

Thanks, I was able to get that to work. Below is how I tweaked my use case if it helps anyone else who reads this.

var days = Floor(DateDiff($feature["expired_on"], Today(), "days"),0) //Replace "expiration_date" with your field name

if (days > 4){
return 'Beyond 4 Days';
}
else if (days <= 4 && days >= 1){
return '1-4 Days';
}
else if (days < 1){
return 'Expired';
}

XanderBakker
Esri Esteemed Contributor

Great to see that it you have it working. Can you mark the question as solved?

0 Kudos
andresguglielmetti
New Contributor II

Hi, I would like to ask you how to automattically update the Today() field without calculating it manually each day. I want to automatically display how many days have passed since one date in the past. Thanxs!

0 Kudos
XanderBakker
Esri Esteemed Contributor

You can define your symbology on this Today() function and it will draw dynamically changing every day. You can also define the expression in the pop-up to show the updated information automatically. Only if you perform a field calculation you will create static information that will not update based on the current  day if the day is after the day for which the calculation was performed. What use case do you have?

andresguglielmetti
New Contributor II

thanks. I was wondering if "today" function updates automatically and the fields that depends on its calculation updates their values dynamically. I have one DATATIME field taken from Survey123 inspections, then in webmap I added a field with "now()" and "DateDifference" in hours. So, I want to establish an alarm performing dynamically when this difference is larger than 48 hours, so the webmap viewer can observe which survey is time exceeded. I had concerns about  this date difference. I figured out that its dynamically calculated so the viewer doesnt have to perform any calculation.

now I would like to know about frequency does it refresh. Does it refresh when you refresh your webmap? or when you pan in the map?

I founded that the following code worked fine:

var startDate = Date($feature.DATATIME);
var endDate = Date(now());
var age = Round(DateDiff(endDate, startDate, 'hours'));
var estado = IIf (age>=48 , 'time exceeded ' , 'no time exceeded');
return estado

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi ANDRES_GUGLIELMETTI , 

I think that every action that will invoke a request to the server should apply the expression to it. So zoom in and zoom out should work and if you pan away and back, that might too. 

As a little side note, you don't have to apply the Date() function if the DATATIME field is of type Date (same goes for the Now() function)

andresguglielmetti
New Contributor II

thanx for your time!

0 Kudos
FrancisHourigan1
Occasional Contributor

I have a similar question: Let's say that I have a date field that shows a future date like March 21, 2022, and I want to see the symbology for any dates before this date to be green but once this date is reached then the polygon turns red. Essentially future dates are green and past dates are red. Is this a similar Arcade script or is there a more simple way to go about this? 

Thanks!

0 Kudos