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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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
8031
1102
1203
8035

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
V-lianl-msft
Community Support
Community Support

Please try this:

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] ) )
    )

Vlianlmsft_0-1624341586024.png

 

View solution in original post

2 REPLIES 2
V-lianl-msft
Community Support
Community Support

Please try this:

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] ) )
    )

Vlianlmsft_0-1624341586024.png

 

littlemojopuppy
Community Champion
Community Champion

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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