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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
dokat
Post Prodigy
Post Prodigy

Dynamic measure based on slicer selection

Hi,

 

I'd like to calculate dynamic measure based on slicer selection. I have 8 values in slicer "Off Invoice Sales,Net Sales, Trade Spend, Volume, Packages, ASP, Gross Profit". Based on slicer selection i would like to execute different measures. For Ex: If Off Invoice Sales is selected i would like to return {Output OIS] measure and so on. I am using measures in stacked column chart 

I tried below dax code however it returns error. See screenshot of the error below. Is there a workaround this issue?

Output Selected Calculation = 
SWITCH(
  SELECTEDVALUE('Output'[P&L]),
   IF(SELECTEDVALUE('Output'[P&L]) = "Off Invoice Sales", CALCULATE([Output OIS])),
   IF(SELECTEDVALUE('Output'[P&L]) = "Net Sales", CALCULATE([Output NS])),
   IF(SELECTEDVALUE('Output'[P&L]) = "Trade Spend", CALCULATE([Output Trade Spend])),
   IF(SELECTEDVALUE('Output'[P&L]) = "Volume", CALCULATE([Output Volume])),
   IF(SELECTEDVALUE('Output'[P&L]) = "Packages", CALCULATE([Output Packages])),
   IF(SELECTEDVALUE('Output'[P&L]) = "ASP",  CALCULATE([Output ASP])),
   IF(SELECTEDVALUE('Output'[P&L]) = "Gross Profit", CALCULATE([Output GP]))
)

 Screenshot 2023-08-13 213240.png

1 ACCEPTED SOLUTION

Output Selected Calculation = 
SWITCH(
  TRUE(),
   SELECTEDVALUE('Output'[P&L]) = "Off Invoice Sales", [Output OIS],
   SELECTEDVALUE('Output'[P&L]) = "Net Sales", [Output NS],
   SELECTEDVALUE('Output'[P&L]) = "Trade Spend", [Output Trade Spend],
   SELECTEDVALUE('Output'[P&L]) = "Volume", [Output Volume],
   SELECTEDVALUE('Output'[P&L]) = "Packages", [Output Packages],
   SELECTEDVALUE('Output'[P&L]) = "ASP",  [Output ASP],
   SELECTEDVALUE('Output'[P&L]) = "Gross Profit", [Output GP],
BLANK()
)

 

Sorry I missed brackets for selectedvalue. Now it will work. 

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

 

View solution in original post

4 REPLIES 4
Kishore_KVN
Super User
Super User

Hello @dokat ,

No need to use IF and CALCULATE inside a SWITCH statement simply you can mention column name and measure names respectively. Your measure should look as below:

Output Selected Calculation = 
SWITCH(
  TRUE(),
   SELECTEDVALUE('Output'[P&L] = "Off Invoice Sales", [Output OIS],
   SELECTEDVALUE('Output'[P&L] = "Net Sales", [Output NS],
   SELECTEDVALUE('Output'[P&L] = "Trade Spend", [Output Trade Spend],
   SELECTEDVALUE('Output'[P&L] = "Volume", [Output Volume],
   SELECTEDVALUE('Output'[P&L] = "Packages", [Output Packages],
   SELECTEDVALUE('Output'[P&L] = "ASP",  [Output ASP],
   SELECTEDVALUE('Output'[P&L] = "Gross Profit", [Output GP],
BLANK()
)

 

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

@Kishore_KVN Wen i tried your formula its giving me following error message 
Too many arguments were passed to the SELECTEDVALUE function. The maximum argument count for the function is 2.

Output Selected Calculation = 
SWITCH(
  TRUE(),
   SELECTEDVALUE('Output'[P&L]) = "Off Invoice Sales", [Output OIS],
   SELECTEDVALUE('Output'[P&L]) = "Net Sales", [Output NS],
   SELECTEDVALUE('Output'[P&L]) = "Trade Spend", [Output Trade Spend],
   SELECTEDVALUE('Output'[P&L]) = "Volume", [Output Volume],
   SELECTEDVALUE('Output'[P&L]) = "Packages", [Output Packages],
   SELECTEDVALUE('Output'[P&L]) = "ASP",  [Output ASP],
   SELECTEDVALUE('Output'[P&L]) = "Gross Profit", [Output GP],
BLANK()
)

 

Sorry I missed brackets for selectedvalue. Now it will work. 

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

 

@Kishore_KVN Thank you. It worked for the most part except when i select ASP in slicer it returns blank values. Below is the formula i use for [Output ASP]. Do you know what may cause this issue?

 

 

Output ASP = DIVIDE([Output OIS],[Output Volume])

 

Output OIS = CALCULATE(SUMX(FILTER('Output', 'Output'[P&L] = "Off Invoice Sales" && NOT(ISBLANK('Output'[Values]))), 'Output'[Values]))
Output Volume = CALCULATE(SUMX(FILTER('Output','Output'[P&L] = "Volume"), 'Output'[Values]), NOT(ISBLANK('Output'[Values])))

  

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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