Select to view content in your preferred language

Help with a expression to count features only where criteria is matched

618
9
01-16-2026 01:04 PM
Laura_m_Conner
Regular Contributor

All,

In Arc GIS online, in an experience i am building i have text box that i want to  show the count  of all selected data where a "field" matches a "value". I got the text box set up  linked to the data. I am adding the dynamic text. In the expression dialog window what expression in the expression box do I put? so far AI and my own attempts have failed. Is this even posable to do in this manner? If not what  is there an approach I use can that avoids having to making a view and filtering it on the field?

Thanks  

0 Kudos
9 Replies
Ke_Xu
by Esri Regular Contributor
Esri Regular Contributor

Hi @Laura_m_Conner,

Please use Arcade in Experience Builder. The help link is here: https://doc.arcgis.com/en/experience-builder/latest/configure-widgets/advanced-formatting.htm#ESRI_S...

 

 

Thanks,

Ke

0 Kudos
Laura_m_Conner
Regular Contributor

That is what I am trying to do. The problem is I can not figure out the syntax for it.

0 Kudos
KenBuja
MVP Esteemed Contributor

This code should work. You'll have to replace the datasource ID with your own and supply the correct field name in the sql statement.

var ds = $dataSources["dataSource_1-19123c9cd36-layer-2"];
var layer = $dataSources["dataSource_1-19123c9cd36-layer-2"].layer;
var sql = "yourField = value" //or "yourField = 'value'" if it's a string
var filteredLayer = Filter(layer, sql)
var output = Count(filteredLayer)

return {
  value: output,
  text: {
    // size: 14,
    // color: 'rgb(0, 0, 255)',
    // bold: true,
    // italic: false,
    // underline: false,
    // strike: false
  },
};

 This is what it looks like in while editing the Text widget. "Feature Count" is the name of the Arcade script.

2026-01-20_10-06-40.PNG

And this is what the final result is

2026-01-20_10-06-54.PNG

Laura_m_Conner
Regular Contributor

all,

sorry for the delay. I am bouncing between projects. 

Ok I have gotten in to the arcade widow in the text box to past my code. your code is giving me an invalid expression warning when i try to modify it. How do i find my datasource ID for the layer i want to use?  also on this note I am going to try and make a arc GIS solution out if it. the idea is for people to change in their data layer for the template data layer i have set up. is this kind of data source connection going to cause it to brake when the substation happens?   

i got the flowing code to return true / false if a  feature matching  the criteria is selected but cant figure out how to modify it return the count. can that be done? the code is: 

function getFilteredFeatureSet(ds) {
var result = ds.layer;
var queryParams = ds.queryParams;

// Leave selection logic unchanged
if (!IsEmpty(queryParams.where)) {
result = Filter(result, queryParams.where);
}

if (!IsEmpty(queryParams.geometry)) {
result = Intersects(result, queryParams.geometry);
}

// Filter to critical only
result = Filter(result, "criticalCustomer = 1");

return result;
}

// ds must be whatever Experience Builder is passing you for the selected-meter datasource.
// Keep your existing ds line here (do not change it).
// Example (yours will differ): var ds = $datastore["Meters"].asDynamicLayer();

var fs = getFilteredFeatureSet(ds);
return Count(fs);

 

Also resources on this would also be helpfull.

Laura

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Try this script

function getFilteredFeatureSet(ds) {
var result = ds.layer;
var queryParams = ds.queryParams;

// Leave selection logic unchanged
if (!IsEmpty(queryParams.where)) {
result = Filter(result, queryParams.where);
}

if (!IsEmpty(queryParams.geometry)) {
result = Intersects(result, queryParams.geometry);
}

// Filter to critical only
result = Filter(result, "criticalCustomer = 1");

return result;
}

// ds must be whatever Experience Builder is passing you for the selected-meter datasource.
// Keep your existing ds line here (do not change it).
// Example (yours will differ): var ds = $datastore["Meters"].asDynamicLayer();

var ds = 
var fs = getFilteredFeatureSet(ds);
return Count(fs);

To get the correct layer, after you've connected the text box to your data, click edit, then the Arcade button. Put your cursor on line 24 after the =. On the side of the script editor is the Profile variable button. Click it, then click the arrow on right side of the $datasources line. Click the first line below that which has your datasource ID to add it to your script.

(view in My Videos)

Laura_m_Conner
Regular Contributor

Thanks i try it as soon as I get a chance

0 Kudos
Laura_m_Conner
Regular Contributor

 

That code did not work. it kept giving me in valid syntax error. I was able spring board off of it and managed to  get something that worked:

// Count selected meters where CRITICAL = 1

var fs = $dataSources["dataSource_1-19bc841fd0e-layer-7"].selectedFeatures;

// If nothing is selected, show 0
if (IsEmpty(fs)) {
  return 0;
}

// Count only critical meters within the selection
return Count(Filter(fs, "CRITICAL = 1"));

 

Do you know of any where I can learn the basic syntax rules of arcade? knowing the formal rules would be helpful in trying to compile code for various things.

0 Kudos
KenBuja
MVP Esteemed Contributor

Start with the Arcade documentation, which contains lots of information about how to use the language, especially in the Language features section.

0 Kudos
Laura_m_Conner
Regular Contributor

Thanks Will Do.

0 Kudos