Kontakt os

Calculate age, anniversary and milestone birthday in Power BI

Learn how to calculate age, anniversaries and milestone birthdays based on date of birth and date of employment


The challenge

For an HR report we only know the day of birth and the hire date for the employee, but we would like to show the upcoming milestone birthdays and anniversaries.

We need to calculate their age, and create filters to show the upcoming birthdays/anniversaries 1 year in advance

Power BI report showing a table of employees with their upcoming milestone birthdays and anniversaries filtered to within the next year

The solution

If you don’t already have a date table, first create one by following this guide

Data
For this example we will create a simple SharePoint list with

  • Name of employee
  • Day of birth
  • Employment date

My list looks like this

SharePoint list with Name of employee, Day of birth, and Employment date columns containing sample employee data

Age
When you have added your SharePoint list to Power BI, remove all other columns, than the 3 above, and change the two date fields to date

Your query should now look like this

Power Query editor showing only the three employee columns with Day of birth and Employment date fields set to the Date data type

Step 1
Select your day of birth column, and select date -> Age

Power Query Date menu showing the Age option selected on the Day of birth column to calculate a duration from birth to today

Step 2
Select your new column and click duration -> Total years

Power Query Duration menu showing Total years selected on the Age duration column to convert it to a decimal year value

Step 3
Right click your column and select Transform -> Round -> Round down

Power Query Transform > Round > Round down applied to the Total years column to get a whole number age value

You can now remove the Age column that we used for the duration calculation, so that our query now look like this

Power Query showing the clean employee table with Name, Day of birth, Employment date, and the rounded Age column after removing the intermediate duration column

Seniority
To calculate how many years an employee has been working for the company you can follow the exact same steps as above just with the employment date column.

Power Query showing the employee table with a Seniority column calculated from the Employment date using the same Age calculation steps

Next birthday and seniority date
To show the user of the report when the employee has their next anniversary or milestone birthday add a new custom column

Next birthday

Power Query Add Custom Column dialog showing the Next birthday formula using Date.AddYears with Day of birth and Age plus 1

Formula
Date.AddYears([Dayofbirth], [Age] + 1)

Next seniority date

Power Query Add Custom Column dialog showing the Next seniority date formula using Date.AddYears with Employment date and Seniority plus 1

Formula
Date.AddYears([Employeedate], [Seniority] + 1)

Don’t forget to change the type of the columns to date

Your query should now look like this, and we are ready to start building our report

Power Query showing the fully prepared employee table with Name, Day of birth, Employment date, Age, Seniority, Next birthday, and Next seniority date columns ready for reporting

The report (and some DAX)

Before you continue, don’t forget to connect your date table to your employees table

Step 1
Create a new table visual and insert

  • Name of employee
  • Day of birth
  • Age
  • Employee date
  • Seniority

Power BI table visual showing employee name, day of birth, age, employment date, and seniority columns for all employees

If you want to format the date as in my table above, simply change the format of the date columns to dd-MM-yyyy

Power BI column formatting settings showing the date format changed to dd-MM-yyyy for the date columns in the table visual

Step 2
The table will show you all the employees, so now we need add measures that will filter the table for us.

Create a new measure with the following code

Anniversaries = SUMX(
    FILTER(
        Anniversary,
        Anniversary[Seniority] IN {19, 29, 39, 49, 59, 69, 79, 89, 99}
    ),
    Anniversary[Seniority] + 1
)

And another measure with the following code

Milestone birthday = SUMX(
    FILTER(
        Anniversary,
        Anniversary[Age] IN {19, 29, 39, 49, 59, 69, 79, 89, 99}
    ),
    Anniversary[Age] + 1
)

This code will show us all employees who has a milestone within the next year (with a 10 year interval) but will return the visual with 1 “year” added

Step 3
Now we could just add the next birthday and next anniversary date, but that will show the date in all rows

Power BI table showing next birthday and next seniority date columns populated on every row before applying the blank-filtering measures

To avoid that we will create two new measures, that returns a blank value if their corresponding columns has no value

Anniversary date formatted = 
IF(
    Anniversary[Anniversaries] = BLANK(), 
    BLANK(), 
    SELECTEDVALUE(Anniversary[SeniorityNextDate])
)
Milestone birthday formatted = 
IF(
    Anniversary[Milestone birthday] = BLANK(), 
    BLANK(), 
    SELECTEDVALUE(Anniversary[dayofbirthNext])
)

We now have a table that will show us the upcoming anniversaries and milestone birthdays within the next year 🎂

Final Power BI table filtered to show only employees with upcoming milestone anniversaries or birthdays within the next year using the DAX measures

Pinksky bruger cookies for at forbedre funktionaliteten på denne hjemmeside. Du bestemmer selv hvad vi må bruge. Læs mere i vores cookiepolitik.