Date fields in data expressions for serial charts

1474
4
Jump to solution
01-20-2022 07:26 PM
Labels (1)
ChrisH2
New Contributor III

Seeking some assistance with what i think is just a formatting problem.

I've used this script in a data expression to power a serial chart showing the time of day in 24hr format for a dataset collected in ArcGIS Collector. The original reason for doing this as a data expression was to gain access to a field that just had the time of day (or rounded up to an hour value from 1 - 24)

Everything works well at this stage

What I'm trying to do now is include the original creation_date field in the featureset so that I can filter this field with a date category selector. As it is now, the creation date field is being treated as a string and when I attempt to apply a filter a this data expression via the date category selector, there are no valid fields to choose from.

If I change line 18 of the code from esriFieldTypeString to esriFieldTypeDate, the featureset doesn't return any records.

The creation_date field is actually of type esriFieldTypeDate, so the problem must be with my scripting. Any suggestions are most welcome 

Untitled.png

//ground activity - time of day
//obtains the hour in military (long) format from the creation_date field

// Write an expression that returns a FeatureSet.
// Documentation: https://arcg.is/3c419TD
// Samples: https://arcg.is/38SEWWz



// Create a FeatureSet from the Feature Layer containing the Ground activitydata.
var fs =FeatureSetByPortalItem(Portal('https://gis.xxxxxx.xx.xx.xx/portal'), '0116ef7ee89c4de0829fe554315e2177', 0)



var dowDict = { 
  'fields': [{ 'name': 'dow_num', 'type': 'esriFieldTypeInteger'},
  {'name': 'OBJECTID','type': 'esriFieldTypeInteger'},
  {'name': 'creation_date','type': 'esriFieldTypeString'},
  {'name': 'militaryhr', 'type': 'esriFieldTypeString'},
  {'name': 'Shooter_Organisation','type': 'esriFieldTypeString'},
  {'name': 'species', 'type': 'esriFieldTypeString'}], 
  'geometryType': '', 
  'features': [] 
}; 

var index = 0; 

for (var feature in fs) { 
    dowDict.features[index] = { 
        'attributes': { 
            'dow_num': Hour(feature['creation_date']), 
            'objectid': feature['OBJECTID'],
            'creation_date': feature['creation_date'],
            'militaryhr': Text(feature['creation_date'], 'HH'),
            'Shooter_Organisation': feature['Shooter_Organisation'],
            'species': feature['species_common_name'] 
        }} 
    index++;} 

// Convert dictionary to feature set. 
//var fs_dict = FeatureSet(Text(dowDict)); 
var fs_dict = FeatureSet(Text(dowDict)); 

// Return case data by day of Hour
return fs_dict;

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

I've seen this sort of behavior in Data Expressions with date fields.

I don't understand why this works, but try casting it as a Number. In line 33, you'd put:

'creation_date': Number(feature['creation_date']),

Trying to use a date value directly.Trying to use a date value directly.

Cast as number.Cast as number.

 

- Josh Carlson
Kendall County GIS

View solution in original post

4 Replies
jcarlson
MVP Esteemed Contributor

I've seen this sort of behavior in Data Expressions with date fields.

I don't understand why this works, but try casting it as a Number. In line 33, you'd put:

'creation_date': Number(feature['creation_date']),

Trying to use a date value directly.Trying to use a date value directly.

Cast as number.Cast as number.

 

- Josh Carlson
Kendall County GIS
TimWarfe_MSC
New Contributor II

I was stuck with the exact situation too.   Thank you, that worked perfectly for me also.  🙂

0 Kudos
ChrisH2
New Contributor III

Worked perfectly. Thankyou so much @jcarlson 

0 Kudos
DavidNyenhuis1
Esri Contributor

With this week's update to Arcade and ArcGIS Dashboards, date fields in feature set constructors now just work. You no longer have to wrap dates with Number() if you pass the dictionary into the FeatureSet() function (which as of this release accepts a dictionary as opposed to only text based JSON).

Instead of:

return FeatureSet(Text(dict))

Do this:

return FeatureSet(dict)

Learn more in this blog post

NOTE: For Enterprise users, this update is targeted for 11.2

0 Kudos