<pre><code>{# Get all members #}
{% set members = craft.users().limit(null) %}

{# Get total number of members #}
{% set memberCount = members.total() %}

{# Set this outside the 'for' loop so you can use it later #}
{% set ageSum = 0 %}

{# Add 'em up #}
{# We assume here you have a custom member field with the handle 'age' #}
{% for member in members %}
    {% set ageSum = ageSum + member.age %} 
{% endfor %}

{# Get the average #}
{% set avgAge = ageSum / memberCount %}

{# Print it #}
&lt;p&gt;Average member age is {{ avgAge }}&lt;p&gt;
</code></pre>

<p>If you want to limit the calculation to a particular user group, just add its handle to the end of the first <code>set</code>.</p>

<pre><code>{% set members = craft.users().group('groupHandle') %}
</code></pre>

<p>If <code>age</code> is not a required field you might want to skip records that don't have an <code>age</code>. I'm not entirely sure what including them would do to the math.</p>

<pre><code>{% set ageSum = (member.age and member.age &gt; 0) ? ageSum + member.age : ageSum %}
</code></pre>

CC BY-SA 3.0
2016-09-28T19:29:55.990Z
Philip, in your example you don't add .limit(x) to it. If I leave that out, I only get 100 results. Is there a way to disable this default limit of 100?
John Stevens
CC BY-SA 3.0
2016-09-28T19:45:13.087Z
@JohnStevens, you can add `.limit(null)` to the criteria in order to return all users – i.e. `{% set members = craft.users().limit(null) %}`.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-09-28T19:45:47.203Z
Actually, I'll edit the question to include it :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-09-30T16:26:30.650Z
I also made the assumption of an `age` field, versus the far more logical `birthdate` field. But it should be easy enough to replace all my `age` field references with an age calculation based on the `birthdate`.
Philip Jones
CC BY-SA 3.0
2016-09-30T16:28:09.447Z
And thanks @MatsMikkelRummelhoff for updating with `.limit(null)`. I always forget to account for the 100-record limit. Good call.
Philip Jones
CC BY-SA 3.0
2016-09-28T17:54:25.427Z
2016-09-28T19:46:00.077Z
2016-09-28T19:46:00.077Z
Philip Jones
<p>I don't think this will exactly answer your question, but I think it will help get you going in the right direction.</p>

<p>Within my application, I am grabbing entries that belong to the currently logged in user - finding the fields and then displaying an average distance.</p>

<p>If you'll notice I am setting a <code>level': 2</code> in my parameters. That's because I am working with a structure and the first level is the "parent" entry that does not have any of the data I am attempting to average.</p>

<pre><code>{# Get all of the entries entered by the user. #}
{% set entries = craft.entries({
    'section': 'mySection',
    'level': 2,
    'descendantOf': user.id
}) %}

{% if entries|length %}

    ...

    {% set value = 0 %}
    {% set count = 0 %}

    {% for entry in entires %}
        {% set value = value + entry.distance %}
        {% set count = loop.index %}
    {% endfor %}

    {% set averageDistance = (value / count)|round(1, 'floor') %}

    &lt;p&gt;Avg: {{ averageDistance }}&lt;/p&gt;

    ...

{% else %}
    {# Could not find any entries. #}
{% endif %}
</code></pre>

<p>So in a nutshell, you'll want to:</p>

<ul>
<li>grab the entries from the appropriate section</li>
<li>find the field(s) you want to average ex: distance</li>
<li>do math</li>
<li>return the result</li>
</ul>

<p>For you (this is a stab in the dark) you would want to get all users, get the value of the <code>age</code> field. Then loop over to get your <code>count</code> and divide that by the ages.</p>

<p>Hope this helps!</p>

CC BY-SA 3.0
2016-09-28T17:32:22.737Z
Thanks Damon, your example gave me insight on how to do it.

`{% set totaal = craft.users.group('leden').limit(5000)|length %}
{% set sumage = 0 %}
{% for age in craft.users.group('leden').limit(5000) %}
 {% set sumage = sumage + age.geboortedatum.diff(now).format('%y') %}
{% endfor %}
Avg: {{ (sumage / totaal)|round  }}
`
John Stevens
CC BY-SA 3.0
2016-09-28T15:21:43.413Z
2016-09-28T15:21:43.413Z
Damon
<p>We are building a family community website with 600+ members.</p>

<p>One of the gimmicks will be some stats, like average age all members.</p>

<p>Is there a smart way to calculate this?</p>

<templating><filter>
How to calculate average age members
# How to calculate average age members

(Asked by: username_0 on 2016-09-28)

We are building a family community website with 600+ members.

One of the gimmicks will be some stats, like average age all members.

Is there a smart way to calculate this?

**Tags:** templating, filter


---

## 2 answers

### Answer by username_1 on 2016-09-28. Score: 2 | Accepted answer

{# Get all members #}
{% set members = craft.users().limit(null) %}

{# Get total number of members #}
{% set memberCount = members.total() %}

{# Set this outside the 'for' loop so you can use it later #}
{% set ageSum = 0 %}

{# Add 'em up #}
{# We assume here you have a custom member field with the handle 'age' #}
{% for member in members %}
    {% set ageSum = ageSum + member.age %} 
{% endfor %}

{# Get the average #}
{% set avgAge = ageSum / memberCount %}

{# Print it #}
Average member age is {{ avgAge }}

If you want to limit the calculation to a particular user group, just add its handle to the end of the first set.
{% set members = craft.users().group('groupHandle') %}

If age is not a required field you might want to skip records that don't have an age. I'm not entirely sure what including them would do to the math.
{% set ageSum = (member.age and member.age > 0) ? ageSum + member.age : ageSum %}

> **Comments:**
> **username_0**: Philip, in your example you don't add .limit(x) to it. If I leave that out, I only get 100 results. Is there a way to disable this default limit of 100?  
> **username_2**: @JohnStevens, you can add `.limit(null)` to the criteria in order to return all users – i.e. `{% set members = craft.users().limit(null) %}`.  
> **username_2**: Actually, I'll edit the question to include it :)  
> **username_1**: I also made the assumption of an `age` field, versus the far more logical `birthdate` field. But it should be easy enough to replace all my `age` field references with an age calculation based on the `birthdate`.  
> **username_1**: And thanks @MatsMikkelRummelhoff for updating with `.limit(null)`. I always forget to account for the 100-record limit. Good call.  

---

### Answer by username_3 on 2016-09-28. Score: 2

I don't think this will exactly answer your question, but I think it will help get you going in the right direction.
Within my application, I am grabbing entries that belong to the currently logged in user - finding the fields and then displaying an average distance.
If you'll notice I am setting a level': 2 in my parameters. That's because I am working with a structure and the first level is the "parent" entry that does not have any of the data I am attempting to average.
{# Get all of the entries entered by the user. #}
{% set entries = craft.entries({
    'section': 'mySection',
    'level': 2,
    'descendantOf': user.id
}) %}

{% if entries|length %}

    ...

    {% set value = 0 %}
    {% set count = 0 %}

    {% for entry in entires %}
        {% set value = value + entry.distance %}
        {% set count = loop.index %}
    {% endfor %}

    {% set averageDistance = (value / count)|round(1, 'floor') %}

    Avg: {{ averageDistance }}

    ...

{% else %}
    {# Could not find any entries. #}
{% endif %}

So in a nutshell, you'll want to:

grab the entries from the appropriate section
find the field(s) you want to average ex: distance
do math
return the result

For you (this is a stab in the dark) you would want to get all users, get the value of the age field. Then loop over to get your count and divide that by the ages.
Hope this helps!

> **Comments:**
> **username_0**: Thanks username_3, your example gave me insight on how to do it.  `{% set totaal = craft.users.group('leden').limit(5000)|length %} {% set sumage = 0 %} {% for age in craft.users.group('leden').limit(5000) %}  {% set sumage = sumage + age.geboortedatum.diff(now).format('%y') %} {% endfor %} Avg: {{ (sumage / totaal)|round  }} `  

---

