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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Zozozop
Frequent Visitor

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

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super 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
        )
    )

View solution in original post

1 REPLY 1
AlexisOlson
Super User
Super 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
        )
    )

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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