How do I create a calculation that applies different filters based on a user-selected slicer option

0 votes

How do I create a calculation that applies different filters based on a user-selected slicer option?
I need a Power BI measure that changes the applied filters dynamically based on a slicer selection. For example, if the user selects "Current Year," the measure should filter data for the current year, and if they select "Last Year," it should filter for the previous year. What is the best way to implement this logic using DAX?

12 hours ago in Power BI by Evanjalin
• 22,610 points
16 views

1 answer to this question.

0 votes

Disconnected Table with DAX Measure using SWITCH() and SELECTEDVALUE() will help you achieve this.

1. Slicer Table:

Create an empty table with just a single column named ''Time Periods.'' Within its rows, insert the following values:

Current Year

Last Year

Last 3 Years

Last 5 Years

Again, this doesn't relate to the data model in your report.

2. Create a DAX Measure

Use SWITCH() For the selected filter:

SalesFiltered =
VAR SelectedPeriod = SELECTEDVALUE('Time Periods'[Period Name])
RETURN
SWITCH(
    SelectedPeriod,
    "Current Year", CALCULATE([Total Sales], YEAR('Sales'[Order Date]) = YEAR(TODAY())),
    "Last Year", CALCULATE([Total Sales], YEAR('Sales'[Order Date]) = YEAR(TODAY()) - 1),
    "Last 3 Years", CALCULATE([Total Sales], 'Sales'[Order Date] >= DATE(YEAR(TODAY()) - 3, 1, 1)),
    [Total Sales]  -- Default case
)

3. Add Slicer & Use Measure

  • Add the Time Periods table as a slicer.

  • Use SalesFiltered in visuals.

answered 12 hours ago by anonymous
• 22,610 points

Related Questions In Power BI

0 votes
0 answers
0 votes
2 answers

How do I create a Power BI visual that dynamically adjusts based on user-selected filters and slicers?

The application of slicers and filters in ...READ MORE

answered Jan 23 in Power BI by anonymous
• 21,730 points
155 views
0 votes
1 answer

How do I create a measure that dynamically switches between different aggregation types based on user selection?

To create a dynamic aggregation measure in ...READ MORE

answered 12 hours ago in Power BI by anonymous
• 22,610 points
9 views
0 votes
1 answer

How can I create a function in Power Query that processes data differently based on a user-selected parameter?

The following steps outline the creation of ...READ MORE

answered Mar 17 in Power BI by anonymous
• 22,610 points
82 views
0 votes
1 answer

Install Power BI Desktop

It’s a pretty simple process. All you ...READ MORE

answered Oct 9, 2018 in Power BI by Kalgi
• 52,350 points
1,212 views
0 votes
1 answer

Few tips before I start creating Power BI dashboard

It’s always advisable to begin with the data ...READ MORE

answered Oct 9, 2018 in Power BI by Kalgi
• 52,350 points
1,030 views
0 votes
1 answer

How do I format the KPI in Power BI

format the KPI by selecting the paint ...READ MORE

answered Oct 9, 2018 in Power BI by Kalgi
• 52,350 points
1,373 views
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer

How do I create a dynamic KPI card that changes color based on user-selected thresholds?

To create a dynamic KPI card that ...READ MORE

answered Mar 17 in Power BI by anonymous
• 22,610 points
71 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP