I'm trying to create a dashboard that dynamically filters data based on specific years but change when a new year rolls around without having to edit the dashboard each year. I thought I had it figured out but cannot seem to get the dashboard to accept the use of my expression within an Indicator widget.
I have data hosted at AGOL. We have maintenance data in a related table. When maintenance has been performed, a new record gets added to the table with the date. I desire to count the number of times each year maintenance has been performed.
I've confirmed access to the data. Without converting the data to a "FeatureSet" I get the expected value as a result but cannot get the dashboard to accept it as a data source.
I've spent more time than I care to admit trying to figure this out including trying AI and cannot seem to get it to work. I'm obviously missing something so I'm here to hopefully gain some knowledge.
// Get the current year
var currentYear = Year(Now())
// Set up portal and hosted feature layer
var portal = Portal("MY-URL")
// Load FeatureSet from the portal item
var fs = FeatureSetByPortalItem(
portal,
"MyItemID", // Your portal item ID
2, // Layer index
["Date"], // Field to use (ensure "Date" is correct)
false
)
// Initialize counter
var count = 0
// Loop through features and filter by current year
for (var f in fs) {
var featureDate = f.Date
if (!IsEmpty(featureDate)) {
var featureYear = Left(Text(featureDate), 4) // Extract year from Date field
if (featureYear == Text(currentYear)) {
count += 1 // Increment count for matching year
}
}
}
// Optional: If you want to return a FeatureSet with matching features
var resultDict = {
fields: [
{name: 'feature_count', type: 'esriFieldTypeInteger'} // Single field for the count
],
geometryType: '', // No geometry since you're just counting features
features: [
{
attributes: { feature_count: count }
}
]
}
// Return the count or the filtered FeatureSet
return resultDict // Or return count if you just need the number 