Wednesday, 26 February 2020

How to calculate the actual interval boundaries between months using DATEDIFF in DAX

When i was writing a DAX code to return the difference between dates in months i found DATEDIFF function is very useful. But when i found the visualization sometimes does not produce expected results, after analyzing i found that if the selected date period is fallen in same month but from first date of the month to the last date of same month, datediff for Month shows 0.







How  do you get the month difference if the difference between two dates is less than a month?

Below is the DAX code that you can use to get more accurate figure for interval boundaries between dates. If you want to see the exact number it produces then reformat to decimal places and see how code behaves.






M_PY_Rate =
VAR startDate =
CALCULATE ( MIN ( dimDate[FullDate] ), ALLSELECTED ( dimDate[FullDate] ) )
VAR endDate =
CALCULATE ( MAX ( dimDate[FullDate] ), ALLSELECTED ( dimDate[FullDate] ) )
VAR diff =
( 1
* ( endDate - startDate) )
/ DAY ( EOMONTH ( endDate, 0 ) )
--DATEDIFF ( endDate, startDate, MONTH ) (Archived)
RETURN
CALCULATE (
[M_SI_RATE],
PARALLELPERIOD ( dimDate[FullDate], -(diff), MONTH )
)

No comments:

Post a Comment