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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
SRJPBI
Frequent Visitor

Is it possible to replace variables in measures with a filter?

I have two measures. The first one calculates sales from the year selected in a slicer:

 

Sales_this_year =
VAR V_year = SELECTEDVALUE(my_table[YEAR])
RETURN
CALCULATE(SUM(my_table[SALES]),
my_table[YEAR] = V_year)

 

The second one calculates sales from the previous year; the only change to the DAX is subtracting 1 from the year selected in the slicer:

 

Sales_last_year =
VAR V_year = SELECTEDVALUE(my_table[YEAR])-1
RETURN
CALCULATE(SUM(my_table[SALES]),
my_table[YEAR] = V_year)

 

The two measures are then displayed on my report in separate cards.

 

Having two measures that are so similar seems inefficient, especially when scaled up to a full report with many measures like this showing this year's and last year's results.

 

Is it possible to create a single measure like this, and display it in both cards with filters on each card to determine the year each should be looking at?

 

Sales = SUM(my_table[SALES])

 

I'm also open to different ideas if anyone can think of a better way.

1 REPLY 1
AmiraBedh
Super User
Super User

Can you try the following ?

Sales = CALCULATE(SUM(my_table[SALES]), ALL(my_table), FILTER(my_table, my_table[YEAR] = SELECTEDVALUE(my_table[YEAR])))

The measure uses the CALCULATE function to calculate the sum of sales, but it also uses the ALL function to remove any filters that are applied to the table. This means that the measure will calculate the sum of sales for all years, regardless of which year is selected in the slicer.

 

For example, to create a card that shows sales for this year, you would use the following formula:

Sales This Year = CALCULATE(SUM(my_table[SALES]), ALL(my_table), FILTER(my_table, my_table[YEAR] = SELECTEDVALUE(my_table[YEAR])))

 

Another approach would be to use a parameter to determine the year to calculate sales for. This would allow you to create a single measure that can be used to calculate sales for any year, without having to change the formula.

To do this, you would first create a parameter for the year. Then, you would use the CALCULATE function with the FILTER function to calculate sales for the selected year.

 

Sales = CALCULATE(SUM(my_table[SALES]), FILTER(my_table, my_table[YEAR] = SELECTEDVALUE(parameter_year)))

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.