cancel
Showing results for
Did you mean:
Frequent Visitor

## Different DAX calculations based on selected values

Hi Community,
I am having an issue creating a measure to make different calculations depending on the cost center selected, below is a sample data

CC     Value
 803 1 110 2 120 3 803 5

My logic is if 803 is selected on a cost center slicer, it would have a average calculation, if anything that is not 803, then sum of the cost center values. I created the measure below:

``````Test Measure =
VAR selectedValues =
VALUES ( Test[Cost Center] )
RETURN
IF (
CONTAINS ( selectedValues, Test[Cost Center], "803" ),
CALCULATE ( SUM ( Test[Value] ), Test[Cost Center] <> "803" ) + CALCULATE ( AVG ( Test[Value] ), Test[Cost Center] = "803" ),
CALCULATE ( SUM ( Test[Value] ) )
)``````

This works fine if some values are selected in the slicer; however if none are selected or all values are selected, it would give the wrong calculation. Any directions or help would be really appreciated!

1 ACCEPTED SOLUTION
Community Support

``````Test Measure =
VAR selectedValues =
VALUES ('Table'[CC])
RETURN
IF (
CONTAINS ( selectedValues, 'Table'[CC], 803 ),
CALCULATE(AVERAGE('Table'[value]),ALLSELECTED('Table'[CC])),
CALCULATE ( SUM ('Table'[value] ) )
)``````

2 REPLIES 2
Community Support

``````Test Measure =
VAR selectedValues =
VALUES ('Table'[CC])
RETURN
IF (
CONTAINS ( selectedValues, 'Table'[CC], 803 ),
CALCULATE(AVERAGE('Table'[value]),ALLSELECTED('Table'[CC])),
CALCULATE ( SUM ('Table'[value] ) )
)``````

Super User

Try something along the lines of IF(SELECTEDVALUE(CostCenter) = 803, [do something], [do something else])

Announcements

#### Launching new user group features

Learn how to create your own user groups today!