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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

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
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors