Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
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.
Solved! Go to Solution.
@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 )
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
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
The correct answer is
Date.Year([Date2])*12+Date.Month([Date2])-Date.Year([Date1])*12-Date.Month([Date1])
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,
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())
@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 )
⭕ 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.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingThere 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.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingThis 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
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
62 | |
40 | |
36 | |
28 | |
15 |