Select to view content in your preferred language

Date Diff of Feature and Related Table

660
6
08-18-2022 08:04 AM
JasonCyphers
Frequent Contributor

How can I calculate the difference, in days, between a date on a hosted feature layer and a date on a related table record?

I have a report point, that captures information about a repair that needs to be made, and a related table that captures information about the repairs that were made.  I'd like to be able to use arcade to determine how many days lapsed between the point being added and the point being repaired.

0 Kudos
6 Replies
jcarlson
MVP Esteemed Contributor

Should be pretty straightforward. With related tables, you just need some way to sort / filter the table, to make sure that you're getting the right record, then just pull out its particular date field.

var repairs = FeatureSetByRelationshipName($feature,"relationshipname")

var recent_repair = First(OrderBy(repairs, 'created_date DESC'))

var repair_date = recent_repair['created_date']

var report_date = $feature['created_date']

return DateDiff(repair_date, report_date, 'days')
- Josh Carlson
Kendall County GIS
0 Kudos
JasonCyphers
Frequent Contributor

So, if I created an attribute on the point feature called "days_to_repair", I could use the above script in Pro to calculate the values?

0 Kudos
jcarlson
MVP Esteemed Contributor

Yes, I imagine so.

- Josh Carlson
Kendall County GIS
0 Kudos
JasonCyphers
Frequent Contributor

I tried... no luck 😞

JasonCyphers_0-1660909544476.png

Script works great in a Portal map pop-up, to display the # of days to repair (I was even able to include some code to display the duration in hours if it was less than a day), so not sure why it won't populate the field in Pro.

0 Kudos
jcarlson
MVP Esteemed Contributor

Hm. I would guess it's encountering a null value somewhere. Let's throw in some code to check for values first.

var repairs = FeatureSetByRelationshipName($feature, 'report_repairs')

// check for existence of values in related table first

If (Count(repairs) == 0){
    Console('No related records!')
    return null
} else {
    var recent_repair = First(OrderBy(repairs, 'created_date DESC'))
    // put the rest of the code in these brackets
}
- Josh Carlson
Kendall County GIS
0 Kudos
JasonCyphers
Frequent Contributor

Thanks for the help.  That still didn't seem to work though. 🤔

JasonCyphers_0-1660927244149.png

 

0 Kudos