Select to view content in your preferred language

DateDiff Help

694
4
04-29-2022 09:10 AM
MaryEllenStRomain
New Contributor

Hey everyone!

I'm very new to Arcade, so bear with me. 

In my AGO map, I have a "Parks" point layer and it's related "Assessments" table. I am trying to calculate a field in the "Parks" layer to display the days since the last assessment, based on the date field in the "Assessments" table. 

I understand that I need to use the DateDiff function, but it seems to be having a problem finding the related table and date field I'm wanting to point to. 

The relationship is a 1:M, and I'd like to pull the most recent assessment date. I'm not sure if it's the syntax or the 1:M that is causing an error:

var startDate = Date();
var endDate = Date(InspectionDate);
var age = DateDiff(endDate, startDate, 'days');
return age;

Execution Error:Runtime Error: Identifier Not Found. InspectionDate

Any help would be much appreciated! Thanks!

0 Kudos
4 Replies
jcarlson
MVP Esteemed Contributor

To access an attribute, you need to put $feature. in front of the attribute name. You can also use bracket notation, like $feature['InspectionDate'] to pull in an attribute.

Without the $feature., the expression is looking for a variable called "InspectionDate".

- Josh Carlson
Kendall County GIS
0 Kudos
MaryEllenStRomain
New Contributor

Thanks Josh!

I did play around with that. When I use $feature['InspectionDate'], it returns a Runtime Error: Identifier Not Found. InspectionDate. I'm pulling the 'InspectionDate' from my list of Globals and all of the test data is filled out with dates. Do I need to do something special to indicate that the 'InspectionDate' is in the related table of the $feature?

Thanks for your help!

0 Kudos
jcarlson
MVP Esteemed Contributor

Oh jeez, I'm sorry. I went through your post way too quick and just focused on the code portion once I saw that it wasn't accessing a specific feature. More generally, you need to specify a feature to get its attributes, so it's not always $feature, but it needs to be a feature object.

As @KenBuja points out, you need to use another function to access related records.

In text:

  1. Get the related records
  2. Sort by date
  3. Grab the first record
  4. Pull out the date

In code:

// related records
var rel = FeatureSetByRelationshipName(
    $feature,
    'Inspections', // the name here will vary, see note below code snippet
    ['InspectionDate'],
    false
)

// get the latest one as a feature
var latest = First(
    OrderBy(
        rel,
        'InspectionDate DESC'
    )
)

// return the timestamp of the latest inspection
return latest['InspectionDate']

 

  For the relationship name, the expression builder can pipe that in for you, if you don't know it.

brave_N9xXA2sXyz.gif

- Josh Carlson
Kendall County GIS
0 Kudos
KenBuja
MVP Esteemed Contributor

Since you're working with a related table, you have to use FeatureSetByRelationshipName to get the values in that table. Here's an example (and the output):

var test = FeatureSetByRelationshipName($feature,"Source");
for (var f in test) {
    var output = f.Year;
    console(output)
}

year.png