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

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

Reply
Anonymous
Not applicable

Calculatewith slicer applied

Hello,

 

I would like to calculate our margin year over year. I had a formula that worked for the current year.

 

CurrentMargin =
var som = SUM(Table[salesExVAT]) - SUM(table[costprice])
return DIVIDE(som,SUM(table[SalesExVAT]))
 

Last year margin =

var LYmargin = CALCULATE(SUM(Table[costprice]),YEAR(Table[Date])=YEAR(TODAY())-1)
return DIVIDE(LYmargin,[PrevYear]) (PrevYear = sum of turnover last year)
 
Margin YOY =
var som = [CurrentMargin]-[LastyearMargin]
return DIVIDE(som,[LastyearMargin])
 
This gave the result that I needed. But I have added a slicer on the top of my page with year which can be selected.
Now I need the or a formula to auto adjust to the year selected. So if I choose 2021, it has to look to that previous year (2020), but when I select 2019, it has to look to that previous year.
 
What kind of DAX formula is needed to create this?
 
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

 

You want to use the slicer to select the year to return the value of the previous year of the selected year, right? Then you need a separate year table, which has nothing to do with the main table.

Here's my example.

Sample data:

vstephenmsft_4-1638347593033.png

vstephenmsft_3-1638347576008.png

vstephenmsft_0-1638347550847.png

 

1.Create a measure.

PreviousYear Value = CALCULATE(SUM('Table'[Value]),FILTER('Table',[Year]=SELECTEDVALUE('Table (2)'[Year])-1))

2.Create a slicer with the year field from Table(2). In the table visual, the year field is from Table. When I selcet year 2020, it shows the value in 2019.

vstephenmsft_6-1638347741613.png

 

 

Best Regards,

Stephen Tao

 

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

 

View solution in original post

4 REPLIES 4
lbendlin
Super User
Super User

You may be slightly overcomplicating this

 

LastYearMargin = CALCULATE([CurrentYearMargin],SAMEPERIODLASTYEAR(Table[Date]))

 

(although this will work more reliably if you have a true calendar table)

Anonymous
Not applicable

@lbendlinthanks for your response!

I can definitely try that. But will that also work with a slicer like this?

Thomdh_0-1638175458109.png

Because if I use the measure [CurrentYear] it will always return 2021 right? Even if the slicer 2020 is selected.

 

Anonymous
Not applicable

Hi @Anonymous ,

 

You want to use the slicer to select the year to return the value of the previous year of the selected year, right? Then you need a separate year table, which has nothing to do with the main table.

Here's my example.

Sample data:

vstephenmsft_4-1638347593033.png

vstephenmsft_3-1638347576008.png

vstephenmsft_0-1638347550847.png

 

1.Create a measure.

PreviousYear Value = CALCULATE(SUM('Table'[Value]),FILTER('Table',[Year]=SELECTEDVALUE('Table (2)'[Year])-1))

2.Create a slicer with the year field from Table(2). In the table visual, the year field is from Table. When I selcet year 2020, it shows the value in 2019.

vstephenmsft_6-1638347741613.png

 

 

Best Regards,

Stephen Tao

 

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

 

No. [CurrentYear] has nothing to do with 2021. It is based on the current filter context for each element of your visual.

 

You might consider renaming the measure to reduce the ambiguity.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.