Forum Discussion

FrankvanDorp's avatar
FrankvanDorp
Helper I
3 years ago
Solved

Sort clustered column chart based on slicer

Relative beginner in PowerBI here. I have this table, showing the last month's profits for a company in several different countries. The next column shows the change of this figure relative to a previous period. The last column specifies which previous period it is compared with, either last month, or the same month in the previous year.

CountryProfitChangePeriod
USA72404,00-12,60Last Month
UK40032,005,5Last Month
Germany43103,00-1,3Last Month
France22872,002,7Last Month
Netherlands14099,00-0,3Last Month
Italy20385,00-5,2Last Month
Belgium9398,003,3Last Month
India12674,00-0,8Last Month
China18837,00-14,8Last Month
Canada10297,005,3Last Month
Mexico4722,00-2Last Month
Australia15810,004,7Last Month
Luxembourg1427,00-9,4Last Month
Poland8838,001,9Last Month
Czech Republic4552,00-2,6Last Month
Switzerland7981,005,3Last Month
Austria6330,00-0,4Last Month
Spain26477,0013,8Last Month
Portugal10825,002,7Last Month
Denmark11699,000,7Last Month
Sweden10483,001,5Last Month
Norway5008,00-9,4Last Month
USA72404,00-4,8Same Month Last Year
UK40032,0021,5Same Month Last Year
Germany43103,004,2Same Month Last Year
France22872,0010,4Same Month Last Year
Netherlands14099,00-1,4Same Month Last Year
Italy20385,00-18,2Same Month Last Year
Belgium9398,000,5Same Month Last Year
India12674,001,6Same Month Last Year
China18837,00-28Same Month Last Year
Canada10297,001,5Same Month Last Year
Mexico4722,00-9,4Same Month Last Year
Australia15810,0039,1Same Month Last Year
Luxembourg1427,00-15,4Same Month Last Year
Poland8838,00-2Same Month Last Year
Czech Republic4552,0072,4Same Month Last Year
Switzerland7981,0013,1Same Month Last Year
Austria6330,006,7Same Month Last Year
Spain26477,0058,2Same Month Last Year
Portugal10825,0028,5Same Month Last Year
Denmark11699,00-11,9Same Month Last Year
Sweden10483,00-7,4Same Month Last Year
Norway5008,00-44,9Same Month Last Year

 

I have made a clustered column chart in PowerBI, which looks like this:

 

 

The next thing I want to do is have a slicer, where you can select either "Last Month" or "Same Month Last Year", and then sort the clustered column chart based on that value. So if you were to select "Same Month Last Year", you should get:

 

And if you select "Last Month", you should get:

 

 

So what I need is this chart to check a slicer, and then to sort its data based on the value in the column "Period", ascending.

I found one previous thread that dealt with a similar problem, here: Solved: Sorting based on a slicer values selection - Microsoft Power BI Community.

However, this solution showed how to make the chart read the slicer and select the right column, but I need an extra step of looking within that column and selecting one of the two possible categories there ("last month" or "same month previous year"), and then sorting the nations from best to worst based on their score for that particular category.

I'm currently stumped how I can do this, so any help would be greatly appreciated.

 

Here is the link to the pbix file: https://drive.google.com/file/d/1QgFedVVr7A55U-DceqJf6D4RKMMVO6gM/view?usp=sharing

  • FrankvanDorp,

     

    Try this solution.

     

    1. Create table SlicerPeriod (no relationships):

     

     

    2. Create measures:

     

    Change = SUM ( FactTable[Change] )
    Sort Order = 
    VAR vPeriodSelection =
        SELECTEDVALUE ( SlicerPeriod[Period] )
    VAR vCurrentCountry =
        SELECTEDVALUE ( FactTable[Country] )
    VAR vTableAmount =
        ADDCOLUMNS (
            ALLSELECTED ( FactTable[Country] ),
            "@Amount", CALCULATE ( [Change], FactTable[Period] = vPeriodSelection )
        )
    VAR vTableRank =
        ADDCOLUMNS ( vTableAmount, "@Rank", RANKX ( vTableAmount, [@Amount],, ASC ) )
    VAR vResult =
        MAXX ( FILTER ( vTableRank, FactTable[Country] = vCurrentCountry ), [@Rank] )
    RETURN
        vResult

     

    3. Create slicer using SlicerPeriod[Period].

     

    4. Create matrix and add the measure [Sort Order] as a Tooltip (sort Ascending):

     

     

    5. Add remaining fields to matrix. Legend uses FactTable[Period].

     

     

    6. Result:

     

    ---

     

8 Replies

  • FrankvanDorp,

     

    Try this solution.

     

    1. Create table SlicerPeriod (no relationships):

     

     

    2. Create measures:

     

    Change = SUM ( FactTable[Change] )
    Sort Order = 
    VAR vPeriodSelection =
        SELECTEDVALUE ( SlicerPeriod[Period] )
    VAR vCurrentCountry =
        SELECTEDVALUE ( FactTable[Country] )
    VAR vTableAmount =
        ADDCOLUMNS (
            ALLSELECTED ( FactTable[Country] ),
            "@Amount", CALCULATE ( [Change], FactTable[Period] = vPeriodSelection )
        )
    VAR vTableRank =
        ADDCOLUMNS ( vTableAmount, "@Rank", RANKX ( vTableAmount, [@Amount],, ASC ) )
    VAR vResult =
        MAXX ( FILTER ( vTableRank, FactTable[Country] = vCurrentCountry ), [@Rank] )
    RETURN
        vResult

     

    3. Create slicer using SlicerPeriod[Period].

     

    4. Create matrix and add the measure [Sort Order] as a Tooltip (sort Ascending):

     

     

    5. Add remaining fields to matrix. Legend uses FactTable[Period].

     

     

    6. Result:

     

    ---

     

    • DataInsights's avatar
      DataInsights
      Super User

      Correction: "Clustered column chart", not matrix. 🙂

      • FrankvanDorp's avatar
        FrankvanDorp
        Helper I

        For some reason, after adding all that I do get a sorted chart, but it doesn't show both 'Last Month' and 'Same Month Last Year', only the one I selected.

        When I deselect, I get a graph with both again, though unsorted:

        (btw the new measure Change I named 'Change2' because it won't allow me to name it Change as there is already a column with that name).

        Is there maybe some step I missed?

         

    • FrankvanDorp's avatar
      FrankvanDorp
      Helper I

      Apologies, I forgot, I have now updated the post with a link to the pbix file.