Select to view content in your preferred language

Data Expressions

371
11
3 weeks ago
Lebowski
Frequent Contributor

Hello All,

I have created a Dashboard on my Portal. The layers I am referencing for the Dashboard are found in a map which is also included in the portal. I need to create a Table Widget that pulls field from two different layers within the map. I am attempting to use a Data Expression to retrieve the data from the two layers. I am just beginning to learn Arcade but I am pretty sure I am on the right track with the expression below; however, no matter how many times I have tried to correct an syntax errors within the expression I allows receive a message stating stating "Unable to execute the Arcade Script". I can't even get a simple expression to work.

1. Are there any specific permissions on the Portal that need to be enable to use Data Expressions withing a dashboard?

Here is the current expression. I am pulling from two layer 'Ownership Parcels' and 'Site Boundary'. From the 'Ownership Parcels' layer I need the following fields: 'COST_CENTER', 'Name', and 'CalculatedAcre'. From the 'Site Boundary' layer I need the these two field: 'Name' and 'COST_CENTER'. 'COST_CENTER' is the field that will link the two layers. Any advice would be appreciated!

 

// Connect to the same portal as your dashboard
var portal = Portal("https://some.portal.college.edu/portal")

// Shared item ID for both layers
var itemId = "4b1aefe40a8847bab37217a36688dfbf"

// Ownership Parcels (Layer 24)
var parcels = FeatureSetByPortalItem(
portal, itemId, 24,
["Name", "CalculatedArea", "COST_CENTER"]
)

// Site Boundary (Layer 57)
var sites = FeatureSetByPortalItem(
portal, itemId, 57,
["Name", "COST_CENTER"]
)

// Define table schema
var fields = [
{ name: "Parcel_Name", type: "esriFieldTypeString" },
{ name: "Calculated_Area", type: "esriFieldTypeDouble" },
{ name: "Parcel_COST_CENTER", type: "esriFieldTypeString" },
{ name: "Site_Name", type: "esriFieldTypeString" },
{ name: "Site_COST_CENTER", type: "esriFieldTypeString" }
]

// List of output features
var features = []

// Intersect parcels with sites and build output table
for (var parcel in parcels) {
for (var site in sites) {
if (Intersects(parcel, site)) {
Push(features, {
attributes: {
Parcel_Name: parcel["Name"],
Calculated_Area: parcel["CalculatedArea"],
Parcel_COST_CENTER: parcel["COST_CENTER"],
Site_Name: site["Name"],
Site_COST_CENTER: site["COST_CENTER"]
}
})
}
}
}

// Return result (dictionary structure — required by Dashboards)
return {
fields: fields,
geometryType: "",
features: features
}

 

 

0 Kudos
11 Replies
CodyPatterson
MVP Regular Contributor

Hey @Lebowski 

When you're working through the arcade, what I would do is slowly work through the program, stopping at each line and hitting check/verify/run to make sure that each step is working.

First I would verify that you have permissions to view the layer, and also have a creator license to create the dashboards, next, I would try running this to verify you can connect:

var portal = Portal("https://some.portal.college.edu/portal")

var itemId = "4b1aefe40a8847bab37217a36688dfbf"

If everything works out, I would then try to move onto this part here and add it on:

var parcels = FeatureSetByPortalItem(
portal, itemId, 24,
["Name", "CalculatedArea", "COST_CENTER"]
)

Continuing this process should isolate your issue, let me know if this ends up working for you!

Cody

Lebowski
Frequent Contributor
var portal = Portal("https://some.portal.college.edu/portal")

var itemId = "4b1aefe40a8847bab37217a36688dfbf"
0 Kudos
Lebowski
Frequent Contributor

So when I add the expression above everything is still red and the return I get is 'null"

0 Kudos
KenBuja
MVP Esteemed Contributor

You have to return a FeatureSet from a Data Expression to use in an element. Try using this as your return

return FeatureSet({ fields: fields, features: features });

Also, when posting code, please use the Insert/Edit code sample button

Lebowski
Frequent Contributor
// Connect to the same portal as your dashboard
var portal = Portal("https://some.portal.college.edu/portal")

// Shared item ID for both layers
var itemId = "7712f75bdb7246d9b6c56f2c9e029dbe"

// Ownership Parcels (Layer 24)
var parcels = FeatureSetByPortalItem(
portal, itemId, 24,
["Name", "CalculatedArea", "COST_CENTER"]
)

// Site Boundary (Layer 57)
var sites = FeatureSetByPortalItem(
portal, itemId, 57,
["Name", "COST_CENTER"]
)

// Define table schema
var fields = [
{ name: "Parcel_Name", type: "esriFieldTypeString" },
{ name: "Calculated_Area", type: "esriFieldTypeDouble" },
{ name: "Parcel_COST_CENTER", type: "esriFieldTypeString" },
{ name: "Site_Name", type: "esriFieldTypeString" },
{ name: "Site_COST_CENTER", type: "esriFieldTypeString" }
]

// List of output features
var features = []

// Intersect parcels with sites and build output table
for (var parcel in parcels) {
for (var site in sites) {
if (Intersects(parcel, site)) {
Push(features, {
attributes: {
Parcel_Name: parcel["Name"],
Calculated_Area: parcel["CalculatedArea"],
Parcel_COST_CENTER: parcel["COST_CENTER"],
Site_Name: site["Name"],
Site_COST_CENTER: site["COST_CENTER"]
}
})
}
}
}

// Return result (dictionary structure — required by Dashboards)
return FeatureSet({ fields: fields, features: features });
}
0 Kudos
Lebowski
Frequent Contributor

Here is my updated expression, still unable to execute. In the Arcade Playground my output says "Test execution error: Unexpected token '}'.. Verify test data."

0 Kudos
KenBuja
MVP Esteemed Contributor

Remove line 50

Lebowski
Frequent Contributor

Output from PlaygroundArcade_Playground.jpg

0 Kudos
KenBuja
MVP Esteemed Contributor

If you have a lot of of sites and parcels, this expression will take a long time to calculate. To see how it's progressing through the parcels loop, put this Console statement at line 32

Console(parcel.name);

and check the Console tab.