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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors