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
joanna-g2
Frequent Visitor

Switch measures and allow multiselection

Hi Community,


I have a slicer based on disconnected table that includes values A, B, C, D. 
I have a table with transactional data that stores values A, B, C and D in separate columns. 

Requirement is to create an new measure, let's call it F, that is going to return A, B, C, D OR sum of any combination of those columns. 


I have created F as

 

F =
(VAR UserSelection =
SELECTEDVALUE(DisconnectedTable[DisconnectedTable],"G")
RETURN
SWITCH(
TRUE(),
UserSelection="A", SUM(A),
UserSelection="B", SUM(B),
UserSelection="C", SUM(C),
UserSelection="D", SUM(D),
UserSelection="G", [G],
[G]))
 
where
G = SUM(A) +SUM(B) + SUM(C) +SUM(D)

The problem I'm facing is that if user selects multiple answers in the slicer, selectedvalue won't be evalutated propery and G will be returned. In case multiple answers are selected, I would like the measure to sum only the selected columns. 
 
Example 
A = 2
B = 0
C = 4
D = 8

If I select A, I'm getting 2 which is correct.
If I select A, B and C instead of getting 6, I'm getting 14. 

Is there any other DAX formula that could be used to replace selectedvalue and allow multiselection?
1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

The best approach would probably be to unpivot the columns A, B, C, and D so that you avoid writing this sort of measure entirely.

 

If they have to be separate columns, try this:

F =
VAR SelectedValues = VALUES ( DisconnectedTable[DisconnectedTable] )
RETURN
    IF ( "A" IN SelectedValues, SUM ( [A] ) )
  + IF ( "B" IN SelectedValues, SUM ( [B] ) )
  + IF ( "C" IN SelectedValues, SUM ( [C] ) )
  + IF ( "D" IN SelectedValues, SUM ( [D] ) )

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

The best approach would probably be to unpivot the columns A, B, C, and D so that you avoid writing this sort of measure entirely.

 

If they have to be separate columns, try this:

F =
VAR SelectedValues = VALUES ( DisconnectedTable[DisconnectedTable] )
RETURN
    IF ( "A" IN SelectedValues, SUM ( [A] ) )
  + IF ( "B" IN SelectedValues, SUM ( [B] ) )
  + IF ( "C" IN SelectedValues, SUM ( [C] ) )
  + IF ( "D" IN SelectedValues, SUM ( [D] ) )

Thanks @AlexisOlson ! Formula you provided works great! 

Greg_Deckler
Super User
Super User

@joanna-g2 Probably want something like:

F =
  VAR UserSelections = DisconnectedTable[DisconnectedTable]
  VAR __SumA = IF("A" IN UserSelections),SUM(A),BLANK())
  VAR __SumB = IF("B" IN UserSelections),SUM(B) + __SumA,__SumA)
  VAR __SumC = IF("C" IN UserSelections),SUM(C) + __SumB,__SumB)
  VAR __SumD = IF("D" IN UserSelections),SUM(D) + __SumC,__SumC)
RETURN
  __SumD

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hey @Greg_Deckler !

Thanks for your input! I think it is a very good approach. 


There is one challange with the sollution you have suggested. 
VAR UserSelections requires a single value for column in the DisconnectedTable. Do you have any suggestions how to work it around? I'm not able to use the Max/Min aggregation as it will pick up only one value from the slicer. 

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.