Forum Discussion

Madhu7624's avatar
Madhu7624
Regular Visitor
1 year ago
Solved

Sorting Data in Different Order Based on Slicer Selection in Power bi

Hi Everyone,

I want to sort the data in a specific order. When I select a value in the slicer.

Below is the raw data:

Table : 

YearMonthProductAmount
20231AA100
20232BB200
20233CC100
20234DD100
20235EE300
20241AA300
20242CC500
20244DD600
20245ZZ300
20246BB300



slicer : 



order Required 

The order in which the products need to be sorted, when we select the year from slicer.





  • Try this ....

     

    Create a "driver table" ...

     

     Click on Sortkey and unpivot other columns

    Rename atttubute to Year

    Rename Value to Product

    Change the datya types

     

    Add a M:M Prodcut relationship to your data

     

     

     

    Create 2 measures

    Sorted product = 
    SELECTEDVALUE(driver[Product])

     

    Sales = 
    CALCULATE(
    SUM(yourdata[Amount]),
    yourdata[Year] = SELECTEDVALUE(driver[Year]))

     

    Add Matrix visual 

     

    Change the row text colour to white

    and change the Rows filedname to a single space

    to hide it

    user the Driver year and not yourdate year in the slicer ... otherwise the solutoion will not work

     

     

    Please click [accept solution] and the thumbs up button. Thank you

5 Replies

  • You description is a bit confussing.

     

    Please provide example input data as table (not a screen print)

    and the desire output

    with a clear decsription

    and omit any irreleant data and infomation.

     

    you seem to be saying you want the data in C, E, B, D, A sequence instead of  A, B, C, D, E

     

    That is easy ...

    just create a seperate table with 2 columns

     

    Product Sortkey
    C 1
    E 2
    B 3

     

    Then sort product by sortkey

    learn how here

    https://learn.microsoft.com/en-us/power-bi/create-reports/desktop-sort-by-column 

     

    Please click [accept solution] and [thumbs up]

    • Madhu7624's avatar
      Madhu7624
      Regular Visitor

      The issue is that, for example, Product "AA" is present in both 2023 and 2024. I want Product "AA" to be displayed in one order for 2023 and a different order for 2024.

      I have attached an image showing the required sort order for 2023 and 2024. As you can see, the sort order for product 's in 2023 is different from 2024.

      • speedramps's avatar
        speedramps
        Icon for Super User rankSuper User

        Try this ....

         

        Create a "driver table" ...

         

         Click on Sortkey and unpivot other columns

        Rename atttubute to Year

        Rename Value to Product

        Change the datya types

         

        Add a M:M Prodcut relationship to your data

         

         

         

        Create 2 measures

        Sorted product = 
        SELECTEDVALUE(driver[Product])

         

        Sales = 
        CALCULATE(
        SUM(yourdata[Amount]),
        yourdata[Year] = SELECTEDVALUE(driver[Year]))

         

        Add Matrix visual 

         

        Change the row text colour to white

        and change the Rows filedname to a single space

        to hide it

        user the Driver year and not yourdate year in the slicer ... otherwise the solutoion will not work

         

         

        Please click [accept solution] and the thumbs up button. Thank you

  • Hi Madhu7624 ,

     

    The best way to handle sorting that changes by slicer selection (like by year) is to set up a custom mapping table that defines the sort order for each year and product combination.

     

    Here’s how you can do it:

     

    1. Build a Sort Mapping Table: Create a table with three columns: Year, Product, and SortOrder.

    2. Create Relationships: Link both Year and Product from your main data table to Year and Product in your new mapping table.

    3. Add a Year Slicer: Use the Year column as a slicer so users can pick the year they want to see.

    4. Build Your Visual: Add Product (from your main table) and the SortOrder field (from your mapping table) to your matrix or table visual.

    5. Sort By SortOrder Click on the column dropdown in your visual and choose “Sort by SortOrder.”
      (You can hide the SortOrder column in the visual if you don’t want to show it.)

    Now, when you change the year with the slicer, the products will sort in the custom order you defined for that year.

    This method is clean, easy to maintain, and super flexible, just update your mapping table.