<?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: Arcade expression to calculate average in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1147513#M26204</link>
    <description>&lt;P&gt;Just to throw another approach on the heap, we can use &lt;STRONG&gt;Filter&lt;/STRONG&gt; to simply drop those zeroes and nulls as well.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var vals = [1, 2, 3, 0, 0, Null]

function DropEm(value){ DefaultValue(value, 0) != 0 }

Average(Filter(vals, DropEm))&lt;/LI-CODE&gt;&lt;P&gt;It still pales in comparison to the concision of Python, though.&lt;/P&gt;</description>
    <pubDate>Thu, 24 Feb 2022 14:24:43 GMT</pubDate>
    <dc:creator>jcarlson</dc:creator>
    <dc:date>2022-02-24T14:24:43Z</dc:date>
    <item>
      <title>Arcade expression to calculate average</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1147402#M26195</link>
      <description>&lt;P&gt;I have a table with five fields for sampling records and a sixth field for a mean/average value to be calculated in.&lt;/P&gt;&lt;P&gt;I have used the Arcade command 'Average' within 'Field Calculator', but find it does not calculate a correct value when 0 values are recorded within some fields.&lt;/P&gt;&lt;P&gt;Average($feature.fld0,$feature.fld1,$feature.fld2,$feature.fld3,$feature.fld4)&lt;/P&gt;&lt;P&gt;Average(1,2,3,0,0) should equal (1+2+3) / 3 = 2, but the value I am getting is (1+2+3+0+0) / 5 = 1.2&lt;/P&gt;&lt;P&gt;What would be the expression to calculate the correct average value when some fields have 0 values (ie. exclude fields with a 0 value from the calculation)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 04:54:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1147402#M26195</guid>
      <dc:creator>CPoynter</dc:creator>
      <dc:date>2022-02-24T04:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression to calculate average</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1147411#M26196</link>
      <description>&lt;P&gt;You&amp;nbsp; can Arcade the logic from this python example&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# python... enclose field names in ! eg. !f0!
# example row
f0, f1, f2, f3, f4 = 3, 2, 1, 0, 0

sum([f0, f1, f2, f3, f4])/len([i for i in [f0, f1, f2, f3, f4] if i &amp;gt; 0])
2.0&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 05:54:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1147411#M26196</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-02-24T05:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression to calculate average</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1147423#M26197</link>
      <description>&lt;LI-CODE lang="javascript"&gt;function non_zero_average(values) {
    var non_zero = []
    for(var i in values) {
        var v = values[i]
        if(!IsEmpty(v) &amp;amp;&amp;amp; v != 0) {
            Push(non_zero, v)
        }
    }
    return Average(non_zero)
}
return non_zero_average([1, 2, 3, 0, null])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 07:16:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1147423#M26197</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-02-24T07:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression to calculate average</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1147513#M26204</link>
      <description>&lt;P&gt;Just to throw another approach on the heap, we can use &lt;STRONG&gt;Filter&lt;/STRONG&gt; to simply drop those zeroes and nulls as well.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var vals = [1, 2, 3, 0, 0, Null]

function DropEm(value){ DefaultValue(value, 0) != 0 }

Average(Filter(vals, DropEm))&lt;/LI-CODE&gt;&lt;P&gt;It still pales in comparison to the concision of Python, though.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 14:24:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1147513#M26204</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2022-02-24T14:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression to calculate average</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1189303#M26395</link>
      <description>&lt;P&gt;Having the same problem and am unable to understand any of the replies above being almost novice in programming, although they make sense to me. My function is as follows:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Average($feature.Qrt1_2022, $feature.Qrt2_2022, $feature.Qrt3_2022, $feature.Qrt4_2022)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All I want it to do is don't account for 0 values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 08:15:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1189303#M26395</guid>
      <dc:creator>Ministry_of_Forestry_Fiji</dc:creator>
      <dc:date>2022-07-05T08:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression to calculate average</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1189307#M26396</link>
      <description>&lt;P&gt;Let's look at Josh's answer above you. What it does is the following:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;get an array of values&lt;/LI&gt;&lt;LI&gt;define a function with which to filter this array. this function returns true if you want the value to stay in the array. we want a value to stay in the array if it is unequal to zero (!=0). but we also have to account for null values. that's what DefaultValue does: it returns the value if it is not null and returns a default value (0 in this case) if it is null.&lt;/LI&gt;&lt;LI&gt;filter the array&lt;/LI&gt;&lt;LI&gt;get the average of the filtered array&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, if we apply Josh's answer to your problem (and make the steps more obvious):&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// get an array of values
var values = [$feature.Qrt1_2022, $feature.Qrt2_2022, $feature.Qrt3_2022, $feature.Qrt4_2022]

// define a function that we use to filter the array
// returns true (keep value) if value is not null and is unequal to 0
function drop_zero(value) {
    var non_null_value = DefaultValue(value, 0)  // value is null? -&amp;gt; value = 0
    return non_null_value != 0
}

// filter the array
var filtered_values = Filter(values, drop_zero)

// return the average of the filtered array
return Average(filtered_values)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 05 Jul 2022 08:36:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1189307#M26396</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-07-05T08:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression to calculate average</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1190666#M26399</link>
      <description>&lt;P&gt;Thank you. This was very helpful.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2022 22:56:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1190666#M26399</guid>
      <dc:creator>Mohammed_Abdullah_BinShorab</dc:creator>
      <dc:date>2022-07-07T22:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade expression to calculate average</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1554731#M27446</link>
      <description>&lt;P&gt;This really saved today's bacon! Thank you!!&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2024 15:53:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/arcade-expression-to-calculate-average/m-p/1554731#M27446</guid>
      <dc:creator>lwilcock1</dc:creator>
      <dc:date>2024-11-01T15:53:14Z</dc:date>
    </item>
  </channel>
</rss>

