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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Anonymous
Not applicable

How to have a line chart that responds to a slicer ?

Hi everyone, 

A power BI newbie here. 

Table which I have has month-wise counts from July,2018. Data goes untill December 2020.

 

I have a slicer which selects the month and the year for the other visuals on the page. 

 

I need to create a line chart which dynamically shows last 2 years of data or 24 months of data. So say suppose if I select, November 2020 from slicers, I need to see a graph that starts from November 2018 and ends at Novmeber 2020. I need to have a text - "Current year" and "Previous Year" in the legend which is also another challenge that I am facing. 

 

Below is the image for reference. 

 

Query.png

 Please help me how to write the correct dax here. I am totally out of ideas

 

3 REPLIES 3
amitchandak
Super User
Super User

@Anonymous , You have to follow this independent table approch

https://www.youtube.com/watch?v=duMSovyosXE

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

When I use this approach, I cannot have an appropriate legend. What I want to create is the following. 
Suppose the slicer selection is November, so what should be seen in the visual is a line for November 2019 - November 2020 and then a differnt line for November 2018 to November 2019, with legend text as "current year" and "previous year". Otherwise it looks like thisQuery 2.png

@Anonymous , try measures like

 

Last 12 =
var _max = maxx(allselected([IDate]), 'IDate'[Date])
var _min = date(year(_max)-1,month(_max),1)
return
calculate(sum(table[value]), filter('Date', 'Date'[Date] >= _min && 'Date'[Date] <=_max))

 


Last 12 to Last 12 =
var _max1 = maxx(allselected([IDate]), 'IDate'[Date])
var _max = eomonth(date(year(_max1)-1,month(_max1)-1,1),0)
var _min = date(year(_max)-1,month(_max),1)
return
calculate(sum(table[value]), filter('Date', 'Date'[Date] >= _min && 'Date'[Date] <=_max))

 

where Idate is independent date table

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Kudoed Authors