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
FrankvanDorp
Helper I
Helper I

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:

 

graph3.PNG

 

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:

graph1.PNG

 

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

 

graph2.PNG

 

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

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@FrankvanDorp,

 

Try this solution.

 

1. Create table SlicerPeriod (no relationships):

 

DataInsights_0-1666705185663.png

 

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):

 

DataInsights_1-1666705402445.png

 

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

 

DataInsights_2-1666705475858.png

 

6. Result:

 

DataInsights_3-1666705545710.png

---

DataInsights_4-1666705583832.png

 





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

Proud to be a Super User!




View solution in original post

8 REPLIES 8
DataInsights
Super User
Super User

@FrankvanDorp,

 

Try this solution.

 

1. Create table SlicerPeriod (no relationships):

 

DataInsights_0-1666705185663.png

 

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):

 

DataInsights_1-1666705402445.png

 

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

 

DataInsights_2-1666705475858.png

 

6. Result:

 

DataInsights_3-1666705545710.png

---

DataInsights_4-1666705583832.png

 





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

Proud to be a Super User!




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





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

Proud to be a Super User!




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.

FrankvanDorp_0-1666712269927.png

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

FrankvanDorp_1-1666712363681.png

(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,

 

Note step 3: Create slicer using SlicerPeriod[Period]. It appears your slicer uses the Period field from your data table, causing it to filter the data. The table SlicerPeriod doesn't filter the visual; it's merely a way to capture the user's selection.

 

I use a measures table to avoid naming conflicts like the one you mentioned, and to organize measures so they aren't scattered throughout tables. This is just a personal preference, and won't impact results.





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

Proud to be a Super User!




I got it to work, fortunately. The slicer selected the SlicerPeriod table as it should, but apparently PowerBI had automatically created a link between the tables because they both had a column named Period. So removing that link solved the issue.

 

Thank you very much for your help, I've accepted your original post as the solution.

Glad to hear that works. That's a good point about the automatic relationships. You can disable that feature in Options.





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

Proud to be a Super User!




v-luwang-msft
Community Support
Community Support

Hi @FrankvanDorp ,

Could you pls share your pbix file ,remember to remove confidential data.

 

 

Best Regards

Lucien

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

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.