<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Attribute Rule Calculation: Count the occurrences of a value in a number of fields in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162185#M53784</link>
    <description>&lt;P&gt;I'm getting an "Error on line 9.&amp;nbsp; String type expected" message when I try this method.&amp;nbsp; Is trying to use "$feature" on a standalone table an issue (i.e., does the data source have to be a feature layer)?&amp;nbsp; I tried whittling my list down to just one field to be sure that wasn't the problem.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Apr 2022 16:25:28 GMT</pubDate>
    <dc:creator>ToddLusk</dc:creator>
    <dc:date>2022-04-07T16:25:28Z</dc:date>
    <item>
      <title>Attribute Rule Calculation: Count the occurrences of a value in a number of fields</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162008#M53754</link>
      <description>&lt;P&gt;I'm attempting to create an Attribute Rule Calculation that counts the number of occurrences of a given value ("x") in a number of fields in a dataset.&amp;nbsp; I want to update a different field ("IssuesCount") in the said dataset with that number.&amp;nbsp; In total, there are 15 fields that I need to check.&lt;/P&gt;&lt;P&gt;My thought was that I would start with a list of fields names that I want to check then dynamically generate the field names, check to see if they contained "x", then count up the occurrences.&amp;nbsp; But I'm struggling to dynamically generate the field names from the list.&lt;/P&gt;&lt;P&gt;Here's the list of fields :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var fieldList = ['IssueAdjacentProperty', 'IssueCultivation',
'IssueFoodPlots', 'IssueHaying', 'IssueTimberHarvestCutting',
'IssueBurning', 'IssueNewExpandedTrail', 'IssueNewExpandedRoad',
'IssueParkedEquipment', 'IssueNewBuildings', 'IssueGrazingLivestock',
'IssueCommercialSeed', 'IssueUtilities', 'IssueMining', 'IssueImpervious',
'IssueHydrology', 'IssueUnauthorizedEasement', 'IssueWaste', 'IssueMowing',
'IssueOther', 'IssueExpandedYard', 'IssueFencing', 'IssueErosion',
'IssueStormwater', 'IssueYardWaste']&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And here's me trying to dynamically generate the field names to check the values:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Dynamically generate field name
for (var f in fieldList){
  // Doesn't work as it returns the literal string of '$feature.0'
  var fieldName = "$feature." + f​

  // Return the value in the field.
  // This part doesn't work because 'fieldName' equals
  // '$feature.0' at this point and Arcade is looking
  // for a field called 'fieldName' here.
  var checkValue = $feature.fieldName

  // Then do something like loop through fields checking for 'x'
  // (unless there is a more efficient way to do that too)
  var counter = 0
  if (checkValue == 'x') {
    counter++;
  }
}

// Return the count
return counter;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I think once I get this field name piece figured out I have, I know what to do on the rest of the Attribute Rule side of things.&lt;/P&gt;&lt;P&gt;Thanks for any help!&lt;/P&gt;&lt;P&gt;Todd Lusk, Senior GIS Specialist&lt;BR /&gt;Dakota County, MN&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 11:49:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162008#M53754</guid>
      <dc:creator>ToddLusk</dc:creator>
      <dc:date>2022-04-07T11:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Rule Calculation: Count the occurrences of a value in a number of fields</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162027#M53756</link>
      <description>&lt;P&gt;A few different things.&lt;/P&gt;&lt;P&gt;1. In a for loop with an array, your variable is the index. Use&amp;nbsp;&lt;STRONG&gt;parentarray[index]&amp;nbsp;&lt;/STRONG&gt;to access the actual contents of that item.&lt;/P&gt;&lt;P&gt;2. You're adding the text string "$feature." to your&amp;nbsp;&lt;STRONG&gt;fieldname&lt;/STRONG&gt; variable, which you are then accessing with&amp;nbsp;&lt;STRONG&gt;$feature.fieldname&lt;/STRONG&gt;. Written out, that would be "$feature.$feature.", plus whatever your field name is.&lt;/P&gt;&lt;P&gt;Accessing fields with dot notation only works with literal field names, and then only if the field name has no invalid characters.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_0-1649334876312.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/38324i08D3CDFE884AE9DD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_0-1649334876312.png" alt="jcarlson_0-1649334876312.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Instead, try using bracket notation. Your field name in this situation is just a string, so you can pipe in any variable.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_1-1649334977292.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/38325i34CBCA3A35989686/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_1-1649334977292.png" alt="jcarlson_1-1649334977292.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;3. Establish your count&amp;nbsp;&lt;EM&gt;outside&amp;nbsp;&lt;/EM&gt;of the loop. Otherwise you're going to re-set it to 0 every time it goes to the next field in the list.&lt;/P&gt;&lt;P&gt;There are&amp;nbsp;&lt;EM&gt;lots&amp;nbsp;&lt;/EM&gt;of ways to do the counting bit, but looking at the fields in a for loop seems fine. You probably don't need so many variables, though.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var fieldList = ['your', 'field', 'names']

var total = 0

for (var f in fieldList){
    if ($feature[fieldList[f] == 'x'){
        total += 1
    }
}

return total&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 12:44:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162027#M53756</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2022-04-07T12:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Rule Calculation: Count the occurrences of a value in a number of fields</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162029#M53757</link>
      <description>&lt;P&gt;There are multiple ways to use for loops:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var arry = [1, 2, 3]

// The "normal" way
// we define start value, end value, and increment of the loop variable
for(var i = 0; i &amp;lt; Count(array); i++) {
    Console(array[i])
}

// The "for in" way
// This is a shorter form of the "normal" approach
// the loop variable is the index of the array
for(var i in array) {
    Console(array[i])
}

// the "for in" way on a FeatureSet
// when you use "for in" on a feature set, the loop variable is not the index
// of the feature set, but the actual feature.
var fs = FeatureSetByName(...)
for(var f in fs) {
    Console(f.Field)
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;You tried to mix the second and third way.&amp;nbsp;&lt;STRONG&gt;f&lt;/STRONG&gt; is the index of &lt;STRONG&gt;fieldList&lt;/STRONG&gt;, not the value at that index.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Easiest way to do this is probably this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// get the _values_
var value_list = [
    $feature.IssueAdjacentProperty,
    $feature.IssueCultivation,
    $feature.IssueFoodPlots, 
    $feature.IssueHaying, 
    $feature.IssueTimberHarvestCutting,
// ...
]

// define a filter function
function equals_search_value(val) { return val == "x" }

// filter the value array and return the count
return Count(Filter(value_list, equals_search_value))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 12:47:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162029#M53757</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-04-07T12:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Rule Calculation: Count the occurrences of a value in a number of fields</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162035#M53759</link>
      <description>&lt;P&gt;Hello Todd,&lt;/P&gt;&lt;P&gt;I have done something similar with counts, but instead of counting data in fields I have created a script that uses the "&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/field-statistics-to-table.htm" target="_self"&gt;FieldStatisticsToTable&lt;/A&gt;" geoprocessing tool.&amp;nbsp; What it does is, it counts the stats in several tables using my unique ID then puts the count into a stand alone table for each.&amp;nbsp; Once all the stand alone tables are created, I have one master table where I append the counts for one report.&amp;nbsp; A suggestion on how to extract only the&amp;nbsp;&lt;SPAN&gt;("x") in a number of fields is to set up a definition query in your dataset to only display those records OR you could use "&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/conversion/feature-class-to-feature-class.htm" target="_self"&gt;FeatureToFeatureClass&lt;/A&gt;" geoprocessing tool to create another table (preferably in a file geodatabase) from the default dataset and use it to run the field stats.&amp;nbsp; If you need a new field "IssueCount" to be visible, you can add it as a field to your new table that is created with the featuretofeatureclass tool and append the statistic counts to that field.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 12:57:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162035#M53759</guid>
      <dc:creator>ABishop</dc:creator>
      <dc:date>2022-04-07T12:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Rule Calculation: Count the occurrences of a value in a number of fields</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162067#M53764</link>
      <description>&lt;P&gt;Thanks, Josh!&amp;nbsp; This seems to be the way my brain was thinking about it but clearly didn't have the syntax quite right.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 13:51:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162067#M53764</guid>
      <dc:creator>ToddLusk</dc:creator>
      <dc:date>2022-04-07T13:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Rule Calculation: Count the occurrences of a value in a number of fields</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162069#M53765</link>
      <description>&lt;P&gt;Thanks, Johannes!&amp;nbsp; While my brain doesn't quite comprehend your method as well as it does Josh's above, I can see how this method works too.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 13:52:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162069#M53765</guid>
      <dc:creator>ToddLusk</dc:creator>
      <dc:date>2022-04-07T13:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Rule Calculation: Count the occurrences of a value in a number of fields</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162185#M53784</link>
      <description>&lt;P&gt;I'm getting an "Error on line 9.&amp;nbsp; String type expected" message when I try this method.&amp;nbsp; Is trying to use "$feature" on a standalone table an issue (i.e., does the data source have to be a feature layer)?&amp;nbsp; I tried whittling my list down to just one field to be sure that wasn't the problem.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 16:25:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162185#M53784</guid>
      <dc:creator>ToddLusk</dc:creator>
      <dc:date>2022-04-07T16:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute Rule Calculation: Count the occurrences of a value in a number of fields</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162431#M53812</link>
      <description>&lt;P&gt;Hmmm... Sadly, I have no idea what could cause that error. I have only tested my expression in the Arcade Playground, maybe there's some hickups when you involve actual data. You should probably stick to Josh's answer.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1649404423989.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/38427i11528711E270B597/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_0-1649404423989.png" alt="JohannesLindner_0-1649404423989.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Is trying to use "$feature" on a standalone table an issue (i.e., does the data source have to be a feature layer)?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;No, $feature and FeatureSetBy*() work on non-feature-class tables, too.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2022 07:56:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/attribute-rule-calculation-count-the-occurrences/m-p/1162431#M53812</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-04-08T07:56:51Z</dc:date>
    </item>
  </channel>
</rss>

