Dynamic Date Filter with Arcade

7188
8
08-13-2019 01:04 PM

Dynamic Date Filter with Arcade

Users have the need to apply a dynamic date filter to a feature layer within their web map .  For example, they may need to show all features that were edited on the current day.  Instead of having to manually update the filter in the web map each day, the below steps will walk you through how to display features dynamically using a custom style Arcade expression.

 

Steps:

1.  Within your web map, click the Change Style icon:

2.  For the option Choose an attribute to show select Custom (Expression):

 

3.  Add in the following Arcade expression:

var time = -1
var units = 'days'  //milliseconds, seconds, minutes, hours, days, months, years
var dateField = "last_edited_date"

var pastDate = DateAdd(Date(), time, units)
if($feature[dateField] > pastDate){
    return 'Open'
}

Change the first three variables to whatever values you would like.  The supported units are milliseconds, seconds, minutes, hours, days, months, years.  The dateField variable is the date field (i.e. last_edited_date) you will query.  The above example is filtering all features that have been edited after yesterday.  You can also specify anything you would like for the return.  This will show for the symbology's legend:

 

4.  Finally set a Refresh Interval so that the features will update dynamically:

Comments

Hi Jake - thanks for the tip.  Here is what I REALLY NEED to figure out soon!  How do I filter a layer in a web map to show only the most recent feature in the layer?  For example, think of a "track log" of locations - and then the "last known location" of the tracklog.  How would I filter the layer to only display the last known location?  I know how to depict locations in the last few days or hours, but I really only want the very last location.  Any ideas?  Ultimately, I'd like to be able to filter out the last known locations for each unique individual within a dataset.  My real world example deals with collared animals.  The GPS collars don't always reliably upload at a regular, specified interval.  I will have many individual animals collared.  I want to display just the most recent for each collar.

Thanks,

Ken

I don't think this will be possible via Arcade.  I would approach this via Python.  For example, you could have a script that queries the most recent features, sets a definition query on the layer, then publish/overwrite the service.  Unfortunately, I don't have an example of this.

Great post Jake Skinner‌, it's really helpful.

One of the small issues with this method is that - the features that aren't shown on the map are still selectable (even though they are invisible), and will produce a popup.

Kelly Gerrow - Could you please look into why features that aren't selected to be shown on webmaps produce this behaviour. THANKS!!!

But, you'll have the capability to use "in the last" and "not in the last" dynamic date expressions on webmap filters or when defining feature layer views (and get around this issue) 

The following expression will show features edited in the last day. Hope this helps.

Gee

Hi Gee,

Please report the issue through tech support.

-Kelly

The "in the last" and "not in the last" dynamic date expressions are not available if you are using a self-hosted feature service.  I'm trying to use this to filter out all reports in a citizen reporting layer that were reported that are older then 3 months so that past reports do not clog up my public facing map.  I'm not getting this to work yet, but I'm working on it!

As the above user stated: "One of the small issues with this method is that - the features that aren't shown on the map are still selectable (even though they are invisible), and will produce a popup."

I am trying to filter a map service with thousands of points, so almost every time I click on a part of the map a popup is still produced, even though most of the points are invisible.

The "in the last" and "not in the last" dynamic date expressions also do not work in the collector app.  Apparently it is a bug that I just found.

 

@KenMorefield1 

@JakeSkinner 

This can be achieved by creating a dynamic join layer in AGOL. You would end up with two layers on a map. I would change the symbology of the "most recent record" to be slightly larger and a different color. Then make sure the dynamic join is the topmost layer. Depending on functionality I would turn off popups on the dynamic layer also. 

Version history
Last update:
‎12-12-2021 03:48 AM
Updated by: