Select to view content in your preferred language

Arcade Expression For Selecting Future Dates

1626
8
Jump to solution
02-11-2022 02:20 AM
map16jamie
Occasional Contributor

Hi there, does anyone have a method of filtering a layer that shows features containing a date field which are within the next 'x' days. I know that there is already a date filter which can filter features within the last 'x' days but not within the next.

For example the layer has a field containing future dates and I would like only features to be selected by this field if it is within x days of todays date.

IDFruitDate
1Apple13/02/2022
2Pear14/02/2022

 

Any help would be much appreciated 

0 Kudos
2 Solutions

Accepted Solutions
HuubZwart
Frequent Contributor

I don't think this is possible using the regular layer filter in the webmap, but you can tweak the definition expression for the required layer in the web map using ArcGIS Online Assistant (or update the web map via REST interface). You can use any supported SQL statement here such as 

DateField BETWEEN CURRENT_DATE() AND CURRENT_DATE() + x

 

Be aware this makes it impossible to further edit the expression in the webmap interface, you will get an error there if you try to.

View solution in original post

0 Kudos
JohannesLindner
MVP Frequent Contributor

Ah. In that case:

  • go to the layer's style settings
    JohannesLindner_1-1644828124623.png
  • Choose "New Expression" as symbol field
    JohannesLindner_2-1644828221502.png
  • edit and use this expression (it will return true or false):
var start_date = Today()
var end_date = DateAdd(Today(), 5, "days")
var test_date = $feature.DateField

return test_date >= start_date && test_date <= end_date
  •  Choose "Distinct Values" as styling option
  • Style the symbols for true and false
    JohannesLindner_3-1644828672667.png
  • You can also hide the false features by setting the transparency to 100%.

 

 Before:

JohannesLindner_0-1644828065361.png

 

After applying the Arcade expression:

JohannesLindner_4-1644828879232.png

 

After hiding the false features:

JohannesLindner_5-1644828931087.png

 

 

Note that this approach will probably be slower than just filtering out the undesired features from the layer (see HuubZwart's answer).


Have a great day!
Johannes

View solution in original post

0 Kudos
8 Replies
JohannesLindner
MVP Frequent Contributor
var start_date = Text(Today(), "Y-MM-DD")   // Today: 2022-02-11
var end_date = Text(DateAdd(Today(), 5, "days"), "Y-MM-DD")  // Today +5 days: 2022-02-16 

var fs = FeatureSetBy*(...)
var future_records = Filter(fs, "DateField BETWEEN @start_date and @end_date")




Have a great day!
Johannes
0 Kudos
map16jamie
Occasional Contributor

Hi Johannes,

Thank you for your reply, I forgot to mention I was trying to do this in Mapviewer. I tried the expression above but the function 'Filter' cannot be used within the symbology.

0 Kudos
JohannesLindner
MVP Frequent Contributor

Ah. In that case:

  • go to the layer's style settings
    JohannesLindner_1-1644828124623.png
  • Choose "New Expression" as symbol field
    JohannesLindner_2-1644828221502.png
  • edit and use this expression (it will return true or false):
var start_date = Today()
var end_date = DateAdd(Today(), 5, "days")
var test_date = $feature.DateField

return test_date >= start_date && test_date <= end_date
  •  Choose "Distinct Values" as styling option
  • Style the symbols for true and false
    JohannesLindner_3-1644828672667.png
  • You can also hide the false features by setting the transparency to 100%.

 

 Before:

JohannesLindner_0-1644828065361.png

 

After applying the Arcade expression:

JohannesLindner_4-1644828879232.png

 

After hiding the false features:

JohannesLindner_5-1644828931087.png

 

 

Note that this approach will probably be slower than just filtering out the undesired features from the layer (see HuubZwart's answer).


Have a great day!
Johannes
0 Kudos
map16jamie
Occasional Contributor

Thank you Johannes! 

0 Kudos
HuubZwart
Frequent Contributor

I don't think this is possible using the regular layer filter in the webmap, but you can tweak the definition expression for the required layer in the web map using ArcGIS Online Assistant (or update the web map via REST interface). You can use any supported SQL statement here such as 

DateField BETWEEN CURRENT_DATE() AND CURRENT_DATE() + x

 

Be aware this makes it impossible to further edit the expression in the webmap interface, you will get an error there if you try to.

0 Kudos
map16jamie
Occasional Contributor

Thank you HubbZwart, This has worked 🙂

0 Kudos
map16jamie
Occasional Contributor

Hi HuubZwart,

Thank you for this. Having tried this it works when viewing on Mapviewer but when trying to load this map on Field Maps it does not load the layer at all. Does this method work on Field Maps?

0 Kudos
skunkpup
Emerging Contributor

It helped me a lot. Thanks.

fireboy and watergirl

0 Kudos