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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. 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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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