To calculate age in Power BI, you can use the DATEDIFF function along with the DATE function.
The DATEDIFF function calculates the difference between two dates, while the DATE function creates a date value from given year, month, and day values.
Here’s an example of how you can calculate age:
DateDiff = DATEDIFF(
'TableName'[BirthDate],
DATE(YEAR(NOW()), MONTH(NOW()), DAY(NOW())),
YEAR
)
In this example, replace ‘TableName’ with the name of your table, and ‘BirthDate’ with the name of the column that contains the birth dates.
The DATEDIFF function takes three arguments:
- The start date: ‘TableName'[BirthDate]
- The end date: DATE(YEAR(NOW()), MONTH(NOW()), DAY(NOW())) – this gives you the current date
- The unit of measurement for the difference: YEAR – this will return the age in years
The result of the calculation will be a column that contains the ages corresponding to each birth date in your table.