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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
bparikh
Regular Visitor

Date difference in Months - Power Query

Hello Experts,

 

I've two DateTime columns and am trying to find the difference in Months by adding a new column but getting an error.

I'm using = Date.Month ([Date1] - [Date2])

Date1 & Date2 both are - DateTime format

 

The erros is mentioned below,

Expression.Error: The Date value must contain the Date component.
Details:
974.00:00:00

 

Any help will be really appreciated.

 

Thanks in advance.

1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@bparikh 

if you need the number of months between the dates then add the following, it should work for Date/Time

 = Number.Round(Number.From(([Date 2] - [Date 1])/( 365.25 / 12 )) ,0 )

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

9 REPLIES 9
LeTapia
New Member

What about this function

(Date1 as date , Date2 as date)=>
let
    Source =
            List.Generate(
                () => [y=Date.From(Date1)],
                each [y] <= Date.From(Date2),
                each [y = Date.AddMonths([y] ,1)],
                each [y]
            ),
    Months=List.Count(Source)
in
    Months
lbendlin
Super User
Super User

The correct answer is 

 

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

I want to calculate datediff, for example if start date is 4may and end date is 4june then if I select may month it should return days only from 4may to31 may, if I select June it should give 1june to 4june only. Please help me with this, 

rmsolfest
Advocate I
Advocate I

This is a little late response, but some may still find it helpful.

 

These can be standard columns in one's common date table for easy reference, filtering & and calculating offsets (including month difference).

diff_Year = [Year] - Date.Year( DateTime.LocalNow() )


diff_Month = 12 * ( [Year] - Date.Year( DateTime.LocalNow() ) ) + [Month Number] - Date.Month(DateTime.LocalNow())

Fowmy
Super User
Super User

@bparikh 

if you need the number of months between the dates then add the following, it should work for Date/Time

 = Number.Round(Number.From(([Date 2] - [Date 1])/( 365.25 / 12 )) ,0 )

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Wow - it worked ! Thanks a lot Fowmy.

 

Just curious, what was wrong in my approach. 

Subtracting dates in Power Query doesn't return days @bparikh - it returns a duration, that is why I used the Duration function in my solution. Fowmy converted the dates to numbers (same numbers you'd see in Excel with an unformatted date) to do the days. You should check out the Duration functions like Duration.TotalDays I used. They are a bit more flexible when a time component comes in, and can be very powerful. Also helps you understand how Power Query handles dates, times, and the differences between them.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
edhans
Super User
Super User

There is no Power Query function like DATEDIFF in DAX, so there are at least two ways to approach this.

 

= Duration.TotalDays(Duration.From([Date2] - [Date1]))

 

That would return the total days. You could then do something like:

 

= Number.IntegerDivide( TheTotalDays, 30)

 

That would give you an approximation of the months. 

If you want to count the actual months, you could do this:

 

                let
                    varTimePeriod1 = Date.Year([Date1]) * 100 + Date.Month([Date1]),
                    varTimePeriod2 = Date.Year([Date2]) * 100 + Date.Month([Date2])
                in
                varTimePriod2 - varTimePeriod1

 

That would turn May 1, 2021 to 202105, and August 15, 2021 to 202108. Then 202108-202105 = 3 months.

But note that it would also return 1 month for a June 30 and July 1 date difference, and 0 months for June 1 and June 30, so it depends on your scenario as to whether the counting days in the first example or just the year/month combo is more relevant to your situation.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

This was a great solution, but didn't quite work for me. I made a slight modification for my needs.

With the provided code, there were some issues when comparing a date in Dec to a date in Jan. I wanted those to show up as only 1 month difference.

So instead I calculated the number of months since the year 0, and subtracted the difference between those.


let
today = Date.From(DateTime.LocalNow()),
varTimePeriod1 = Date.Year(today) * 12 + Date.Month(today),
varTimePeriod2 = Date.Year([Date]) * 12 + Date.Month([Date])

in
varTimePeriod2 - varTimePeriod1

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors