Select to view content in your preferred language

Pass values to new columns Arcade

2134
4
Jump to solution
10-25-2021 03:09 AM
VeronicaArambula
Emerging Contributor

Hello,

I'm trying to pass field values to different columns, and also remove the time in the date (like in the image below). I wrote a piece of code but it doesn't work.

VeronicaArambula_0-1635156307937.png

 

 

// Reference layer using the FeatureSetByPortalItem() method. 
var fs = FeatureSetByPortalItem(Portal('https://arcgis.com/'), '74d6c3d630c14458a3c23c71a19447ad', 0, ['*'], false);

// Empty dictionary to capture each value 
var choicesDict = {'fields': [{ 'name': 'ID', 'type': 'esriFieldTypeString'}, 
                              { 'name': 'dates', 'type': 'esriFieldTypeDate'},
                              { 'name': 'morning', 'type': 'esriFieldTypeString'},
                              { 'name': 'afternoon', 'type': 'esriFieldTypeString'},
                              { 'name': 'night', 'type': 'esriFieldTypeString'}], 
                    'geometryType': '', 
                    'features': []}; 

// Store values in the dictionary
for (var feature in fs) {
    var id = feature['ID']
    var actdate = feature['actionDate']
    var morning = feature['Shift_vehicles'] == 'Morning check (6-7am)'
    var afternoon = feature['Shift_vehicles'] == 'Afternoon check (2-4pm)'
    var night = feature['Shift_vehicles'] == 'Night check (11pm-1am)'
    Push(choicesDict.features, {"attributes": {"ID": id, 
                                               "dates": actdate,
                                               "morning": morning,
                                               "afternoon": afternoon,
                                               "night": night}})
} 

//return choicesDict

// Convert dictionary to featureSet. 
var fs_dict = FeatureSet(Text(choicesDict)); 
return fs_dict
// Return featureset  
return GroupBy(fs_dict, ['ID', 'dates'],
       [{ name: 'morning', expression: 'morning', statistic: 'COUNT' },
        { name: 'afternoon', expression: 'afternoon', statistic: 'COUNT' },
        { name: 'night', expression: 'night', statistic: 'COUNT' }]); 

 

 

 

 

 

 

 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
SzuNyiapTang
Esri Contributor

Hi @VeronicaArambula 

I believe it is due to the date formatting - expecting in UNIX timestamp.

Also helped to modify the date (remove time) and logic for counting if false (changed to null)

 

// Reference layer using the FeatureSetByPortalItem() method. 
var fs = FeatureSetByPortalItem(Portal('https://arcgis.com/'), '74d6c3d630c14458a3c23c71a19447ad', 0, ['*'], false);

// Empty dictionary to capture each value 
var choicesDict = {'fields': [{ 'name': 'ID', 'type': 'esriFieldTypeString'}, 
                              { 'name': 'dates', 'type': 'esriFieldTypeDate'},
                              { 'name': 'morning', 'type': 'esriFieldTypeString'},
                              { 'name': 'afternoon', 'type': 'esriFieldTypeString'},
                              { 'name': 'night', 'type': 'esriFieldTypeString'}], 
                    'geometryType': '', 
                    'features': []}; 
//define the start of date
var start = ToLocal(Date(1970, 0, 01, 0, 0, 0, 0))
// Store values in the dictionary
for (var feature in fs) {
    var id = feature['ID']
    //remove the time
    var actdate = Date(Year(feature['actionDate']),Month(feature['actionDate']),Day(feature['actionDate']))
    //Calculate the difference
    var dt = DateDiff(actdate, start, 'milliseconds')
    //make the value to null for count
    var morning = Iif(feature['Shift_vehicles'] == 'Morning check (6-7am)',1,null)
    var afternoon = Iif(feature['Shift_vehicles'] == 'Afternoon check (2-4pm)',1,null)
    var night = Iif(feature['Shift_vehicles'] == 'Night check (11pm-1am)',1,null)
    Push(choicesDict.features, {"attributes": {"ID": id, 
                                               "dates": dt,
                                               "morning": morning,
                                               "afternoon": afternoon,
                                               "night": night}})
} 

//return choicesDict

// Convert dictionary to featureSet. 
var fs_dict = FeatureSet(Text(choicesDict)); 
return fs_dict
// Return featureset  
return GroupBy(fs_dict, ['ID', 'dates'],
       [{ name: 'morning', expression: 'morning', statistic: 'COUNT' },
        { name: 'afternoon', expression: 'afternoon', statistic: 'COUNT' },
        { name: 'night', expression: 'night', statistic: 'COUNT' }]); 

 Cheers,

Tang

Cheers,
Tang

View solution in original post

4 Replies
SzuNyiapTang
Esri Contributor

Hi @VeronicaArambula 

I believe it is due to the date formatting - expecting in UNIX timestamp.

Also helped to modify the date (remove time) and logic for counting if false (changed to null)

 

// Reference layer using the FeatureSetByPortalItem() method. 
var fs = FeatureSetByPortalItem(Portal('https://arcgis.com/'), '74d6c3d630c14458a3c23c71a19447ad', 0, ['*'], false);

// Empty dictionary to capture each value 
var choicesDict = {'fields': [{ 'name': 'ID', 'type': 'esriFieldTypeString'}, 
                              { 'name': 'dates', 'type': 'esriFieldTypeDate'},
                              { 'name': 'morning', 'type': 'esriFieldTypeString'},
                              { 'name': 'afternoon', 'type': 'esriFieldTypeString'},
                              { 'name': 'night', 'type': 'esriFieldTypeString'}], 
                    'geometryType': '', 
                    'features': []}; 
//define the start of date
var start = ToLocal(Date(1970, 0, 01, 0, 0, 0, 0))
// Store values in the dictionary
for (var feature in fs) {
    var id = feature['ID']
    //remove the time
    var actdate = Date(Year(feature['actionDate']),Month(feature['actionDate']),Day(feature['actionDate']))
    //Calculate the difference
    var dt = DateDiff(actdate, start, 'milliseconds')
    //make the value to null for count
    var morning = Iif(feature['Shift_vehicles'] == 'Morning check (6-7am)',1,null)
    var afternoon = Iif(feature['Shift_vehicles'] == 'Afternoon check (2-4pm)',1,null)
    var night = Iif(feature['Shift_vehicles'] == 'Night check (11pm-1am)',1,null)
    Push(choicesDict.features, {"attributes": {"ID": id, 
                                               "dates": dt,
                                               "morning": morning,
                                               "afternoon": afternoon,
                                               "night": night}})
} 

//return choicesDict

// Convert dictionary to featureSet. 
var fs_dict = FeatureSet(Text(choicesDict)); 
return fs_dict
// Return featureset  
return GroupBy(fs_dict, ['ID', 'dates'],
       [{ name: 'morning', expression: 'morning', statistic: 'COUNT' },
        { name: 'afternoon', expression: 'afternoon', statistic: 'COUNT' },
        { name: 'night', expression: 'night', statistic: 'COUNT' }]); 

 Cheers,

Tang

Cheers,
Tang
VeronicaArambula
Emerging Contributor

Oh wow!!! Thank you very very much!! It works 🙂

I'd like to ask you one thing:

1. why is it necessary to add the start date and then use the DateDiff?

 

 

0 Kudos
SzuNyiapTang
Esri Contributor

Hi Veronica,

For the date field, it is expecting in the format of Unix Timestamp which is the elapsed time after January 1st, 1970 at UTC.

Cheers,

Tang

Cheers,
Tang
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