cancel
Showing results for
Did you mean:

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Helper I

## DATEDIFF to calculate number of months if Start date of month varies?

1 ACCEPTED SOLUTION
Community Champion

Hi @DCrowther

Please see the below DAX expressions.

```Payment Month Count =
VAR _NextSheduledDate = 'Table'[Next Scheduled Payment]
VAR _NextSheduledYear = YEAR( _NextSheduledDate )
VAR _TaxCutoffDate = IF( DATE( _NextSheduledYear, 4, 5 ) > _NextSheduledDate, DATE( _NextSheduledYear, 4, 5 ), DATE( _NextSheduledYear + 1, 4, 5 ) )
VAR _DateDiff = DATEDIFF( _NextSheduledDate, _TaxCutoffDate, MONTH )
RETURN _DateDiff + IF( DAY( _NextSheduledDate ) <= 5, 1, 0 )```

2 Version with hard coded Year

```Payment Month Count =
VAR _NextSheduledDate = 'Table'[Next Scheduled Payment]
VAR _DateDiff = DATEDIFF( _NextSheduledDate, DATE( 2020, 4, 5 ), MONTH )
RETURN _DateDiff + IF( DAY( _NextSheduledDate ) <= 5, 1, 0 )```

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.

4 REPLIES 4
Helper I

Sorry, i didnt get to explain the content of my query

I am looking to calculat the number of months between a start date (payment date) and the the end date (end date being UK Tax year of 5/4/2020)

However there are set different start dates (Payment Date) so for example, so may be on on the 1st of the month, some maybe on 15th of the month and some on the 25th, however the DAX DATEDIFF function may result in a slight mis-calculation of payment months remaining depending on the start, can anyone help with this?

I have included below an excel formula that actually works so i am looking for the DAX equivalent

=MONTH(F5)-MONTH(E5)+12*(YEAR(F5)-YEAR(E5))+IF(DAY(F5)>=DAY(E5),1,0)

Regards

Dan

Community Champion

Can you creater a small data sample + expected result?

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.

Helper I

Hi Mariusz

Above is the two examples, first image shows how PBI is resulting in a 7 month count no matter if the start date is on the 1st, 15th or 25th. The second image shows how excel can give a different month number calc of 8, depending if the day of the month changes. 8 months should be the correct output.

Hope this helps

Regards

Dan

Community Champion

Hi @DCrowther

Please see the below DAX expressions.

```Payment Month Count =
VAR _NextSheduledDate = 'Table'[Next Scheduled Payment]
VAR _NextSheduledYear = YEAR( _NextSheduledDate )
VAR _TaxCutoffDate = IF( DATE( _NextSheduledYear, 4, 5 ) > _NextSheduledDate, DATE( _NextSheduledYear, 4, 5 ), DATE( _NextSheduledYear + 1, 4, 5 ) )
VAR _DateDiff = DATEDIFF( _NextSheduledDate, _TaxCutoffDate, MONTH )
RETURN _DateDiff + IF( DAY( _NextSheduledDate ) <= 5, 1, 0 )```

2 Version with hard coded Year

```Payment Month Count =
VAR _NextSheduledDate = 'Table'[Next Scheduled Payment]
VAR _DateDiff = DATEDIFF( _NextSheduledDate, DATE( 2020, 4, 5 ), MONTH )
RETURN _DateDiff + IF( DAY( _NextSheduledDate ) <= 5, 1, 0 )```

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.

Announcements

#### 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 Monthly Update - June 2024

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

#### New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

Top Solution Authors
Top Kudoed Authors