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
Dz6kb4
Frequent Visitor

Different date periods - YTD,current month, previous month in a filter

Hi all.

 

I'm looking to be able to filter my data based on different date periods ie. YTD, current month, previous month.  However, I would like to be able to select one of these from a drop down filter.  is this possible?

 

Many thanks.

1 ACCEPTED SOLUTION
Phil_Seamark
Employee
Employee

Hi @Dz6kb4

 

Sure.  Just create a measure table and don't connect it to anything

 

The table may contain data like :

 

I called my table Date Range

 

 

ID    Value
--     --------
1       This Month
2       Current Month
3       Last Month

 

Then add that table to a slicer but don't create any relationships between this table and your date table.

 

Then create a measure on your Date table that uses DAX to determine if your date is part of the grouping. 

 

Date Range Filter = 
VAR ThisMonth = TODAY()
VAR LastMonth = EOMONTH(TODAY(),-1)
VAR PrevMonth = EOMONTH(TODAY(),-2)
RETURN
SWITCH( 
			TRUE() , 
			-- Current Month
			MAX('Date Range'[ID])=1 
			&& MONTH(MAX('Dates'[Date])) = MONTH(ThisMonth) 
			&& YEAR (MAX('Dates'[Date])) = YEAR(ThisMonth) , 1 ,
			-- Last Month
			MAX('Date Range'[ID])=2 
			&& MONTH(MAX('Dates'[Date])) = MONTH(LastMonth) 
			&& YEAR (MAX('Dates'[Date])) = YEAR(LastMonth) , 1 ,
			-- Last Month
			MAX('Date Range'[ID])=3
			&& MONTH(MAX('Dates'[Date])) = MONTH(PrevMonth) 
			&& YEAR (MAX('Dates'[Date])) = YEAR(PrevMonth) , 1 ,			
			
			-- ELSE
			0)


Then just drag this measure in as a filter on your visual and set that it must Equal = 1

 

Grab this PBIX file for a working copy

 

https://1drv.ms/u/s!AtDlC2rep7a-kHP_xOzy4vBRgbEH

 


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

View solution in original post

3 REPLIES 3
Phil_Seamark
Employee
Employee

Hi @Dz6kb4

 

Sure.  Just create a measure table and don't connect it to anything

 

The table may contain data like :

 

I called my table Date Range

 

 

ID    Value
--     --------
1       This Month
2       Current Month
3       Last Month

 

Then add that table to a slicer but don't create any relationships between this table and your date table.

 

Then create a measure on your Date table that uses DAX to determine if your date is part of the grouping. 

 

Date Range Filter = 
VAR ThisMonth = TODAY()
VAR LastMonth = EOMONTH(TODAY(),-1)
VAR PrevMonth = EOMONTH(TODAY(),-2)
RETURN
SWITCH( 
			TRUE() , 
			-- Current Month
			MAX('Date Range'[ID])=1 
			&& MONTH(MAX('Dates'[Date])) = MONTH(ThisMonth) 
			&& YEAR (MAX('Dates'[Date])) = YEAR(ThisMonth) , 1 ,
			-- Last Month
			MAX('Date Range'[ID])=2 
			&& MONTH(MAX('Dates'[Date])) = MONTH(LastMonth) 
			&& YEAR (MAX('Dates'[Date])) = YEAR(LastMonth) , 1 ,
			-- Last Month
			MAX('Date Range'[ID])=3
			&& MONTH(MAX('Dates'[Date])) = MONTH(PrevMonth) 
			&& YEAR (MAX('Dates'[Date])) = YEAR(PrevMonth) , 1 ,			
			
			-- ELSE
			0)


Then just drag this measure in as a filter on your visual and set that it must Equal = 1

 

Grab this PBIX file for a working copy

 

https://1drv.ms/u/s!AtDlC2rep7a-kHP_xOzy4vBRgbEH

 


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Wow. This is brilliant thanks alot. Do you know what the dax is for year to date to add also?
Many thanks for your help

Hi @Dz6kb4

 

If you want the current year that's easier.

 

 

Date Range Filter = 
VAR ThisMonth = TODAY()
VAR LastMonth = EOMONTH(TODAY(),-1)
VAR PrevMonth = EOMONTH(TODAY(),-2)
RETURN
SWITCH( 
			TRUE() , 
			-- Current Month
			MAX('Date Range'[ID])=1 
			&& MONTH(MAX('Dates'[Date])) = MONTH(ThisMonth) 
			&& YEAR (MAX('Dates'[Date])) = YEAR(ThisMonth) , 1 ,
			-- Last Month
			MAX('Date Range'[ID])=2 
			&& MONTH(MAX('Dates'[Date])) = MONTH(LastMonth) 
			&& YEAR (MAX('Dates'[Date])) = YEAR(LastMonth) , 1 ,
			-- Last Month
			MAX('Date Range'[ID])=3
			&& MONTH(MAX('Dates'[Date])) = MONTH(PrevMonth) 
			&& YEAR (MAX('Dates'[Date])) = YEAR(PrevMonth) , 1 ,
			-- This year
			MAX('Date Range'[ID])=4
			YEAR (MAX('Dates'[Date])) = YEAR(PrevMonth) , 1 ,	
-- ELSE 0)

 


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

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.