Select to view content in your preferred language

Dashboard filtering indicator data with Expression

444
4
Jump to solution
04-14-2025 12:55 PM
tschrage
Emerging Contributor

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

 ArcadeError.jpg

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

This expression will filter out the records from the current year. The original dataset has 493721 records, with 2550 records for 2025

var fs = FeatureSetByPortalItem(
  Portal("https://www.arcgis.com"),
  "79461a1ec0974301bde274177c7108bd", //a layer from the Living Atlas to test 
  0,
  ["place", "mag", "time"],
  false
);

return Filter(fs, `EXTRACT(YEAR from time) = ${Year(Now())}`); //time is the Date field

Then, in the Indicator, use the Statistic value type and Count.

Snag_1cd6399.png

View solution in original post

0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor

This expression will filter out the records from the current year. The original dataset has 493721 records, with 2550 records for 2025

var fs = FeatureSetByPortalItem(
  Portal("https://www.arcgis.com"),
  "79461a1ec0974301bde274177c7108bd", //a layer from the Living Atlas to test 
  0,
  ["place", "mag", "time"],
  false
);

return Filter(fs, `EXTRACT(YEAR from time) = ${Year(Now())}`); //time is the Date field

Then, in the Indicator, use the Statistic value type and Count.

Snag_1cd6399.png

0 Kudos
tschrage
Emerging Contributor

I try that verbatim using my portal information and get the generic error message: "Test execution error: Unknown Error. Verify test data."

My Date field appears to be formatted exactly like your data.  The only difference I can see is mine is located in a related table and yours is part of the base feature.

0 Kudos
KenBuja
MVP Esteemed Contributor

Did you change the name of the date field in line 9?

return Filter(fs, `EXTRACT(YEAR from yourDateField) = ${Year(Now())}`)
0 Kudos
tschrage
Emerging Contributor

 I didn't.  I guess I didn't understand that Extract function.  That worked.  Thanks!

0 Kudos