Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Calculate number of months between two dates in PowerQuery

How do I calculate the number of months between two dates in PowerQuery? I can figure it out in DAX but can't seem to find the syntax to make it work in PowerQuery as a custom coloumn.

 

For example:

Start Date: 01/12/2020

End Date: 01/03/2020

= 4 months

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

It is easiest to use DATEDIFF with MONTH on the DAX side, but in Power Query you can use the formula below.  It isn't exact but may work for you.

 

= Duration.TotalDays([EndDate]-[StartDate])/30

 

If within the same year, you can use Date.Month([EndDate]) - DateMonth([StartDate])

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

6 REPLIES 6
Krishna_
New Member

I know this is very old thread, i've landed here searching for a quick solution. 

I found none are really useful. So i've figured out by myself. Below is the solution.

 

Number.Abs(Date.Year(Date.EndOfMonth([To]))+Date.Month(Date.EndOfMonth([To]))/12-Date.Year([From])-Date.Month([From])/12)*12 + 1

Anonymous
Not applicable

All the answers here are bad.

 

Correct answer can be see in this questions comments (User lbendlin:)

https://community.powerbi.com/t5/Power-Query/Date-difference-in-Months-Power-Query/td-p/1993216

 

Extract from that:

 

Date.Year([Date2])*12+Date.Month([Date2])-Date.Year([Date1])*12-Date.Month([Date1])

 

Ashish_Mathur
Super User
Super User

Hi,

See if this helps - Generating Rows by Month for Date Ranges in Power Query | by Daniel Marsh-Patrick | Daniel Marsh-Pat...


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
mahoneypat
Microsoft Employee
Microsoft Employee

The first one works across years.  Please share that part of your M code to troubleshoot.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


mahoneypat
Microsoft Employee
Microsoft Employee

It is easiest to use DATEDIFF with MONTH on the DAX side, but in Power Query you can use the formula below.  It isn't exact but may work for you.

 

= Duration.TotalDays([EndDate]-[StartDate])/30

 

If within the same year, you can use Date.Month([EndDate]) - DateMonth([StartDate])

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Thanks @mahoneypat , I've tried both those solutions but they don't work as it's not accurate and goes over various years.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.