Select to view content in your preferred language

Arcade Data Expression - "Unable to execute script"

1557
5
02-28-2023 07:05 AM
C_McNamara
Occasional Contributor

Hello!

I am new to using arcade in dashboards and, hopefully, am making a simple mistake. I am trying to create an indicator from 2 different services (sum of Approved Dwelling Units). I am returning the correct value in the "Edit data expression" page but when I click done I am unable to use the expression - Esri says "Unable to execute script". 

This is what my code currently looks like. This is my first time calculating values with different layers using FeatureSetByPortalItem

Code:

 

var PrelimPlans = FeatureSetByPortalItem(

  Portal('https://www.arcgis.com/'),

  '2a7c6c6f86b14253b1ecf079e3f4e6dd',

  1,

  ['Name', 'APPR_DU_TOT', 'Count'],

  false

);

//Count(PrelimPlans);

 

var PrelimDUTOT = Sum(PrelimPlans,'APPR_DU_TOT');

//Return PrelimDUTOT

 

var SitePlan = FeatureSetByPortalItem(

  Portal('https://www.arcgis.com/'),

  '33c2a66277c34153907ba5d14484a24b',

  3,

  ['Name', 'APPR_DU_TOT', 'Count'],

  false

);

//Count(SitePlan);

Sum(SitePlan,"APPR_DU_TOT")

var SitePlanDUTOT = Sum(SitePlan,'APPR_DU_TOT');

//Return SitePlanDUTOT

 

var CombinedDwellingUnits = PrelimDUTOT + SitePlanDUTOT;

Return CombinedDwellingUnits

 

***

Any help would be greatly appreciated!

 

All the best,

Colin

0 Kudos
5 Replies
jcarlson
MVP Esteemed Contributor

A Data Expression needs to return a FeatureSet, where your expression is just returning a number.

The simplest way to do this would be to wrap your number in a single-feature FeatureSet, like this:

return FeatureSet(Text({
    fields: [{name: 'the_count', type: 'esriFieldTypeInteger'}],
    geometryType: '',
    features: [{attributes: {the_count: CombinedDwellingUnits}}]
}))

Put that in instead of your final return line, see if it works.

- Josh Carlson
Kendall County GIS
C_McNamara
Occasional Contributor

Works!

Thanks, Josh!

C_McNamara
Occasional Contributor

Hey Josh (@jcarlson),

Is it possible to combine these two layers together by creating a dynamic FeatureSet with a whole bunch of similar variables to visualize summarized stats like the problem above? 

C_McNamara_1-1677606861871.png

So, instead of creating a sum in the data expression, I would access a combined table or FeatureSet with 9,669 records (sum/join of my Preliminary plans and Site Plans).

 

 

 

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Yes, that's definitely possible! It can take a bit longer, since the only way to merge two featuresets is to do it feature by feature, but it shouldn't be too bad.

Here is an example expression that does just that:

https://github.com/Esri/arcade-expressions/blob/master/dashboard_data/CombineMultipleLayers(SerialCh...

Never mind that it's for a Serial Chart, you can use the same thing for an indicator.

- Josh Carlson
Kendall County GIS
0 Kudos
C_McNamara
Occasional Contributor

Think I got it!!

var PrelimPlans = FeatureSetByPortalItem(Portal('https://www.arcgis.com/'),'2a7c6c6f86b14253b1ecf079e3f4e6dd',1,['*'],false);
var SitePlan = FeatureSetByPortalItem(Portal('https://www.arcgis.com/'),'33c2a66277c34153907ba5d14484a24b',3,['*'],false); 
//var SitePlanDUTOT = Sum(SitePlan,'APPR_DU_TOT');
//Return SitePlanDUTOT
//Distinct(SitePlan,'APNO') var combinedDict = { 
  'fields': [ 
    { name: "APNO", type:"esriFieldTypeString" },
    { name: "APPR_DU_TOT", type: "esriFieldTypeDouble" },
    { name: "PROP_DU_SF", type: "esriFieldTypeDouble" }, 
    { name: "PROP_DU_MF", type: "esriFieldTypeDouble" },
    {name: "APPR_SQFT_TOT", type: "esriFieldTypeDouble" },
  ], 
  geometryType: "", 
  features: [], 
}; var i = 0; 
// Loop through each FeatureSet and store its attributes 
for (var t in PrelimPlans) { 
  combinedDict.features[i++] = { 
    attributes: {
      APNO: t["APNO"],
      APPR_DU_TOT: t["APPR_DU_TOT"],
      PROP_DU_SF: t["PROP_DU_SF"], 
      PROP_DU_MF: t["PROP_DU_MF"],
      APPR_SQFT_TOT: t["APPR_SQFT_TOT"],
    }, 
  }; 
} for (var m in SitePlan) { 
  combinedDict.features[i++] = { 
    attributes: { 
      APNO: m["APNO"],
      APPR_DU_TOT: m["APPR_DU_TOT"],
      PROP_DU_SF: m["PROP_DU_SF"], 
      PROP_DU_MF: m["PROP_DU_MF"],
      APPR_SQFT_TOT: m["APPR_SQFT_TOT"],
    }, 
  }; 
}
return FeatureSet(Text(combinedDict))
0 Kudos