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
ironboy0418
Helper II
Helper II

How to create a custom relative date slicer using DAX?

Hello, I am trying to create a relative date slicer but I am not getting the desired results to filter the table. Here is my DAX formula: 

 

RelativePeriod =
VAR SelectedPeriod =
    SELECTEDVALUE (Period[Period])
VAR DynamicPeriod =
    SWITCH (
        TRUE(),
        SelectedPeriod = "Yesterday", today()-1,
        SelectedPeriod = "MTD", DATESMTD('Date'[CalendarDate]),
        SelectedPeriod = "QTD", DATESQTD('Date'[CalendarDate]),
        SelectedPeriod = "YTD", DATESYTD('Date'[CalendarDate]),
        SelectedPeriod = "Last Year", PREVIOUSYEAR('Date'[CalendarDate]),
        BLANK()
    )
RETURN
    DynamicPeriod
 
ironboy0418_0-1678773005587.png

 

3 ACCEPTED SOLUTIONS

@ironboy0418 

Please try

 

RelativePeriod =
SWITCH (
    SELECTEDVALUE ( Period[Period] ),
    "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    "MTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN FILTER (
                        DATESMTD ( 'Date'[CalendarDate] ),
                        FORMAT ( 'Date'[CalendarDate], "YYYYMM" ) = FORMAT ( TODAY (), "YYYYMM" )
                    )
            )
        ),
    "QTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN FILTER (
                        DATESQTD ( 'Date'[CalendarDate] ),
                        FORMAT ( 'Date'[CalendarDate], "YYYYQ" ) = FORMAT ( TODAY (), "YYYYQ" )
                    )
            )
        ),
    "YTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN FILTER (
                        DATESYTD ( 'Date'[CalendarDate] ),
                        YEAR ( 'Date'[CalendarDate] ) = YEAR ( TODAY () )
                    )
            )
        ),
    "Last Year",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN PREVIOUSYEAR ( 'Date'[CalendarDate] ) )
        )
)

 

View solution in original post

@ironboy0418 
I guess the following would solve all scenarios 

RelativePeriod =
SWITCH (
    SELECTEDVALUE ( Period[Period] ),
    "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    "MTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESMTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "QTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESQTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "YTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESYTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "Last Year",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        PREVIOUSYEAR ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        )
)

View solution in original post

@ironboy0418 
OMG! It is a very stupid mistake from my side. Please try below. I also added < before the = just in case the date of TODAY is not part of the date table.

RelativePeriod =
SWITCH (
    SELECTEDVALUE ( Period[Period] ),
    "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    "MTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESMTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "QTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESQTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "YTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESYTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "Last Year",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        PREVIOUSYEAR ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "Last Week",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[Week Rank]
                    = CALCULATE (
                        MAX ( 'Date'[Week Rank] ),
                        'Date'[CalendarDate] <= TODAY (),
                        ALL ( 'Date' )
                    ) - 1
            )
        )
)

View solution in original post

19 REPLIES 19
tamerj1
Super User
Super User

Hi @ironboy0418 
Place the measure in the filter pane of the table visual and select "is not blank" then apply the filter

RelativePeriod =
SWITCH (
    SELECTEDVALUE ( Period[Period] ),
    SelectedPeriod = "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    SelectedPeriod = "MTD",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN DATESMTD ( 'Date'[CalendarDate] ) )
        ),
    SelectedPeriod = "QTD",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN DATESQTD ( 'Date'[CalendarDate] ) )
        ),
    SelectedPeriod = "YTD",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN DATESYTD ( 'Date'[CalendarDate] ) )
        ),
    SelectedPeriod = "Last Year",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN PREVIOUSYEAR ( 'Date'[CalendarDate] ) )
        )
)

@tamerj1 I did. However, it returns this error:

ironboy0418_0-1678780377111.png

 

The measure I've edited based on your answer above:

RelativePeriod =
VAR SelectedPeriod =
    SELECTEDVALUE (Period[Period])
VAR DynamicPeriod =
    SWITCH (
SELECTEDVALUE ( Period[Period] ),
    SelectedPeriod = "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    SelectedPeriod = "MTD",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN DATESMTD ( 'Date'[CalendarDate] ) )
        ),
    SelectedPeriod = "QTD",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN DATESQTD ( 'Date'[CalendarDate] ) )
        ),
    SelectedPeriod = "YTD",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN DATESYTD ( 'Date'[CalendarDate] ) )
        ),
    SelectedPeriod = "Last Year",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN PREVIOUSYEAR ( 'Date'[CalendarDate] ) )
        )
    )
RETURN
    DynamicPeriod
 
ironboy0418_1-1678780458166.png

 

 

@ironboy0418 

My mistake.

RelativePeriod =
SWITCH (
    SELECTEDVALUE ( Period[Period] ),
    "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    "MTD",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN DATESMTD ( 'Date'[CalendarDate] ) )
        ),
    "QTD",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN DATESQTD ( 'Date'[CalendarDate] ) )
        ),
    "YTD",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN DATESYTD ( 'Date'[CalendarDate] ) )
        ),
    "Last Year",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN PREVIOUSYEAR ( 'Date'[CalendarDate] ) )
        )
)

@tamerj1 I did copy the formula above however, it doesn't return the desired result. As I selected MTD, it should filter the March MTD dates only:

ironboy0418_0-1678782473529.png

 

DAX Formula:

ironboy0418_2-1678782618575.png

 

 

 

Hi @ironboy0418 
Can you please in little more details what should be the expected results?

 My apologies. When I select MTD in the slicer it should return the dates only for March 1, 2023 to date. However, it returns all of the dates from January 2022. Same goes with YTD 

 

Not sure but it works when I select 'yesterday' in the slicer:

 

Maybe, the dax formula that uses IN?

 

 

COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] in DATESMTD ( 'Date'[CalendarDate] ) )
        )

 

ironboy0418_1-1678784999304.png

ironboy0418_0-1678784969018.png

@tamerj1

@ironboy0418 
So YTD and MTD should return the date starting from the year of today and the month of today respectively. Right? What about other slicer options? Any issues?

Yes correct. Same issue with 'Last Year' data. I initially write dax formula for Yesterday, MTD, QTD, YTD, and Last Year. Will update when we figure out the solution 🙂

@ironboy0418 

Please try

 

RelativePeriod =
SWITCH (
    SELECTEDVALUE ( Period[Period] ),
    "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    "MTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN FILTER (
                        DATESMTD ( 'Date'[CalendarDate] ),
                        FORMAT ( 'Date'[CalendarDate], "YYYYMM" ) = FORMAT ( TODAY (), "YYYYMM" )
                    )
            )
        ),
    "QTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN FILTER (
                        DATESQTD ( 'Date'[CalendarDate] ),
                        FORMAT ( 'Date'[CalendarDate], "YYYYQ" ) = FORMAT ( TODAY (), "YYYYQ" )
                    )
            )
        ),
    "YTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN FILTER (
                        DATESYTD ( 'Date'[CalendarDate] ),
                        YEAR ( 'Date'[CalendarDate] ) = YEAR ( TODAY () )
                    )
            )
        ),
    "Last Year",
        COUNTROWS (
            FILTER ( 'Date', 'Date'[CalendarDate] IN PREVIOUSYEAR ( 'Date'[CalendarDate] ) )
        )
)

 

@tamerj1 It worked for YTD, QTD, MTD! Thank you so much. Will try to do for the previous year

@ironboy0418 
I guess the following would solve all scenarios 

RelativePeriod =
SWITCH (
    SELECTEDVALUE ( Period[Period] ),
    "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    "MTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESMTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "QTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESQTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "YTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESYTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "Last Year",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        PREVIOUSYEAR ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        )
)

@tamerj1Can I also ask how to write dax to filter last week dates? I created a variable but not sure how to execute it inside your dax formula:

 

VAR PREVIOUSWEEK = FILTER(ALL('Date'),'Date'[Week Rank]=MAX('Date'[Week Rank])-1)

@ironboy0418 

RelativePeriod =
SWITCH (
    SELECTEDVALUE ( Period[Period] ),
    "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    "MTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESMTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "QTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESQTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "YTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESYTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "Last Year",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        PREVIOUSYEAR ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "Last Week",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    = CALCULATE (
                        MAX ( 'Date'[Week Rank] ),
                        'Date'[CalendarDate] = TODAY (),
                        ALL ( 'Date' )
                    ) - 1
            )
        )
)

@tamerj1 Thanks for this! However, it returns no result when I select 'Last Week' I'm not sure why. Here's my dax formula for Week Rank:

 

Week Rank = RANKX(ALL('Date'),'Date'[Start of Week],,ASC,Dense)
 
ironboy0418_0-1678833550991.pngironboy0418_1-1678833566779.png

 

@ironboy0418 

what is the last date in your date table

@tamerj1 the last date in my date table is the date today:

ironboy0418_0-1678858165738.png

 

 

 

@ironboy0418 
OMG! It is a very stupid mistake from my side. Please try below. I also added < before the = just in case the date of TODAY is not part of the date table.

RelativePeriod =
SWITCH (
    SELECTEDVALUE ( Period[Period] ),
    "Yesterday",
        COUNTROWS ( FILTER ( 'Date', 'Date'[CalendarDate] = TODAY () - 1 ) ),
    "MTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESMTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "QTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESQTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "YTD",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        DATESYTD ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "Last Year",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[CalendarDate]
                    IN CALCULATETABLE (
                        PREVIOUSYEAR ( 'Date'[CalendarDate] ),
                        'Date'[CalendarDate] = TODAY ()
                    )
            )
        ),
    "Last Week",
        COUNTROWS (
            FILTER (
                'Date',
                'Date'[Week Rank]
                    = CALCULATE (
                        MAX ( 'Date'[Week Rank] ),
                        'Date'[CalendarDate] <= TODAY (),
                        ALL ( 'Date' )
                    ) - 1
            )
        )
)

Thank you so much for this @tamerj1 It worked:

ironboy0418_0-1678863731770.png

 

Great! Thank you so much for the help! I really appreciate it!

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.

Top Solution Authors