Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Ania26
Helper III
Helper III

Matrix table with TOPN plus Others in rows and columns

Hello,

 

I would like to create matrix table with top 5 Destination Markets in Columns and top 5 Origin Markets in Rows plus remaining data as OTHERS in both rows and columns. The data would change based on the Year selection. 

Destination RegionDestination MarketYearOrigin MarketValue
EUROPEBosnia And Herzegovina2024Bosnia And Herzegovina5
SSEA-CIS-MEAArmenia2025Armenia12
SSEA-CIS-MEAArmenia2024Armenia22
EA&A-DFAustralia2024New Zealand45
EA&A-DFAustralia2024Malaysia46
EA&A-DFAustralia2024Philippines47
EA&A-DFAustralia2024Korea48
EA&A-DFAustralia2024Kazakhstan50
EUROPEBulgaria2025Macedonia121
EUROPEAustria2025Slovenia124
EUROPEAustria2025Slovakia128
EUROPEAustria2025Bosnia And Herzegovina130
EUROPEAustria2024Croatia135
EUROPEAustria2024Slovenia136
EUROPEAustria2024Hungary137
EUROPEAustria2024Duty Free146
EUROPEAustria2024Cyprus147
EUROPEAustria2024Serbia148
EUROPEAustria2024Slovakia156
EUROPECyprus2024Turkish Cyprus174
EUROPEAustria2024Czech Republic177
EUROPEAustria2024Germany179
EUROPEAustria2024Macedonia180
EUROPEAustria2024Romania182
EUROPEAustria2024Egypt185
EUROPEAustria2024Bulgaria187
EUROPEAustria2024Ukraine188
EUROPEAustria2024Turkey189
EUROPEAustria2024Greece191
EUROPEAustria2024Bosnia And Herzegovina196
EUROPEBulgaria2025Austria197
EUROPEAustria2025Czech Republic199
EUROPEAustria2025Spain200
EUROPEBulgaria2024Netherlands206
EUROPEAustria2025Duty Free211
EUROPEAustria2025Ukraine212
EUROPEAustria2025Italy217
EUROPEAustria2024Poland219
EUROPECyprus2024Cyprus285
EUROPECroatia2024Croatia287
EUROPEAustria2024Austria297
EUROPEAustria2025Austria298
EUROPEBulgaria2024Bulgaria308
EUROPEBulgaria2025Bulgaria309
EUROPEBosnia And Herzegovina2025Bosnia And Herzegovina316
SSEA-CIS-MEAAzerbaijan2024Azerbaijan322
EUROPEAlbania2024Albania323
EA&A-DFAustralia2024Japan370
EA&A-DFAustralia2024Duty Free371
EA&A-DFAustralia2024Greece378
EUROPEAlbania2024Italy379
EUROPEAlbania2024Montenegro380
EUROPEAlbania2024Duty Free381
EUROPEAustria2024Turkish Cyprus479
EUROPEAustria2024Italy481
EUROPEAustria2024Japan489
EUROPEAustria2024Switzerland490
EUROPEAustria2024Korea491
EUROPEAustria2024Portugal492
EUROPEAustria2024Albania493
EUROPEBulgaria2024Duty Free506
EUROPEBulgaria2024Italy507
EUROPEBulgaria2024Germany512
EUROPEBulgaria2025Serbia542
EUROPEBulgaria2025Greece543
EUROPEBulgaria2025Unspecified544
EUROPEBulgaria2025Poland551
EUROPEBulgaria2025Montenegro552
EUROPEBulgaria2025Duty Free553
EUROPEBulgaria2025Cyprus554
EUROPEAustria2025Croatia576
EUROPEAustria2025Hungary577
EUROPEAustria2025Poland578
EUROPEAustria2025Romania579
EUROPEAustria2025Moldova583
EUROPEAustria2025Serbia584
EUROPEAustria2025Germany590
EUROPEAustria2025Bulgaria591
EUROPEAustria2025Japan593
EUROPEAustria2025Portugal594
EUROPEAustria2025Switzerland600
EUROPECyprus2024Greece612
EUROPECyprus2024Ukraine613
EUROPEAustria2024Spain614
EUROPEAustria2024Georgia615
EUROPEAustria2024Armenia616
EUROPECroatia2024Bosnia And Herzegovina629
EUROPECroatia2024Germany630
EUROPECroatia2024Bulgaria631
EUROPEBulgaria2024Greece635
EUROPEBulgaria2024Moldova636
EUROPEBulgaria2024Serbia638
EUROPEBulgaria2024Macedonia642
EUROPEAustria2025Canary Islands696

 

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @Ania26 ,

 

To achieve this dynamic Top 5 matrix in DAX, the best approach is to create two disconnected tables—one for the rows and one for the columns—and then write a comprehensive measure to handle the logic. This keeps your model clean and your calculations powerful. Assume your main table is named Sales.

 

First, you'll need to create these two tables using the New Table option under the Modeling tab. These tables will provide the labels for your matrix axes and must not have a relationship with your main Sales table.

Create the table for your Origin Markets:

Origin Market Group = 
UNION(
    VALUES('Sales'[Origin Market]),
    ROW("Origin Market", "Others")
)

Then, create the corresponding table for your Destination Markets:

Destination Market Group = 
UNION(
    VALUES('Sales'[Destination Market]),
    ROW("Destination Market", "Others")
)

With the tables for your axes ready, you can now create the DAX measures. It's good practice to have a simple base measure for the sum, which the main measure will use.

Total Value = SUM('Sales'[Value])

Now, here is the primary measure that performs all the heavy lifting. It identifies the top markets based on the filter context (like a selected year), compares them to the selections on the matrix axes, and calculates the appropriate value for each cell, including the "Others" aggregations.

Matrix Value = 
VAR TopNValue = 5

// Determine Top 5 Origin Markets based on the total value in the current filter context
VAR TopOriginMarkets =
    TOPN(
        TopNValue,
        CALCULATETABLE(
            VALUES('Sales'[Origin Market]),
            ALL('Sales'[Origin Market])
        ),
        [Total Value],
        DESC
    )

// Determine Top 5 Destination Markets
VAR TopDestinationMarkets =
    TOPN(
        TopNValue,
        CALCULATETABLE(
            VALUES('Sales'[Destination Market]),
            ALL('Sales'[Destination Market])
        ),
        [Total Value],
        DESC
    )

// Get the market selected on the matrix axis from our disconnected tables
VAR SelectedOrigin = SELECTEDVALUE('Origin Market Group'[Origin Market])
VAR SelectedDestination = SELECTEDVALUE('Destination Market Group'[Destination Market])

// Calculate the result based on whether the selected market is in the Top 5 or is "Others"
VAR Result =
    SWITCH(
        TRUE(),
        // Case 1: A Top Origin intersecting with a Top Destination
        SelectedOrigin <> "Others" && SelectedDestination <> "Others",
            CALCULATE(
                [Total Value],
                'Sales'[Origin Market] = SelectedOrigin,
                'Sales'[Destination Market] = SelectedDestination
            ),
        // Case 2: A Top Origin intersecting with "Others" Destination
        SelectedOrigin <> "Others" && SelectedDestination = "Others",
            CALCULATE(
                [Total Value],
                'Sales'[Origin Market] = SelectedOrigin,
                NOT('Sales'[Destination Market] IN TopDestinationMarkets)
            ),
        // Case 3: "Others" Origin intersecting with a Top Destination
        SelectedOrigin = "Others" && SelectedDestination <> "Others",
            CALCULATE(
                [Total Value],
                NOT('Sales'[Origin Market] IN TopOriginMarkets),
                'Sales'[Destination Market] = SelectedDestination
            ),
        // Case 4: "Others" Origin intersecting with "Others" Destination
        SelectedOrigin = "Others" && SelectedDestination = "Others",
            CALCULATE(
                [Total Value],
                NOT('Sales'[Origin Market] IN TopOriginMarkets),
                NOT('Sales'[Destination Market] IN TopDestinationMarkets)
            )
    )
RETURN
    // Ensure that a value only appears if the selected market is actually in the Top list for that category
    IF(
        (SelectedOrigin <> "Others" && NOT(SelectedOrigin IN TopOriginMarkets)) ||
        (SelectedDestination <> "Others" && NOT(SelectedDestination IN TopDestinationMarkets)),
        BLANK(),
        Result
    )

To build the final report, add a Matrix visual to your canvas. Drag the Origin Market field from your Origin Market Group table into the Rows field well. Next, drag the Destination Market field from the Destination Market Group table into the Columns field well. Finally, place your new [Matrix Value] measure into the Values field. Add a slicer for the [Year] field from your Sales table, and you're all set! Your visual will now work exactly as requested.  

 

Best regards,

View solution in original post

8 REPLIES 8
v-pnaroju-msft
Community Support
Community Support

Hi Ania26,

We would like to follow up and see whether the details we shared have resolved your problem.
If you need any more assistance, please feel free to connect with the Microsoft Fabric community.

Thank you.

v-pnaroju-msft
Community Support
Community Support

Hi Ania26,

Thank you for your update.

Please find attached a screenshot along with the revised sample PBIX file, which may assist in resolving the matter:

vpnarojumsft_0-1753210477258.png

We hope the information provided proves helpful in addressing the issue. Should you have any further queries, please feel free to reach out to the Microsoft Fabric community.

Thank you.

v-pnaroju-msft
Community Support
Community Support

Thankyou @FBergamaschi, @DataNinja777, for your response.

Hi Ania26,

We appreciate your question on the Microsoft Fabric Community Forum.

Based on my understanding of the issue, please find attached a screenshot and a sample PBIX file that may assist in resolving the matter:

vpnarojumsft_0-1753098243992.png

We hope the information provided helps to resolve the issue.Should you have any further queries, kindly feel free to contact the Microsoft Fabric community.

Thank you.

 

Hello thank you for your help. We have here Others in the rows but what I meant is also to have Others in the columns. So if we have top 3, then top 3 in the rows and top 3 in the columns plus Others in both.

DataNinja777
Super User
Super User

Hi @Ania26 ,

 

To achieve this dynamic Top 5 matrix in DAX, the best approach is to create two disconnected tables—one for the rows and one for the columns—and then write a comprehensive measure to handle the logic. This keeps your model clean and your calculations powerful. Assume your main table is named Sales.

 

First, you'll need to create these two tables using the New Table option under the Modeling tab. These tables will provide the labels for your matrix axes and must not have a relationship with your main Sales table.

Create the table for your Origin Markets:

Origin Market Group = 
UNION(
    VALUES('Sales'[Origin Market]),
    ROW("Origin Market", "Others")
)

Then, create the corresponding table for your Destination Markets:

Destination Market Group = 
UNION(
    VALUES('Sales'[Destination Market]),
    ROW("Destination Market", "Others")
)

With the tables for your axes ready, you can now create the DAX measures. It's good practice to have a simple base measure for the sum, which the main measure will use.

Total Value = SUM('Sales'[Value])

Now, here is the primary measure that performs all the heavy lifting. It identifies the top markets based on the filter context (like a selected year), compares them to the selections on the matrix axes, and calculates the appropriate value for each cell, including the "Others" aggregations.

Matrix Value = 
VAR TopNValue = 5

// Determine Top 5 Origin Markets based on the total value in the current filter context
VAR TopOriginMarkets =
    TOPN(
        TopNValue,
        CALCULATETABLE(
            VALUES('Sales'[Origin Market]),
            ALL('Sales'[Origin Market])
        ),
        [Total Value],
        DESC
    )

// Determine Top 5 Destination Markets
VAR TopDestinationMarkets =
    TOPN(
        TopNValue,
        CALCULATETABLE(
            VALUES('Sales'[Destination Market]),
            ALL('Sales'[Destination Market])
        ),
        [Total Value],
        DESC
    )

// Get the market selected on the matrix axis from our disconnected tables
VAR SelectedOrigin = SELECTEDVALUE('Origin Market Group'[Origin Market])
VAR SelectedDestination = SELECTEDVALUE('Destination Market Group'[Destination Market])

// Calculate the result based on whether the selected market is in the Top 5 or is "Others"
VAR Result =
    SWITCH(
        TRUE(),
        // Case 1: A Top Origin intersecting with a Top Destination
        SelectedOrigin <> "Others" && SelectedDestination <> "Others",
            CALCULATE(
                [Total Value],
                'Sales'[Origin Market] = SelectedOrigin,
                'Sales'[Destination Market] = SelectedDestination
            ),
        // Case 2: A Top Origin intersecting with "Others" Destination
        SelectedOrigin <> "Others" && SelectedDestination = "Others",
            CALCULATE(
                [Total Value],
                'Sales'[Origin Market] = SelectedOrigin,
                NOT('Sales'[Destination Market] IN TopDestinationMarkets)
            ),
        // Case 3: "Others" Origin intersecting with a Top Destination
        SelectedOrigin = "Others" && SelectedDestination <> "Others",
            CALCULATE(
                [Total Value],
                NOT('Sales'[Origin Market] IN TopOriginMarkets),
                'Sales'[Destination Market] = SelectedDestination
            ),
        // Case 4: "Others" Origin intersecting with "Others" Destination
        SelectedOrigin = "Others" && SelectedDestination = "Others",
            CALCULATE(
                [Total Value],
                NOT('Sales'[Origin Market] IN TopOriginMarkets),
                NOT('Sales'[Destination Market] IN TopDestinationMarkets)
            )
    )
RETURN
    // Ensure that a value only appears if the selected market is actually in the Top list for that category
    IF(
        (SelectedOrigin <> "Others" && NOT(SelectedOrigin IN TopOriginMarkets)) ||
        (SelectedDestination <> "Others" && NOT(SelectedDestination IN TopDestinationMarkets)),
        BLANK(),
        Result
    )

To build the final report, add a Matrix visual to your canvas. Drag the Origin Market field from your Origin Market Group table into the Rows field well. Next, drag the Destination Market field from the Destination Market Group table into the Columns field well. Finally, place your new [Matrix Value] measure into the Values field. Add a slicer for the [Year] field from your Sales table, and you're all set! Your visual will now work exactly as requested.  

 

Best regards,

Hello, could you please have a look at your DAX measure and try to add Totals to rows and columns?

 

I am impressed. Everything works except to Totals , they do not show:

Ania26_0-1752844301514.png

 

FBergamaschi
Solution Sage
Solution Sage

Here you find a complete solution

 

https://www.sqlbi.com/articles/showing-the-top-5-products-and-others-row/

 

If this helped, please consider giving kudos and mark as a solution

@me in replies or I'll lose your thread

consider voting this Power BI idea

Francesco Bergamaschi

MBA, M.Eng, M.Econ, Professor of BI

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.