Select to view content in your preferred language

Groupby() date with count and field concatenate?

88
1
yesterday
JamesGustine
Regular Contributor

Does Groupby() have the ability to concatenate fields as the they are grouped?  Seems like this should be easier but I can't find any documentation.

The results would be groupby(date), count, concat(field_value).

Tags (3)
0 Kudos
1 Reply
KenBuja
MVP Esteemed Contributor

Here's one I use to concatenate fields. It uses GroupBy to get the required fields and counts, then creates a new featureset with the concatenated fields.

My original dataset has the field Strat_Num, which is a unique combination of Habitat, Depth, Subregion, and Admin.

Snag_1771b77.png

I wanted to group by Stratum Number, concatenate the other values, and get their count.

This is the code I came up with

var fs = FeatureSetByPortalItem(
  Portal("https://www.arcgis.com/"),
  "itemId",
  0
);

var grouped = GroupBy(
  fs,
  "Strat_Num",
  [
    { name: "Habitat", expression: "Habitat", statistic: "Max" },
    { name: "Depth", expression: "Depth", statistic: "Max" },
    { name: "SUBREGION", expression: "SUBREGION", statistic: "Max" },
    { name: "ADMIN", expression: "ADMIN", statistic: "Max" },
    { name: "Count", expression: "1", statistic: "Count" }
  ]
);
var features = [];
for (var i of grouped) {
  Push(
    features,
    {
      attributes:
        {
          Number: i.Strat_num,
          Stratum: `${i.Habitat} ${i.Depth} ${i.SUBREGION} ${i.Admin}`,
          Count: i.Count
        }
    }
  );
}

return FeatureSet(
  {
    fields: [
      { name: "Number", type: "esriFieldTypeInteger" },
      { name: "Stratum", type: "esriFieldTypeString" },
      { name: "Count", type: "esriFieldTypeInteger" }
    ],
    features: features
  }
);

which gives me this table

Snag_17ee983.png

0 Kudos