Power Query: Age from Birthdate on Action date

SOQLee

Board Regular
Joined
Mar 18, 2015
Messages
58
Office Version
  1. 365
Platform
  1. Windows
In Power Query, is it possible to determine the AGE from the BIRTH DATE at a specific ACTION date? I have the the following columns:

BirthdateActionDate
1980-01-012017-01-26

<tbody>
</tbody>
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Further to my question. In PowerPivot I use the following DAX formula to create a column. I'm wondering if it can be done in PowerQuery.

=DIVIDE([ActionDate]-[Birthdate],365)
 
Upvote 0
The correct way to calculate the age is to take the difference between the year parts and subtract 1 if the ActionDate is prior to the birthday in that year.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Birthdate", type date}, {"ActionDate", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Age", each Date.Year([ActionDate]) - Date.Year([Birthdate]) - 
                            (if Date.Month([ActionDate]) < Date.Month([Birthdate]) then 1 else 
                             if Date.Month([ActionDate]) = Date.Month([Birthdate]) and Date.Day([ActionDate]) < Date.Day([Birthdate]) then 1 else 0))
in
    #"Added Custom"

Remark: if you choose "From table" when creating the query in Excel, then the data types are by default changed to datetime; I adjusted this to date.
 
Last edited:
Upvote 0
Is it possible to adjust this custom column formula below to prevent error value on refresh when there is no value to calculate from in the [Birthdate] column?

=Date.Year([Appl Dt]) - Date.Year([Birthdate]) -
(if Date.Month([Appl Dt]) < Date.Month([Birthdate]) then 1 else
if Date.Month([Appl Dt]) = Date.Month([Birthdate]) and Date.Day([Appl Dt]) < Date.Day([Birthdate]) then 1 else 0)
 
Upvote 0
Code:
#"Added Custom" = Table.AddColumn(#"Changed Type", "Age", each 
                                         if [Birthdate] = null 
                                         then 
                                              null 
                                          else 
                                              Date.Year([Appl Dt]) - Date.Year([Birthdate]) - Number.From(Date.ToText([Birthdate], "MMdd") > Date.ToText([Appl Dt], "MMdd")))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,168
Members
448,870
Latest member
max_pedreira

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top