Months Between Two Dates Syntax

Simonc64

Active Member
Joined
Feb 15, 2007
Messages
251
Office Version
  1. 365
Hi All Im a complete novice using power query in Excel and giving it a go, but this has stumped me.

Im trying to find the number of months between a persons date of birth and TODAY() - and then how to turn that into a variable

Any help welcomed - thank you !
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
idfirst_nameDOBMonhs
1Ira5/13/2007195
2Veronique8/24/2008180
3Carole6/6/2011147
4Giovanni11/30/2011141
5Drucie9/4/201684
6Gordan8/13/2014108
7Laure10/27/201946
8Percy8/17/2003240
9Jaye10/28/2004226
10Tamas12/21/201592
11Ibby1/2/2011152
12Danny3/19/202041
13Merrile11/18/2013117
14Flss12/13/201944
15Connie8/14/2010156
16Elita2/9/2009174
17Aili5/25/2004231
18Marena7/25/2007193
19Sandro12/14/2012128
20Bradley12/7/2013117
21Alta8/17/2003240
22Daron7/2/2002254
23Vale12/28/2001260
24Analise11/12/201681
25Gelya10/22/201946


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DOB", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Today", each DateTime.LocalNow()),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Today", type date}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Months", each Number.Round(Number.From(([Today] - [DOB])/( 365.25 / 12 )) ,0 )),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Today"})
in
    #"Removed Columns1"
 
Upvote 0
A little bit different than @alansidman's nice solution. Using the Power Query interface.

You have the following table in a worksheet:
1692955125593.png


While you have a cell selected in this range, Ribbon->Data->Get Data->From Other Sources->From Table/Range (note that there is also an additional button on the ribbon for this option)
1692955165908.png


Confirm the table creation with the headers option is selected:
1692955267511.png


The Power Query interface is opened:
1692955469719.png


Click on the Add Column ribbon tab, and enter Months for the New column name, then copy and paste the following formula into the formula editor box:
Power Query:
(Date.Year(DateTime.LocalNow())-Date.Year([Date of birth]))*12 + Date.Month(DateTime.LocalNow()) - Date.Month([Date of birth])
1692955684495.png

Click OK, and you are done.

1692955800427.png


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"Date of birth", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Months", each (Date.Year(DateTime.LocalNow())-Date.Year([Date of birth]))*12 + Date.Month(DateTime.LocalNow()) - Date.Month([Date of birth]))
in
    #"Added Custom"
 
Upvote 0
A little bit different than @alansidman's nice solution. Using the Power Query interface.

You have the following table in a worksheet:
View attachment 97750

While you have a cell selected in this range, Ribbon->Data->Get Data->From Other Sources->From Table/Range (note that there is also an additional button on the ribbon for this option)
View attachment 97751

Confirm the table creation with the headers option is selected:
View attachment 97752

The Power Query interface is opened:
View attachment 97755

Click on the Add Column ribbon tab, and enter Months for the New column name, then copy and paste the following formula into the formula editor box:
Power Query:
(Date.Year(DateTime.LocalNow())-Date.Year([Date of birth]))*12 + Date.Month(DateTime.LocalNow()) - Date.Month([Date of birth])
View attachment 97756
Click OK, and you are done.

View attachment 97757

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"Date of birth", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Months", each (Date.Year(DateTime.LocalNow())-Date.Year([Date of birth]))*12 + Date.Month(DateTime.LocalNow()) - Date.Month([Date of birth]))
in
    #"Added Custom"
Thank you so much for this alternative - I have a lot to learn - I will try this way too !
 
Upvote 0

Forum statistics

Threads
1,223,319
Messages
6,171,427
Members
452,402
Latest member
siduslevis

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