![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2021/07/sound.jpg)
If you’re new to using Power Virtual Agents and are trying to make a chatbot like me even the most simple calculations can seem confusing.
Here’s how to take a users date of birth and calculate their age in years while factoring in months and days.
First you need a question where you can get the users date of birth.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age0.jpg)
Then below that question you just created add a new node.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age1.jpg)
Here you want to call an action, specifically you want to “Create a flow”.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age2.jpg)
This will take you to a new flow on Power Automate.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age3.jpg)
I renamed my flow “Age” but this won’t matter until we’re adding the flow to the topic.
Now you will want to add a text input at the start of your new flow.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age4.jpg)
Here I’ve named my text input “Date”:
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age5.jpg)
Next you will want to add a new action below your starting point.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age6.jpg)
When choosing an operation you should find the “Compose” operation under “Built-in” and “Actions”.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age7.jpg)
Now you will want to click the input box and go to the “Expression” section of the box which pops up when you click it.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age8.jpg)
Here we will paste in this function:
formatDateTime(parseDateTime(triggerBody()['text']), 'yyyy-MM-dd')
Which formats the date into a year-month-day output.
Next you will want to add another action.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age9.jpg)
This will be a “Current Time” operation.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age10.jpg)
After current time you will want to add another action.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age11.jpg)
This will be another “Compose” operation in which you should paste this code:
formatDateTime(parseDateTime(body('Current_time')), 'yyyy-MM-dd')
Which will format the current time to match the same format as the date of birth.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age12.jpg)
Then you should add your third “Compose” action.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age13.jpg)
Which will calculate the difference between these two dates with this simple bit of code:
dateDifference(outputs('Compose'),outputs('Compose_2'))
Then you should add your fourth “Compose” action.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age14.jpg)
This will take the output of the third compose (which will look like this: ‘10788.00:00:00’) and output just the first number (which is the number of days).
split(outputs('Compose_3'), '.')[0]
Now you should add your fifth and final “Compose” action.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age15.jpg)
This should divide the days between the date of birth and the current date by the number of days in a year with the following code:
div(int(outputs('Compose_4')),365)
And finally you should add a number output using the value of your final “Compose” operation.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age16.jpg)
And now after you save your finished flow it’s time to add it to your chatbot topic!
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age17.jpg)
Here you simply add the variable from your initial question about the users date of birth.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age18.jpg)
And your new flow will take that date and returns their age in years.
![](https://davidstockdalescrapcode.co.uk/wp-content/uploads/2023/06/get-age19.jpg)
And now you have the users age (factoring in years, months and days)!