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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Mehef_koul
New Member

Dax calculation help

Hi @everyone,

I would like to calculate a measure to add the values of rows. For example I have a slicer [subject] containing {subject1,subject2,subject3 etc...} when I deactivate Subject 3, the value of Subject 3 is added to the value of Subject 1.

subject value
subject 11
subject 21
subject 31
subject 41
subject 51
subject 61
1 ACCEPTED SOLUTION
johnbasha33
Solution Sage
Solution Sage

@Mehef_koul 

To achieve this, you can create a measure in Power BI that dynamically adjusts the values based on the selection made in the slicer. You can use the SWITCH function along with the ISFILTERED function to determine which subject(s) are selected in the slicer, and then calculate the sum accordingly. Here's how you can do it:

```DAX
Total Value =
VAR SelectedSubjects =
VALUES ( 'YourTable'[subject] )
RETURN
SWITCH (
TRUE (),
COUNTROWS ( SelectedSubjects ) = 0, SUM ( 'YourTable'[value] ), -- If no subject selected, show total value
COUNTROWS ( SelectedSubjects ) > 1, BLANK (), -- If more than one subject selected, show blank (optional)
SELECTEDVALUE ( 'YourTable'[subject] ), -- If only one subject selected, show the value of that subject
CALCULATE (
SUM ( 'YourTable'[value] ),
ALL ( 'YourTable'[subject] ),
'YourTable'[subject] = SELECTEDVALUE ( 'YourTable'[subject] )
)
)
```

Replace `'YourTable'` with the actual name of your table, and `'subject'` and `'value'` with the names of your subject and value columns, respectively.

Here's how the measure works:

- It first checks if any subject is selected. If no subject is selected, it shows the total value.
- If more than one subject is selected, it shows blank (you can remove this line if you want to display something else in this case).
- If only one subject is selected, it shows the value of that subject.
- If a single subject is selected, it calculates the sum of values for that subject only.

This measure should give you the desired result of dynamically adjusting the values based on the selection made in the slicer. Adjust it as needed based on your specific requirements and data model.

Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!

View solution in original post

1 REPLY 1
johnbasha33
Solution Sage
Solution Sage

@Mehef_koul 

To achieve this, you can create a measure in Power BI that dynamically adjusts the values based on the selection made in the slicer. You can use the SWITCH function along with the ISFILTERED function to determine which subject(s) are selected in the slicer, and then calculate the sum accordingly. Here's how you can do it:

```DAX
Total Value =
VAR SelectedSubjects =
VALUES ( 'YourTable'[subject] )
RETURN
SWITCH (
TRUE (),
COUNTROWS ( SelectedSubjects ) = 0, SUM ( 'YourTable'[value] ), -- If no subject selected, show total value
COUNTROWS ( SelectedSubjects ) > 1, BLANK (), -- If more than one subject selected, show blank (optional)
SELECTEDVALUE ( 'YourTable'[subject] ), -- If only one subject selected, show the value of that subject
CALCULATE (
SUM ( 'YourTable'[value] ),
ALL ( 'YourTable'[subject] ),
'YourTable'[subject] = SELECTEDVALUE ( 'YourTable'[subject] )
)
)
```

Replace `'YourTable'` with the actual name of your table, and `'subject'` and `'value'` with the names of your subject and value columns, respectively.

Here's how the measure works:

- It first checks if any subject is selected. If no subject is selected, it shows the total value.
- If more than one subject is selected, it shows blank (you can remove this line if you want to display something else in this case).
- If only one subject is selected, it shows the value of that subject.
- If a single subject is selected, it calculates the sum of values for that subject only.

This measure should give you the desired result of dynamically adjusting the values based on the selection made in the slicer. Adjust it as needed based on your specific requirements and data model.

Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.