Select to view content in your preferred language

FeatureSetByPortalName with subtotals by year

383
7
Jump to solution
06-26-2025 02:23 PM
AmyRoust
Frequent Contributor

I'm having trouble conceptualizing what I need to write in an Arcade statement. Here is a fake table that mirrors what I'm working with:

YearProgram_TypeFed Fund 1Fed Fund 2Fed Fund 3Local Fund 1Local Fund 2Local Fund 3Local Fund 4
2015Program Blue000500010000
2015Program Red10000900010001000000
2016Program Green01700003000000
2016Program Purple0055005500550001700
2017Program Orange65005000120080000

 

I need to return a table that looks like this:

YearFed FundingLocal Funding
201520000100
2016225007200
201770002000

 

The goal is to have a pie chart in a dashboard that compares combined Federal Funding to combined Local Funding. The default view will be all years combined, but I need the pie chart to update when the dashboard filter is set to a specific year.

Bonus points if I can figure out how to add a definition query to the table's results that allows me to filter out specific program types (e.g. add everything up as long as it's not from Program Orange). I can handle that part by creating a view layer from my hosted table, but if I can build it into the query, so much the better. Fewer items to manage in AGO.

I've gotten far enough that all of the values are added up, but I cannot get my mind wrapped around adding in the Year option.

// Fetches features from a public portal item
var fs = FeatureSetByPortalItem(
  Portal("https://xyz.maps.arcgis.com/"),
  "xyz",
  0,
  ["Program_Type","Fed Fund 1", "Fed Fund 2", "Fed Fund 3", "Local Fund 1", "Local Fund 2", "Local Fund 3", "Local Fund 4"],
  false
);

var fedtotal = Sum(fs, "Fed Fund 1") + Sum(fs, "Fed Fund 2") + Sum(fs, "Fed Fund 3");
var localtotal = Sum(fs, "Local Fund 1") + Sum(fs, "Local Fund 2") + Sum(fs, "Local Fund 3") + Sum("Local Fund 4");
var total = fedtotal + localtotal

var fed = round(when(fedtotal=='NaN', 0, fedtotal),2)
var local = round(when(localtotal=='NaN',0, localtotal),2)

return FeatureSet(
  {
    fields: [
      { name: "Federal", type: "esriFieldTypeSingle" },
      { name: "Local", type: "esriFieldTypeSingle" },
      { name: "Total", type: "esriFieldTypeSingle" }
    ],
    features: [{
      attributes: {Federal: fed, Local: local, Total: total}
    }]
  }
);

 

 

Tags (2)
0 Kudos
2 Solutions

Accepted Solutions
DavidSolari
MVP Regular Contributor

A GroupBy call seems ideal. Here's an untested example:

// Fetches features from a public portal item
var fs = FeatureSetByPortalItem(
  Portal("https://xyz.maps.arcgis.com/"),
  "xyz",
  0,
  ["Year", "Fed_Fund_1", "Fed_Fund_2", "Fed_Fund_3", "Local_Fund_1", "Local_Fund_2", "Local_Fund_3", "Local_Fund_4"],
  false
);

var grouped = GroupBy(fs, "Year", [
    {
        name: "Fed_Funding",
        expression: "Fed_Fund_1 + Fed_Fund_2 + Fed_Fund_3",
        statistic: "SUM"
    }, {
        name: "Local_Funding",
        expression: "Local_Fund_1 + Local_Fund_2 + Local_Fund_3 + Local_Fund_4",
        statistic: "SUM"
    }
]);

return grouped

 

View solution in original post

KenBuja
MVP Esteemed Contributor

If you had one entry per year in your data, you could have used the same GroupBy to create the two new columns dividing the sum of the federal (or local) investments by the sum of all investments. However, that won't work with multiple rows per year.

Instead you take the grouped FeatureSet and run another GroupBy to calculate those numbers.

var grouped = GroupBy(fs,"Year",
  [
    {
      name: "Fed_Funding",
      expression: "Fed_Fund_1 + Fed_Fund_2 + Fed_Fund_3",
      statistic: "SUM"
    },
    {
      name: "Local_Funding",
      expression: "Local_Fund_1 + Local_Fund_2 + Local_Fund_3 + Local_Fund_4",
      statistic: "SUM"
    }
  ]
);

GroupBy(grouped, "Year",
  [
    { name: "Federal Funding", expression: "Fed_Funding", statistic: "Max" },
    { name: "Local Funding", expression: "Local_Funding", statistic: "Max" },
    {
      name: "Total Investment",
      expression: "Fed_Funding + Local_Funding",
      statistic: "Max"
    },
    {
      name: "Federal Percentage",
      expression: "Fed_Funding/(Fed_Funding + Local_Funding)",
      statistic: "Max"
    },
    {
      name: "Local Percentage",
      expression: "Local_Funding/(Fed_Funding + Local_Funding)",
      statistic: "Max"
    }
  ]
);

View solution in original post

0 Kudos
7 Replies
DavidSolari
MVP Regular Contributor

A GroupBy call seems ideal. Here's an untested example:

// Fetches features from a public portal item
var fs = FeatureSetByPortalItem(
  Portal("https://xyz.maps.arcgis.com/"),
  "xyz",
  0,
  ["Year", "Fed_Fund_1", "Fed_Fund_2", "Fed_Fund_3", "Local_Fund_1", "Local_Fund_2", "Local_Fund_3", "Local_Fund_4"],
  false
);

var grouped = GroupBy(fs, "Year", [
    {
        name: "Fed_Funding",
        expression: "Fed_Fund_1 + Fed_Fund_2 + Fed_Fund_3",
        statistic: "SUM"
    }, {
        name: "Local_Funding",
        expression: "Local_Fund_1 + Local_Fund_2 + Local_Fund_3 + Local_Fund_4",
        statistic: "SUM"
    }
]);

return grouped

 

KenBuja
MVP Esteemed Contributor

And for the bonus, you can add a filter

// Fetches features from a public portal item
var fs = FeatureSetByPortalItem(
  Portal("https://xyz.maps.arcgis.com/"),
  "xyz",
  0,
  ["Year", "Program_Type", "Fed_Fund_1", "Fed_Fund_2", "Fed_Fund_3", "Local_Fund_1", "Local_Fund_2", "Local_Fund_3", "Local_Fund_4"],
  false
);
var filtered = Filter(fs, "Program_Type <> 'Program Orange'")
return GroupBy(filtered, "Year", [
    {
        name: "Fed_Funding",
        expression: "Fed_Fund_1 + Fed_Fund_2 + Fed_Fund_3",
        statistic: "SUM"
    }, {
        name: "Local_Funding",
        expression: "Local_Fund_1 + Local_Fund_2 + Local_Fund_3 + Local_Fund_4",
        statistic: "SUM"
    }
]);
AmyRoust
Frequent Contributor

@KenBuja , I should have just commented on the other post - recognize your code?

@DavidSolari - I had no idea that Arcade had a group by function! That's fantastic! Thank you.

0 Kudos
AmyRoust
Frequent Contributor

@KenBuja can I get your help one more time? Here is my current result table:

AmyRoust_0-1751037552794.png

 

I need two more columns: one that divides the Fed_Funding category by the Total_Investment category and one that divides the Local_Funding category by the Total_Investment category. 

0 Kudos
KenBuja
MVP Esteemed Contributor

If you had one entry per year in your data, you could have used the same GroupBy to create the two new columns dividing the sum of the federal (or local) investments by the sum of all investments. However, that won't work with multiple rows per year.

Instead you take the grouped FeatureSet and run another GroupBy to calculate those numbers.

var grouped = GroupBy(fs,"Year",
  [
    {
      name: "Fed_Funding",
      expression: "Fed_Fund_1 + Fed_Fund_2 + Fed_Fund_3",
      statistic: "SUM"
    },
    {
      name: "Local_Funding",
      expression: "Local_Fund_1 + Local_Fund_2 + Local_Fund_3 + Local_Fund_4",
      statistic: "SUM"
    }
  ]
);

GroupBy(grouped, "Year",
  [
    { name: "Federal Funding", expression: "Fed_Funding", statistic: "Max" },
    { name: "Local Funding", expression: "Local_Funding", statistic: "Max" },
    {
      name: "Total Investment",
      expression: "Fed_Funding + Local_Funding",
      statistic: "Max"
    },
    {
      name: "Federal Percentage",
      expression: "Fed_Funding/(Fed_Funding + Local_Funding)",
      statistic: "Max"
    },
    {
      name: "Local Percentage",
      expression: "Local_Funding/(Fed_Funding + Local_Funding)",
      statistic: "Max"
    }
  ]
);
0 Kudos
AmyRoust
Frequent Contributor

Thank you so much! If I had your address, I'd send you a fruit basket or an arrangement of some form. I appreciate you taking time out of your schedule to help strangers in Esriland.

0 Kudos
KenBuja
MVP Esteemed Contributor

Glad to help. And if it did solve your question, you can accept this as a solution in addition to David's reply

0 Kudos