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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
MizeelA
Helper I
Helper I

DateDiff Issue with Month Calculation

Hi All,

 

Trying to calculate the month-to-month date difference in Power BI, however, the result is inaccurate for some dates that fall on the last day of the month (Begin - End dates), while it calculates the correct date in Excel. The lines marked in green in Excel, for example, give the correct date as shown below;

In Excel

Formulae:  

=IF(DATEDIF(M7,N7,"m")=0,1,DATEDIF(M7,N7,"m"))

 

MizeelA_3-1645708048198.png

 

In power bi

Formulae: 

Order_Term Main = IF(DATEDIFF('GP Data'[PAFBeginDate],'GP Data'[PAFEndDate - Copy],MONTH)=0,1,(DATEDIFF('GP Data'[PAFBeginDate],'GP Data'[PAFEndDate - Copy],MONTH)))
MizeelA_4-1645708063510.png

 

Your input will be much appreciated.

 

Kind Regards

Mizeel



1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @MizeelA ,

You can add a custom column in Power Query Editor just as below screenshot:

yingyinr_0-1646104945406.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dYzBDcAwCAN34R2J2DRNOwvK/msEUPqsxOPMgd1lKnsMu7TkN3nIai5UPpEwwwTnIVkGVNzfk+lIvH6NnbYKqL3hlK0N", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [PAFBeginDate = _t, #"PAFEndDate - Copy" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"PAFBeginDate", type date}, {"PAFEndDate - Copy", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Order_Term Main", each Number.RoundDown(Number.From(([#"PAFEndDate - Copy"] - [PAFBeginDate])/( 365.25 / 12 )) ,0 ))
in
    #"Added Custom"

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
MizeelA
Helper I
Helper I

Thanks, @johnt75  for the explanation

v-yiruan-msft
Community Support
Community Support

Hi @MizeelA ,

You can add a custom column in Power Query Editor just as below screenshot:

yingyinr_0-1646104945406.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dYzBDcAwCAN34R2J2DRNOwvK/msEUPqsxOPMgd1lKnsMu7TkN3nIai5UPpEwwwTnIVkGVNzfk+lIvH6NnbYKqL3hlK0N", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [PAFBeginDate = _t, #"PAFEndDate - Copy" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"PAFBeginDate", type date}, {"PAFEndDate - Copy", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Order_Term Main", each Number.RoundDown(Number.From(([#"PAFEndDate - Copy"] - [PAFBeginDate])/( 365.25 / 12 )) ,0 ))
in
    #"Added Custom"

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you for your help @v-yiruan-msft . The solution worked perfectly. 

johnt75
Super User
Super User

DATEDIFF in DAX doesn't have a concept of whole months, it simply counts the number of boundaries crossed between the two dates, so DATEDIFF( DATE(2018, 2, 28), DATE(2018, 3, 1), MONTH) returns 1 because 1 month boundary has been crossed, even though there is only 1 day between the 2 dates.

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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