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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors
Top Kudoed Authors