'How can I calculate market share for a segment by selecting one or more products in one or more segments?

I'm attempting to calculate market share (percentage of total) for a segment after a user selects a specific product within a filter.

Data looks like this:

Date Product Segment Volume
Jan 1 Bike Sports 10
Feb 1 Bike Sports 11
Mar 1 Bike Sports 14
Jan 1 Helmet Protective Equipment 9
Feb 1 Helmet Protective Equipment 2
Mar 1 Helmet Protective Equipment 3
Jan 1 Kneepads Protective Equipment 10
Feb 1 Kneepads Protective Equipment 4
Mar 1 Kneepads Protective Equipment 12

I have a measure to calculate the total volume of the segment, defined as:

Total Volume of Segment =
IF (
    HASONEVALUE ( 'Table'[Product] ),
    CALCULATE (
        [Sum of Volume],
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Segment] ),
            VALUES ( 'Table'[Segment] ) = [Segment of Product]
        )
    ),
    CALCULATE ( [Sum of Volume], ALL ( 'Table' ) )
)


The measures within are defined as:

Sum of Volume = SUM( 'Table' [Volume])

&

Segment of Product = IF(HASONEVALUE('Table'[Product]), CALCULATE(VALUES('Table'[Segment])), BLANK())

This is functioning as expected, you can see here:
volume of segment

However, I'm observing unexpected behaviour when a filter is selected:

If a single product is selected, the measure calculates correctly and sums up the total volume in all the products belonging to the same segment: enter image description here

However, if multiple products are selected (whether they're in the same segment or different segment), the total that is returned is the sum of all the products disregarding all of the applied filters: enter image description here

Any help on how to re-write the measure to validate multple selections is greatly appreciated.



Solution 1:[1]

Try changing your ALL to ALLSELECTED:

Total Volume of Segment = 
IF (
    HASONEVALUE ( 'Table'[Product] ),
    CALCULATE (
        [Sum of Volume],
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Segment] ),
            VALUES ( 'Table'[Segment] ) = [Segment of Product]
        )
    ),
    CALCULATE ( [Sum of Volume], ALLSELECTED( 'Table' )
)

)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marc Pincince