<?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: Combine and sum fields in data expression in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1236709#M7041</link>
    <description>&lt;LI-CODE lang="javascript"&gt;// load data
var input = {fields: [{name: "Site", type: "esriFieldTypeString"}, {name: "Species1", type: "esriFieldTypeString"}, {name: "Species1Obs", type: "esriFieldTypeInteger"}, {name: "Species2", type: "esriFieldTypeString"}, {name: "Species2Obs", type: "esriFieldTypeInteger"}, {name: "Species3", type: "esriFieldTypeString"}, {name: "Species3Obs", type: "esriFieldTypeInteger"},], features: [{attributes: {Site: "Little Swamp", Species1: "Red Kangaroo", Species1Obs: 5, Species2: "Wallaby", Species2Obs: 3, Species3: "Eastern Grey", Species3Obs: 10}}, {attributes: {Site: "Big Swamp", Species1: "Fox", Species1Obs: 5, Species2: "Red Kangaroo", Species2Obs: 10, Species3: "Eastern Grey", Species3Obs: 5}},], geometryType: ""}
var fs = Featureset(Text(input))

// flatten the table
var flat_dict = {
    fields: [
        {name: "Species", type: "esriFieldTypeString"},
        {name: "Obs", type: "esriFieldTypeInteger"},
        ],
    features: [],
    geometryType: ""
}
var max_species_number = 3
for(var f in fs) {
    for(var i = 1; i &amp;lt;= max_species_number; i++) {
        var new_f = {attributes: {Species: f[`Species${i}`], Obs: f[`Species${i}Obs`]}}
        Push(flat_dict.features, new_f)
    }
}
var flat_fs = Featureset(Text(flat_dict))

// group by species and return
return GroupBy(flat_fs, ["Species"], [{name: "Obs", expression: "Obs", statistic: "SUM"}])&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_1-1669879689299.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/57328i0CD84EEBD01669DF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_1-1669879689299.png" alt="JohannesLindner_1-1669879689299.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Dec 2022 07:28:18 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2022-12-01T07:28:18Z</dc:date>
    <item>
      <title>Combine and sum fields in data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1236680#M7040</link>
      <description>&lt;P&gt;Hi All&lt;/P&gt;&lt;P&gt;I'm sure this can be done but I'm new to data expressions and just cant seem to get started.&lt;/P&gt;&lt;P&gt;I have a dataset that has fields Species1, Species1Obs, Species2, Species2Obs, Species3, Species3Obs and so on&lt;/P&gt;&lt;P&gt;The species are defined via a dropdown list so what I want to do is to be able to sum the total number of observations per species across all fields. For example the data table might look something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE width="717"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="104"&gt;Site&lt;/TD&gt;&lt;TD width="100"&gt;Species1&lt;/TD&gt;&lt;TD width="110"&gt;Species1Obs&lt;/TD&gt;&lt;TD width="115"&gt;Species2&lt;/TD&gt;&lt;TD width="95"&gt;Species2Obs&lt;/TD&gt;&lt;TD width="105"&gt;Species3&lt;/TD&gt;&lt;TD width="88"&gt;Species3Obs&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Little Swamp&lt;/TD&gt;&lt;TD&gt;Red Kangaroo&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;Wallaby&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Eastern Grey&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Big Swamp&lt;/TD&gt;&lt;TD&gt;Fox&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;Red Kangaroo&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;Eastern Grey&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is to use a data expression so I can transform the data to use indicator(s) eg:&lt;/P&gt;&lt;TABLE width="204"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="104"&gt;Species&lt;/TD&gt;&lt;TD width="100"&gt;Total&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Red Kangaroo&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Wallaby&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Fox&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Eastern Grey&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems like it would be possible in a data expression, I just need some help with a starting point.&lt;/P&gt;&lt;P&gt;Cheers, Chris&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 02:46:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1236680#M7040</guid>
      <dc:creator>ChrisRoberts2</dc:creator>
      <dc:date>2022-12-01T02:46:28Z</dc:date>
    </item>
    <item>
      <title>Re: Combine and sum fields in data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1236709#M7041</link>
      <description>&lt;LI-CODE lang="javascript"&gt;// load data
var input = {fields: [{name: "Site", type: "esriFieldTypeString"}, {name: "Species1", type: "esriFieldTypeString"}, {name: "Species1Obs", type: "esriFieldTypeInteger"}, {name: "Species2", type: "esriFieldTypeString"}, {name: "Species2Obs", type: "esriFieldTypeInteger"}, {name: "Species3", type: "esriFieldTypeString"}, {name: "Species3Obs", type: "esriFieldTypeInteger"},], features: [{attributes: {Site: "Little Swamp", Species1: "Red Kangaroo", Species1Obs: 5, Species2: "Wallaby", Species2Obs: 3, Species3: "Eastern Grey", Species3Obs: 10}}, {attributes: {Site: "Big Swamp", Species1: "Fox", Species1Obs: 5, Species2: "Red Kangaroo", Species2Obs: 10, Species3: "Eastern Grey", Species3Obs: 5}},], geometryType: ""}
var fs = Featureset(Text(input))

// flatten the table
var flat_dict = {
    fields: [
        {name: "Species", type: "esriFieldTypeString"},
        {name: "Obs", type: "esriFieldTypeInteger"},
        ],
    features: [],
    geometryType: ""
}
var max_species_number = 3
for(var f in fs) {
    for(var i = 1; i &amp;lt;= max_species_number; i++) {
        var new_f = {attributes: {Species: f[`Species${i}`], Obs: f[`Species${i}Obs`]}}
        Push(flat_dict.features, new_f)
    }
}
var flat_fs = Featureset(Text(flat_dict))

// group by species and return
return GroupBy(flat_fs, ["Species"], [{name: "Obs", expression: "Obs", statistic: "SUM"}])&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_1-1669879689299.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/57328i0CD84EEBD01669DF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_1-1669879689299.png" alt="JohannesLindner_1-1669879689299.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 07:28:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1236709#M7041</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-12-01T07:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: Combine and sum fields in data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1237072#M7048</link>
      <description>&lt;P&gt;Thanks for the prompt response Johannes&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code you gave me just seems to take a table of data.&amp;nbsp; What I need to do is interegate the dataset in the portal, combine all the species and sum the observation&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 23:25:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1237072#M7048</guid>
      <dc:creator>ChrisRoberts2</dc:creator>
      <dc:date>2022-12-01T23:25:31Z</dc:date>
    </item>
    <item>
      <title>Re: Combine and sum fields in data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1237146#M7049</link>
      <description>&lt;P&gt;Sorry, forgot to mention...&lt;/P&gt;&lt;P&gt;I took your sample data for the expression. In your real expression, you should of course load the featureset from your portal. So instead of lines 2&amp;amp;3 in my code above, you would do something like this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var fs = FeaturesetByPortalItem(
    Portal("https://my-server.de/portal"), // your Portal's url
    "bb11a82308a5439291c17a477755d27a",    // the service id
    0,                                     // the sub-layer id
    ["*"],                                 // the fields you want to load
    false                                  // do you want to load geometries
    )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can get all the neccessary arguments from your table's url:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1669970502979.png" style="width: 756px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/57455iD5F31939B402DAC9/image-dimensions/756x51?v=v2" width="756" height="51" role="button" title="JohannesLindner_0-1669970502979.png" alt="JohannesLindner_0-1669970502979.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 08:42:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1237146#M7049</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-12-02T08:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: Combine and sum fields in data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1237642#M7053</link>
      <description>&lt;P&gt;Awesome!&lt;/P&gt;&lt;P&gt;Thanks so much!!&lt;/P&gt;</description>
      <pubDate>Sun, 04 Dec 2022 21:26:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1237642#M7053</guid>
      <dc:creator>ChrisRoberts2</dc:creator>
      <dc:date>2022-12-04T21:26:13Z</dc:date>
    </item>
    <item>
      <title>Re: Combine and sum fields in data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1238018#M7055</link>
      <description>&lt;P&gt;Hi Johannes&lt;/P&gt;&lt;P&gt;I hate to push my luck because you have been super helpful.....but is it possible to further group/split by Observation Date {obs_date}&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 21:56:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1238018#M7055</guid>
      <dc:creator>ChrisRoberts2</dc:creator>
      <dc:date>2022-12-05T21:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Combine and sum fields in data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1238101#M7058</link>
      <description>&lt;P&gt;I actually think I managed to work it out myself &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;var fs = FeatureSetByPortalItem(Portal('&lt;A href="https://environment.geohub.sa.gov.au/portal" target="_blank" rel="noopener"&gt;https://xxxxx&lt;/A&gt;'), 'a81b87605d0346599ea5cffbebab66e6', 0, ["*"], false);&lt;BR /&gt;// load data&lt;/P&gt;&lt;P&gt;// flatten the table&lt;BR /&gt;var flat_dict = {&lt;BR /&gt;fields: [&lt;BR /&gt;{name: "Species", type: "esriFieldTypeString"},&lt;BR /&gt;{name: "Obs", type: "esriFieldTypeInteger"},&lt;BR /&gt;{name: "Obs_date", type: "esriFieldTypeDate"},&lt;BR /&gt;],&lt;BR /&gt;features: [{attributes: {Obs_date: Number(`obs_date`)}}],&lt;BR /&gt;geometryType: ""&lt;BR /&gt;}&lt;BR /&gt;var max_species_number = 7&lt;BR /&gt;for(var f in fs) {&lt;BR /&gt;for(var i = 1; i &amp;lt;= max_species_number; i++) {&lt;BR /&gt;var new_f = {attributes: {Obs_date: Number(f[`obs_date`]), Species: f[`Species${i}`], Obs: f[`Species${i}Obs`]}}&lt;BR /&gt;Push(flat_dict.features, new_f)&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;var flat_fs = Featureset(Text(flat_dict))&lt;/P&gt;&lt;P&gt;// group by species and return&lt;BR /&gt;return GroupBy(flat_fs, ["Species", "obs_date"], [{name: "Obs", expression: "Obs", statistic: "SUM"}])&lt;/P&gt;</description>
      <pubDate>Tue, 06 Dec 2022 05:58:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/combine-and-sum-fields-in-data-expression/m-p/1238101#M7058</guid>
      <dc:creator>ChrisRoberts2</dc:creator>
      <dc:date>2022-12-06T05:58:28Z</dc:date>
    </item>
  </channel>
</rss>

