Forum Discussion

maryester's avatar
maryester
New Member
1 year ago
Solved

Unable to Sort Stacked Bar Chart by Rank Measure Within X-Axis Hierarchy

Hello everyone, I'm encountering an issue with sorting a stacked bar chart in Power BI Desktop and I'm hoping someone can help me understand if there's a solution or if I'm running into a limitation...
  • DataNinja777's avatar
    1 year ago

    Hi maryester ,

     

    You're experiencing a common limitation in Power BI where it's not possible to sort bars within each subgroup on a stacked or combo column chart when using a hierarchy like "Year Month GI Date" > "Description" on the X-axis. Power BI only allows sorting by one level of the hierarchy at a time, so it either sorts by the top-level grouping (Year-Month) or the measure, but not within each group.

    One workaround is to flatten the hierarchy by combining both fields into a single column, so each combination of Year-Month and Description becomes a unique axis value. You can create a calculated column like this:

    CombinedAxis = FORMAT([Year Month GI Date], "YYYY-MM") & " - " & [Description]
    

    This removes the hierarchy and treats each value independently, allowing you to sort by the measure globally. However, it won't show grouped bars per month—it becomes a flat list.

    If your goal is to preserve monthly grouping and still sort Descriptions within each month, a trickier workaround is to calculate a rank per month and append that rank to create a sorted axis. First, create a ranking measure:

    DescriptionRank =
    RANKX(
        FILTER(
            ALLSELECTED('YourTable'),
            'YourTable'[Year Month GI Date] = EARLIER('YourTable'[Year Month GI Date])
        ),
        [Average of GI Date - Created on],
        ,
        DESC
    )
    

    Then, create a sort key that combines the date, rank, and description:

    SortKey = FORMAT([Year Month GI Date], "YYYY-MM") & "-" & FORMAT([DescriptionRank], "00") & "-" & [Description]
    

    SortKey = FORMAT([Year Month GI Date], "YYYY-MM") & "-" & FORMAT([DescriptionRank], "00") & "-" & [Description]
    You can use this SortKey as your X-axis instead of the hierarchy and sort by this column. It visually preserves the grouping and sorts descriptions within each month, but you lose the drill-down functionality of the original hierarchy. For full sorting capabilities, the matrix visual remains the only one that supports sorting within groups properly.

     

    Best regards,