<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Data expression to add a calculated attribute to all features? in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-to-add-a-calculated-attribute-to/m-p/1290179#M7835</link>
    <description>&lt;P&gt;You have to create a new Featureset and copy the attributes. Luckily, you can do it in a loop, without too much manual work:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// define the new field(s)
var new_fields = [
    {name: "NewAttribute", type: "esriFieldTypeInteger"},
]

// create an empty featureset with existing and new fields
var out_fs = {
        fields: Splice(Schema(layer).fields, new_fields),
        geometryType: Schema(layer).geometryType',
        features: []
    }

// fill the featureset
for (var i in layer) {
    var att = {}
    // copy existing field values
    for(var field in i) {
        var value = i[field]
        att[field] = IIf(TypeOf(value) == "Date", Number(value), value)
    }
    // insert the new value(s)
    var new_value = 25
    att["NewAttribute"] = new_value
    // append the feature to the featureset
    Push(out_fs.features, {attributes: att, geometry: Geometry(i)})
}

// return
return Featureset(Text(out_fs))&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 17 May 2023 16:49:50 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2023-05-17T16:49:50Z</dc:date>
    <item>
      <title>Data expression to add a calculated attribute to all features?</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-to-add-a-calculated-attribute-to/m-p/1290161#M7832</link>
      <description>&lt;P&gt;I'm trying to easily create a data expression that adds an attribute to each feature in a data set, but I'm getting stuck on how to add an attribute to an existing feature without creating a feature from scratch, including all pre-existing attributes.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get an execution error: "key not found - attributes.'&lt;/P&gt;&lt;P&gt;In short, do I have to create a brand new feature and manually reference each attribute I need, or is there an easy way to simply add an attribute to an existing feature.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var portal = Portal('https://www.arcgis.com/');
var layer = FeatureSetByPortalItem(portal, itemid)
var features=[];
for (var i in layer) {
   var newvalue = some logic here
   i['attributes']['newattribute'] = newvalue
   Push(features,i)
}
return {
  geometryType:'',
  features:features
}
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 May 2023 16:16:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-to-add-a-calculated-attribute-to/m-p/1290161#M7832</guid>
      <dc:creator>Jay_Gregory</dc:creator>
      <dc:date>2023-05-17T16:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Data expression to add a calculated attribute to all features?</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-to-add-a-calculated-attribute-to/m-p/1290179#M7835</link>
      <description>&lt;P&gt;You have to create a new Featureset and copy the attributes. Luckily, you can do it in a loop, without too much manual work:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// define the new field(s)
var new_fields = [
    {name: "NewAttribute", type: "esriFieldTypeInteger"},
]

// create an empty featureset with existing and new fields
var out_fs = {
        fields: Splice(Schema(layer).fields, new_fields),
        geometryType: Schema(layer).geometryType',
        features: []
    }

// fill the featureset
for (var i in layer) {
    var att = {}
    // copy existing field values
    for(var field in i) {
        var value = i[field]
        att[field] = IIf(TypeOf(value) == "Date", Number(value), value)
    }
    // insert the new value(s)
    var new_value = 25
    att["NewAttribute"] = new_value
    // append the feature to the featureset
    Push(out_fs.features, {attributes: att, geometry: Geometry(i)})
}

// return
return Featureset(Text(out_fs))&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 17 May 2023 16:49:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-to-add-a-calculated-attribute-to/m-p/1290179#M7835</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-05-17T16:49:50Z</dc:date>
    </item>
  </channel>
</rss>

