Help getting a data expression to work.

753
4
Jump to solution
12-15-2022 07:47 AM
Labels (1)
ChelseaTabor
New Contributor III

I'm writing a data expression to show which polygon (mmz) each point (leaks) resides within.  I also need to keep a few fields from the leaks feature.  I can populate the 'mmz' field, but I'm having trouble getting the other fields I need.  Here's what I have so far and the results:

 

var port = Portal('my portal')
var mmz = FeatureSetByPortalItem(port, 'c879aa9267604224a3f6d492fc997a50', 7,['ZONE_NAME'],true)
var leaks = FeatureSetByPortalItem(port, '44fa0f9330e449c289dfadf2553d805b', 0,['leakloss','leaktype','dateshot','leaks_count','material','maindiameter'],true)

var features = []
var feat

for (var zone in mmz){
     var pts = Contains(zone,leaks)
     for (var pt in pts){
          feat = {attributes:{
               mmz:                 zone ['ZONE_NAME'],
               leakloss:            pt ['leakloss'],
               leaktype:           pt ['leaktype'],
               dateshot:          pt ['dateshot'],
               leaks_count:     pt ['leaks_count'],
               material:            pt ['material'],
               maindiameter: pt ['maindiameter']
               }
          }
Push(features,feat)
}
}

var dict = {
     fields: [
          {name: "mmz", alias:"Zone", type:"esriFieldTypeString"},
          {name: "leakloss", alias:"Gallons Lost", type:"esriFieldTypeInteger"},
          {name: "leaks_count", alias:"Number of Leaks", type:"esriFieldTypeInteger"},
          {name: "leaktype", alias:"Leak Type", type:"esriFieldTypeString"},
          {name: "dateshot", alias:"Date", type:"esriFieldTypeDate"},
          {name: "material", alias:"Material", type:"esriFieldTypeString"},
          {name: "maindiameter", alias:"Diameter", type:"esriFieldTypeInteger"},
     ],
     geometryType:"",
     features:features
}

return FeatureSet(Text(dict))

 

Results:

ChelseaTabor_0-1671118894004.png

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

I don't really see anything obviously wrong with the expression... You've confirmed the fields in the points layer has values, and that they're compatible with an integer field?

Data Expressions also tend to have trouble with Date fields. Try replacing this line:

dateshot: pt ['dateshot'],

with this:

dateshot: Number(pt['dateshot']),

Also, there are spaces between zone / pt and the bracketed attribute name, and there shouldn't be.

- Josh Carlson
Kendall County GIS

View solution in original post

4 Replies
jcarlson
MVP Esteemed Contributor

I don't really see anything obviously wrong with the expression... You've confirmed the fields in the points layer has values, and that they're compatible with an integer field?

Data Expressions also tend to have trouble with Date fields. Try replacing this line:

dateshot: pt ['dateshot'],

with this:

dateshot: Number(pt['dateshot']),

Also, there are spaces between zone / pt and the bracketed attribute name, and there shouldn't be.

- Josh Carlson
Kendall County GIS
ChelseaTabor
New Contributor III

Adding the Number function to the date field worked, but I also had to add the Text function to string field types for those values to show up. 

 

Thanks for your help.

0 Kudos
JohannesLindner
MVP Frequent Contributor

 I also had to add the Text function to string field types for those values to show up

That's weird, this conversion should happen automagically.

 

The problem with Date fields is something that leads to confusion quite regularly. Please consider lending your support to this idea to hopefully get that fixed:

https://community.esri.com/t5/arcgis-online-ideas/arcade-allow-date-values-in-date-fields/idi-p/1204...


Have a great day!
Johannes
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