Age Calculation
Age Calculation in Power BI using Power Query
Power Query has a simple method for calculating the age. But, since DAX is the preferred language usedin several functionsin Power BI, many are unaware of the function available in Power Query. In this blog post I'm going to explain how easy it is to calculateAge in Power BI using PowerBI. This methodis extremely efficient when the estimation of the agecan be done as a pre-calculated row and row basis.
Calculate Age from a date
The table below is DimCustomer table that is part of the AdventureWorksDW table that acts as a birthdate column. I have removes some of the columns that aren't needed to make it easier to read;
To calculate the age of each client, the only thing you need is:
- In Power BI Desktop, Click on Transform Data
- In Power Query Editor window; make sure to select the Birthdate column first.
- go to add Column Tab, under "From Date & Time" section, and then under Date, choose Age
That's all there is to it. This will calculate what's the ratio between Birthdate column as well as the current date and time.
However, the age that you see when you look at the Age column, doesn't look like an age. This is because it's an actual duration.
Duration
Duration is a unique kind of data type used within Power Query which represents the distinction of two DateTime values. Duration is the combination of four values:
days.hours.minutes.seconds
That's how you read the values above. From the perspective of the user it is not expected of them to go and read the particulars of the ones above. there are ways that you can fetch each part from the length. Utilizing the Duration menu it will show the number of seconds and minutes, hours, days, and years from it.
To help in calculating the age in years for instance, you can simply go to Total Years.
It is important to note that the duration is calculated in days . It is was then divided in 365, to calculate the value for the year.
Rounding
In the end, no one says their age as 53.813698630136983! They refer to it as 53, then rounded down. You can select Rounding and round down from the Transform tab for it.
This will provide you with the age in years:
Then you can tidy the other columns if you want (or perhaps you've applied transformations using the Transform tab to avoid the creation of new columns) This column can be renamed as Age: column; Age:
Things to Know
- Refresh The age that is calculated by this method will be refreshed at the time of refreshing your database. And each time, it will compare the birthdate to the date and time for the refreshing. This method is an initial calculation of the age. If you, however, require the calculation to be dynamically done using DAX here is how I described a way that you can use.
- The reason behind Power Query: Benefits of using age calculation with Power Query is that the calculation takes place when you refresh your report. It is done using an algorithm that makes the calculation easier, and there won't be extra overhead to calculate it using DAX as a measure of runtime.
- Another scenario It is not used to calculate the age by birthdate. It can be used to calculate the age of inventory for products and also 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 has earned a BSc on Computer engineering; he have more that 20 years' experience in the field of data analysis data, BI, databases developing, and programming mostly on Microsoft technologies. He has been a Microsoft Data Platform MVP for nine years in a row (from 2011 until now) due to his dedication to Microsoft BI. Reza is a prolific blogger and co-founder of RADACAD. Reza is also the co-founder and co-organizer of Difinity event in New Zealand.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.
He wrote some books on MS SQL BI and also is working on other books. He was also a regular participant on technical forums online like MSDN and Experts-Exchange and was the moderator for MSDN SQL Server forums, and is an MCP, MCSE, and MCITP of BI. He is the founder of the New Zealand Business Intelligence users group. In addition, he's also the creator of very popular book Power BI from Rookie to Rock Star, which is free and contains more than 17000 pages of information and Power BI Pro Architecture, which is the Power BI Pro Architecture published by Apress.
His credentials include being an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday, and SQL Users Groups. And He is a Microsoft Certified Trainer.
Reza's love is to help you find the best solutions for data, and he's a Data enthusiast.This entry was posted under Power BI, Power BI from Rookie to Rockstar, Power Query and related to Power BI, Power BI from Rookie to Rock Star, Power Query. Bookmark the permalink.
Post navigation
Share different visual pages using different security groups in Power BIAge's Calculation of Years that can be used to calculate Leap Year in Power BI with Power Query
Comments
Post a Comment