David Stockdale's Scrapcode

Age From Date of Birth On Power Virtual Agents

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.

Then below that question you just created add a new node.

Here you want to call an action, specifically you want to “Create a flow”.

This will take you to a new flow on Power Automate.

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.

Here I’ve named my text input “Date”:

Next you will want to add a new action below your starting point.

When choosing an operation you should find the “Compose” operation under “Built-in” and “Actions”.

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.

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.

This will be a “Current Time” operation.

After current time you will want to add another action.

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.

Then you should add your third “Compose” action.

Which will calculate the difference between these two dates with this simple bit of code:


Then you should add your fourth “Compose” action.

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.

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:


And finally you should add a number output using the value of your final “Compose” operation.

And now after you save your finished flow it’s time to add it to your chatbot topic!

Here you simply add the variable from your initial question about the users date of birth.

And your new flow will take that date and returns their age in years.

And now you have the users age (factoring in years, months and days)!

Leave a Reply