Forum Discussion
How do I make my date dynamic?
Last year MTD =
var sply_month_s = DATE(YEAR(today())-1,2,1)
var sply_month_e= DATE(YEAR(TODAY())-1,2,DAY(TODAY())-1)
return
CALCULATE(sum(facttable[premium]),
FILTER (ALL(DimCalendar [Date]),
DimCalendar [Date] >= sply_month_s
&& DimCalendar [Date] <= sply_month_e))
//&& DimCalendar [Date] <= day (TODAY())-1))
This is my dax to calculate MTD but it breaks when I try to make it dynamic, it usually calculates the whole month for now I have hard coded it, how do I make it dynamic, my logic entails that I use today()-1 , so it should calculate from 1 St Feb 2023 to 20th Feb 2023 for last year
You're almost there. You can use MONTH too.
Last year MTD = VAR sply_month_s = DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), 1 ) VAR sply_month_e = DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), DAY ( TODAY () ) - 1 ) RETURN CALCULATE ( SUM ( facttable[premium] ), FILTER ( ALL ( DimCalendar[Date] ), DimCalendar[Date] >= sply_month_s && DimCalendar[Date] <= sply_month_e ) )You may want to rewrite it a bit too:
Last year MTD = VAR _end = TODAY () - 1 VAR _year = YEAR ( _end ) - 1 VAR _month = MONTH ( _end ) VAR _day = DAY ( _end ) VAR sply_month_s = DATE ( _year, _month, 1 ) VAR sply_month_e = DATE ( _year, _month, _day ) RETURN CALCULATE ( SUM ( facttable[premium] ), FILTER ( ALL ( DimCalendar[Date] ), DimCalendar[Date] >= sply_month_s && DimCalendar[Date] <= sply_month_e ) )
1 Reply
- AlexisOlsonSuper User
You're almost there. You can use MONTH too.
Last year MTD = VAR sply_month_s = DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), 1 ) VAR sply_month_e = DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), DAY ( TODAY () ) - 1 ) RETURN CALCULATE ( SUM ( facttable[premium] ), FILTER ( ALL ( DimCalendar[Date] ), DimCalendar[Date] >= sply_month_s && DimCalendar[Date] <= sply_month_e ) )You may want to rewrite it a bit too:
Last year MTD = VAR _end = TODAY () - 1 VAR _year = YEAR ( _end ) - 1 VAR _month = MONTH ( _end ) VAR _day = DAY ( _end ) VAR sply_month_s = DATE ( _year, _month, 1 ) VAR sply_month_e = DATE ( _year, _month, _day ) RETURN CALCULATE ( SUM ( facttable[premium] ), FILTER ( ALL ( DimCalendar[Date] ), DimCalendar[Date] >= sply_month_s && DimCalendar[Date] <= sply_month_e ) )