How do I pull the most recent date with my Arcade expression

5837
8
01-15-2019 07:05 AM
by Anonymous User
Not applicable

One problem that I am having with my Arcade script is trying to pull the most recent spill date when clicking on the districts. This is what I have so far:

 

var intersectLayer =Intersects(FeatureSetByName($map,"Spill Service Requests"), $feature)

for (var f in intersectLayer){
return Max(f.SR_Date_Time)
}

I get a result of NaN and Type 'Number'. How could I get the result in the correct format and bring back the date, and possibly other fields for that record. Any help is greatly appreciated. Thanks

0 Kudos
8 Replies
XanderBakker
Esri Esteemed Contributor

Did you try what I answered here: https://community.esri.com/docs/DOC-12773-using-featuresetby-functions-in-arcade-to-drill-down-to-ot... ?

var intersectLayer =Intersects(FeatureSetByName($map,"Spill Service Requests"), $feature);
var max_date = Date(1900, 0, 1);

for (var f in intersectLayer){
    if (f.SR_Date_Time > max_date) {
        max_date = f.SR_Date_Time;
    }
}

return max_date;
XanderBakker
Esri Esteemed Contributor

Taking into account the recommendations made by  PBarker-esristaff  here: What’s new with Arcade: Taking a stroll through FeatureSets Part 2   

Key things to remember
I happen to like lists, so here's a list to help summarize what we’ve learned into a handy set of tips:

  • Define the minimum needed FeatureSets required to do the job
  • Always reduce your FeatureSets when you can
  • Filtering by attributes will always be faster than filtering by geometry
  • Chain functions together when possible to reduce queries
  • Use stats and sorting to avoid unnecessary loops
  • Review Part 1 of this blog series

I wonder if this expression should be changed to:

return Top(OrderBy(Intersects(FeatureSetByName($map,"Spill Service Requests"), $feature), 'SR_Date_Time DESC'), 1);‍‍
0 Kudos
NaomiBegg2
Occasional Contributor III

Hi Xander Bakker‌ 

I've tried this Top function for a popup. 

I get results within the attribute expression builder test.

I have the displaying a description from one field (the expression).

However the popup is showing no information available.  Because it is returning as a table, does this need to be inputted into the popup in a different way?  Or do I need to be calling out the couple of fields I am actually interested in from the table?

0 Kudos
UriGilad_EsriAu
Esri Contributor

Hi Collin & Naomi,

Try this solution to get latest date from a layer:

var LatestDate = Max($layer, "Your_Date_Field")
var FormatDate = Date(LatestDate)
return FormatDate

And to get the latest date from all features intersecting a polygon:

var intersectLayer = Intersects(FeatureSetByName($map,"you_point_layer"), $feature);
var LatestDate = Max(intersectLayer, "Your_Date_field")
var FormatDate = Date(LatestDate)
return FormatDate

0 Kudos
NaomiBegg2
Occasional Contributor III

Hi Uri Gilad

I've tried your script. 

I had to add in layer = Intersect(.....)  as I'm interested in events that have happened within an area.

However my date field is the default CreationDate and I have an error saying that it cannot call member property on this type.

I've successfully managed to use the first suggested solution comparing max_date.  I am now interested in being able to show Top 5 results using the top function.  However I am yet to get this to work in a pretty manner of inserting the whole table into the popup or being able to put only selected fields in (and potentially later filtering by a field).  I have managed to list the top 5 activities and dates, but I did not get these to be nicely beside each other (date: activity, new line ...) rather a list of all the activities and a list of all the dates.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Naomi Begg ,

In your screenshot (which unfortunately only shows part of the expression used) you notice that the return type is a featureset and a featureset cannot be visualized in the pop-up. To create a readable result, you will have to loop through the featureset and create a multiline string with the information of the features that you want to show. 

Something like this:

var fs = Top(OrderBy(Intersects(FeatureSetByName(...

var result = "";
for (var f in fs) {
    if (result == "") {
        result = Text(f.CreationDate, ' - (Y/MM/DD) ') + ": " + f.Activity;
    } else {
        result += TextFormatting.NewLine + Text(f.CreationDate, ' - (Y/MM/DD) ') + ": " + f.Activity;
    }
}

return result;
NaomiBegg2
Occasional Contributor III

Awesome, that has worked.

I had started working on a for loop yesterday but I was getting confused by the date so its great to see how you converted it to Text.  My alternative I was working on was using the YEAR(),MONTH(),DAY() functions however I found it very weird that they decided to go with IT speak rather than common language and put January as month 0 and December as month 11 (today is 05/02/2020 not 05/01/2020!!). 

0 Kudos
NaomiBegg2
Occasional Contributor III

Hi Xander Bakker

Can you help me take this one step further? 

From my list of actions I want to only identify the actions that are "Completed",  from this I want to find use the most recent completed actions date.

My below script currently works as we have only just started collecting data so we don't have multiple completed dates.

var a = Top(OrderBy(Intersects(FeatureSetByName($map,"All Actions"), Buffer($feature,20, 'meters')), 'CreationDate DESC'), 10);
var action = ''
var result = ''
var ndate = (1900/01/01)
for (var b in a){
 if (b.Ranger_Action == "Completed"){
 action = b.Ranger_Action + b.CreationDate
 ndate = DateADD(b.CreationDate, 21, 'days') 
 result = " Next mow due" + Text(ndate, ' Y/MM/DD ') 
 }else{
 // result = "Not regularly mowed"
 
 }
}
return result‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I've tried adding in some if statements and a test date to look at if it is the most recent completed date.  However this script isn't working yet and I assume there is a must simpler way of doing this.

var a = Top(OrderBy(Intersects(FeatureSetByName($map,"All Actions"), Buffer($feature,20, 'meters')), 'CreationDate DESC'), 10);
var action = ''
var result = ''
var ndate = (1900/01/01)
var testdate = (1900/01/01)

for (var b in a){
    if (b.Ranger_Action == "Completed" AND ndate == (1900/01/01)){
        ndate = DateADD(b.CreationDate, 21, 'days') 
        result = " Next mow due" + Text(ndate, ' Y/MM/DD ')  
    } else if (b.Ranger_Action == "Completed"){
                testdate = b.CreationDate;
                if (b.testdate < ndate){
                action = b.Ranger_Action + b.CreationDate
                ndate = DateADD(b.CreationDate, 21, 'days') 
                result = " Next mow due" + Text(ndate, ' Y/MM/DD ')  
                }else {}
        
    }else{
       // result = "Not regularly mowed"
        
    }
}

return result‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

0 Kudos