## Calculate average person age by Date of birth

0
Hi,   new to mendix – I have created a registration form were users enter the date of birth. i would like to calculate the average age of all users that registered. Thanks.
1

Robert is right. The easiest Mendix way is a retrieve and loop in a microflow. There is a way however that will give you better performance if need be, and that is OQL. You do a retrieve over the person, select avg((datediff(DAY,dateofbirth, currentdate)) like this:

``````FROM ForumQuestions.Employee
SELECT avg(datediff(DAY, DateOfBirth, '[%CurrentDateTime%]')) as AverageDaysOfOurPersons
``````

And this is also very doable. To use this OQL:

• import the module OQL,
• in your microflow add widget “Execute OQL” where the 1 parameter is a string, which you will feed this FROM statement to.
• Set the return entity. This entity is likely a non-persistant entity with only one attribute: AverageDaysOfOurPersons

See https://forumquestions-sandbox.mxapps.io/p/questions, click question 96873 for a working sample. and click on the pencil on right side of the screen to view it in Studio (Awesome feature in this usecase). It actually was easier to accomplish then I expected.

Nevertheless, you also want to know how to do this in a microflow and loop, see this shared microflow as example:

• In the loop each time add the daysToDobAndNow
• After the loop, divide this number by the number of times the loop has looped.
0

Thanks for the answers, i need to use microflow for this, i know how to retrieve a list of the users inside the microflow  but i am not sure how to calculate the age in the loop.

-3

The easiest way to do this would be to retrieve all the users and loop over them. You would need to calculate each age in the loop, then add to a total combined age. Outside the loop you would then divide this by the total number of users to get the average (mean) age.

Hope this helps.