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

Show last 2 years by slicer

Hey,
I need to create a stacked bar chart where year and month slicer is there and chart will show last 2 year based on year selection i.e if 2022 is selected then data for 2021 and 2022 should be shown so in x-axis 2021 and 2022 will be there in different columns.Also month slicer setting is such that data is calculated for that month.Also there is a column legend for size.I do have a date table in relationship with main table as one to many.

Any ideas on how I can get this to work in Power BI ?

Thanks in advance,
Shruti

3 REPLIES 3
v-yangliu-msft
Community Support
Community Support

Hi  @shrutik ,

 

Here are the steps you can follow:

1. Create measure.

Flag =
var _selectyear=
SELECTEDVALUE('Date'[Date].[Year])
var _selectmonth=
SELECTEDVALUE('Date'[Date].[MonthNo])
var _laststart2=
DATE(
    _selectyear -1,1,1)
var _lastend2=
EOMONTH(
DATE(
    _selectyear-1,_selectmonth,1),0)
var _laststart1=
DATE(
    _selectyear ,1,1)
var _lastend1=
EOMONTH(
DATE(
    _selectyear,_selectmonth,1),0)
return
IF(
    AND(MAX('Table'[Date]) >=_laststart2 ,MAX('Table'[Date]) <=_lastend2)
    ||
    AND(MAX('Table'[Date]) >=_laststart1 ,MAX('Table'[Date]) <=_lastend1) ,1,0)

2. Place [Flag]in Filters, set is=1, apply filter.

vyangliumsft_0-1678414521780.png

3. Result:

vyangliumsft_1-1678414521784.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Hii,
This does not works when tables are in a relationship.Is there a way for it to work when relationships are there?

amitchandak
Super User
Super User

@shrutik , for that the slicer needs to be on an independent date/year table

 

//Date1 is independent Date table, Date is joined with Table
new measure =
var _max = maxx(allselected(Date1),Date1[Year])
var _min = _max -1
return
calculate( sum(Table[Value]), filter('Date', 'Date'[Year] >=_min && 'Date'[Year] <=_max))

 

Need of an Independent Date Table:https://www.youtube.com/watch?v=44fGGmg9fHI

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.