Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
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:
Solved! Go to Solution.
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] ) )
)
)
@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 ()
)
)
)
)
@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
)
)
)
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:
The measure I've edited based on your answer above:
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:
DAX Formula:
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
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 🙂
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:
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:
@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
)
)
)
Great! Thank you so much for the help! I really appreciate it!
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
15 | |
11 | |
8 | |
8 | |
8 |
User | Count |
---|---|
22 | |
13 | |
11 | |
10 | |
10 |