Age Calculation

Age Calculation in Power BI using Power Query

Power Query has a simple way of calculating the age. But, since DAX is the preferred languagein several analysesin Power BI, many do not know about the function available in Power Query. In this blog I'm going to explain how easy it is to calculateAge in Power BI by using Power BI. The methodis extremely efficient in situations where the Age calculationcan be carried out on an already calculated row-by-row basis.

Calculate Age from a date

This is the DimCustomer table from the AdventureWorksDW table which as a birthdate column. I've removed a few of the extra columns to make it more readable;

If you want to calculate the age of each customer, all you have to do is to:

  • In Power BI Desktop, Click on Transform Data
  • In Power Query Editor window; pick the Birthdate column first.
  • Click on the Add Column Tab. Under "From Date & Time" section, and then under Date, choose the age range.

That's it. This can calculate an amount that is the sum of the Birthdate column, along with the current date and time.

But, the age you see under the Age column, doesn't appear to be an age. It's because it's a duration.

Duration

Duration is a unique data type found in Power Query which represents the differences of two DateTime values. Duration is a mixture of four values:

days.hours.minutes.seconds

and that's how you can interpret the data above. But from users' perspective you don't want them to search for particulars like that. There are ways you can fetch each part from the length. By selecting the Duration menu it will show that you can get the number of seconds, minutes, hours, days, and years from it.

To use the method of calculating the age in years such as, for instance, you can simply click on Total Years:

Take note that the duration of the program is calculated in days , and then divided by 365, to give you the yearly value.

Rounding

At the final point, no one says that their age is 53.813698630136983! They refer to it as 53, with a rounding down. You can select Rounding and Round Down from the Transform tab for it.

This will give you the age in years:

Then you can tidy the other columns, if desired (or perhaps you've applied transformations using the Transform tab to avoid any creation of columns) And name this column; Age:

Things to Know

  • Refresh: The age calculated by this method will be refreshed at the time of refreshing your database. and each time will compare the date of birth with the date and date that the data refresh took place. This method is an earlier calculation of age. If you require the calculation of age to be performed dynamically with DAX, here I explained how you can employ.
  • How to use Power Query: Benefits of performing age calculations in Power Query is that the calculation is made during the refresh of your report. This is done by using an instrument that makes the calculation more simple, and there won't be extra overhead in calculating it using DAX as a measure runtime.
  • Additional scenarios It is not for calculating age only on the basis of birthdate. This is a good way to determine product inventory and for the different between two dates or dates from each other.

Video

REZA RAD

TRAINER, CONSULTANT, MENTORReza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He holds the BSc from Computer engineering. He has more than 20 years' experience in data analysis database, BI, programming, and development predominantly on Microsoft technologies. He is an official Microsoft Data Platform MVP for nine years in a row (from 2011 till now) for his dedication in Microsoft BI. Reza is a prolific author and co-founder at RADACAD. Reza is also co-founder and co-organizer of the Difinity conference at New Zealand.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.
He has written a few books on MS SQL BI and also is writing a few more. He was also an active participant in online forums for technical issues like MSDN and Experts-Exchange and was the moderator of MSDN SQL Server forums, and holds the MCP and MCSE as well as an MCITP of BI. He is the director of the New Zealand Business Intelligence users group. He is also the creator of the book that is very well-loved Power BI from Rookie to Rock Star, which is free and contains more than 700 pages of content, as well as The Power BI Pro Architecture published by Apress.
The speaker is an International speaker at Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday, and SQL user groups. And He is a Microsoft Certified Trainer.
Reza's dream is to help users find the best data solution. He is a Data enthusiast.This article was published under Power BI, Power BI from Rookie to Rockstar, Power Query and included in Power BI, Power BI from Rookie to Rock Star, Power Query. The following is a great resource for you to bookmark.

Post navigation

- Share Different Visual Pages with Different Security Groups in Power BIAge's Age Calculation that can be used to calculate Leap Year in Power BI using Power Query -

Comments

Popular posts from this blog

What Is a Calorie?

power-converter

Convert Length and Distance Units Instantly