Forum Discussion

FZOU's avatar
FZOU
Helper IV
8 months ago
Solved

matrix rows order

Hello Community, In a Power BI Matrix, the region (France, EMEA, Japan, etc.) is always shown above the list of stores, because the visual enforces hierarchical ordering like shown below :  ...
  • Ahmed-Elfeel's avatar
    8 months ago

    Hi FZOU,

    Power BI matrix visual is designed for parent child hierarchies so it repeats the parent (Region) for each child (Store) when you swap them...So here is a couple of solutions Should work try it:


    First Approach:

    In this approach you will create a custom display column that controls the exact order of stores and regions

    • Create a Sorting Index Column:
    SortIndex = 
    VAR CurrentStore = [Store]
    VAR CurrentRegion = [Region]
    VAR StoreRank = RANKX(
        FILTER(ALL('Stores'), [Region] = CurrentRegion),
        [Store],
        ,
        ASC,
        DENSE
    )
    RETURN
    StoreRank * 1000  -- Stores get values like 1000, 2000, 3000
    • Then Create Display Column:
    StoreRegionDisplay = 
    VAR CurrentStore = [Store]
    VAR CurrentRegion = [Region]
    VAR IsRegionRow = [Store] = [Region]  -- Assuming you have region rows in table
    RETURN
    IF(
        IsRegionRow,
        UPPER(CurrentRegion),  -- Region in uppercase
        CurrentStore           -- Store as normal
    )
    • Also Create Region Sort Column:

    RegionSort = 
    VAR CurrentRegion = [Region]
    VAR RegionNumber = SWITCH(
        CurrentRegion,
        "FRANCE", 1,
        "EMEA", 2,
        "CHINA", 3,
        "ARANI", 4,
        99
    )
    RETURN RegionNumber
    • Finally Create Final Sorting Column:
    FinalSort = 
    [RegionSort] * 10000 + [SortIndex]

    Note:

    • Sort StoreRegionDisplay by FinalSort

    • Use StoreRegionDisplay in matrix rows


    Second Approach:

    Here we will use measures to dynamically display stores and regions

    • Just Create a Store List Measure:
    StoreList = 
    VAR StoresInContext = VALUES('Stores'[Store])
    VAR CurrentRegion = SELECTEDVALUE('Stores'[Region])
    VAR StoreCount = COUNTROWS(StoresInContext)
    RETURN
    IF(
        StoreCount = 1,
        SELECTEDVALUE('Stores'[Store]),
        CurrentRegion & " - " & FORMAT(StoreCount, "0") & " stores"
    )
    • Then Use It in Matrix

      • Rows: Store field

      • Add this measure to get smart display

    Bonus Approach

    We will Just create Unpivot table then use it in your Custome Visual

    • Create unpivoted table:

    DisplayTable = 
    UNION(
        SELECTCOLUMNS(
            FILTER('Stores', [Store] <> [Region]),
            "DisplayType", "Store",
            "DisplayValue", [Store],
            "Region", [Region],
            "SortOrder", 1
        ),
        SELECTCOLUMNS(
            'Stores',
            "DisplayType", "Region",
            "DisplayValue", [Region],
            "Region", [Region],
            "SortOrder", 2
        )
    )
    • Then use it in custom visual or table
    if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.