Treemap, sum all except selected values

36 Views Asked by At

I've got a data table, "DF", with two columns: "Country" and "Sales". I've been trying to create a measure that would display a treemap (or a pie chart, for that matter) where, upon selecting one or more categorical values from a slicer (in this case, Germany), all but said selected values would be aggregated into one category ("Others") with same colour as follows:

Current

Desired result

Unfortunately, my DAX skills are not as sharp as I would want them to be. I figured the appropiate logic would be to first create the following VARs:

VAR SelectedCountries = VALUES('DF'[Country])
VAR Sales_SelectedCountries = 
    CALCULATE(
        SUM('DF'[Sales]),
        'DF'[Country] IN SelectedCountries
    )
VAR Sales_OtherCountries = 
    CALCULATE(
        SUM('DF'[Sales]),
        NOT('DF'[Country] IN SelectedCountries)
    ) 

Now I guess all that's left is introducing RETURN and create a custom table where I'd add Sales_SelectedCountries and Sales_OtherCountries as columns under the appropiate lable and colour, and put them in the visual; but I can't seem to figure out the logic behind the next segment of code.

1

There are 1 best solutions below

0
Amira Bedhiafi On

You can create a new table with two columns. If a country is selected in the slicer, it will appear with its actual name and sales; all unselected countries will be aggregated into one row labeled "Others" :

Selected Countries = 
VAR SelectedCountries = VALUES('DF'[Country])
RETURN
UNION(
    SELECTCOLUMNS(
        FILTER('DF', 'DF'[Country] IN SelectedCountries),
        "Country", 'DF'[Country],
        "Sales", 'DF'[Sales]
    ),
    SELECTCOLUMNS(
        FILTER('DF', NOT('DF'[Country] IN SelectedCountries)),
        "Country", "Others",
        "Sales", SUMX(FILTER('DF', NOT('DF'[Country] IN SelectedCountries)), 'DF'[Sales])
    )
)