The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.
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 Region | Destination Market | Year | Origin Market | Value |
EUROPE | Bosnia And Herzegovina | 2024 | Bosnia And Herzegovina | 5 |
SSEA-CIS-MEA | Armenia | 2025 | Armenia | 12 |
SSEA-CIS-MEA | Armenia | 2024 | Armenia | 22 |
EA&A-DF | Australia | 2024 | New Zealand | 45 |
EA&A-DF | Australia | 2024 | Malaysia | 46 |
EA&A-DF | Australia | 2024 | Philippines | 47 |
EA&A-DF | Australia | 2024 | Korea | 48 |
EA&A-DF | Australia | 2024 | Kazakhstan | 50 |
EUROPE | Bulgaria | 2025 | Macedonia | 121 |
EUROPE | Austria | 2025 | Slovenia | 124 |
EUROPE | Austria | 2025 | Slovakia | 128 |
EUROPE | Austria | 2025 | Bosnia And Herzegovina | 130 |
EUROPE | Austria | 2024 | Croatia | 135 |
EUROPE | Austria | 2024 | Slovenia | 136 |
EUROPE | Austria | 2024 | Hungary | 137 |
EUROPE | Austria | 2024 | Duty Free | 146 |
EUROPE | Austria | 2024 | Cyprus | 147 |
EUROPE | Austria | 2024 | Serbia | 148 |
EUROPE | Austria | 2024 | Slovakia | 156 |
EUROPE | Cyprus | 2024 | Turkish Cyprus | 174 |
EUROPE | Austria | 2024 | Czech Republic | 177 |
EUROPE | Austria | 2024 | Germany | 179 |
EUROPE | Austria | 2024 | Macedonia | 180 |
EUROPE | Austria | 2024 | Romania | 182 |
EUROPE | Austria | 2024 | Egypt | 185 |
EUROPE | Austria | 2024 | Bulgaria | 187 |
EUROPE | Austria | 2024 | Ukraine | 188 |
EUROPE | Austria | 2024 | Turkey | 189 |
EUROPE | Austria | 2024 | Greece | 191 |
EUROPE | Austria | 2024 | Bosnia And Herzegovina | 196 |
EUROPE | Bulgaria | 2025 | Austria | 197 |
EUROPE | Austria | 2025 | Czech Republic | 199 |
EUROPE | Austria | 2025 | Spain | 200 |
EUROPE | Bulgaria | 2024 | Netherlands | 206 |
EUROPE | Austria | 2025 | Duty Free | 211 |
EUROPE | Austria | 2025 | Ukraine | 212 |
EUROPE | Austria | 2025 | Italy | 217 |
EUROPE | Austria | 2024 | Poland | 219 |
EUROPE | Cyprus | 2024 | Cyprus | 285 |
EUROPE | Croatia | 2024 | Croatia | 287 |
EUROPE | Austria | 2024 | Austria | 297 |
EUROPE | Austria | 2025 | Austria | 298 |
EUROPE | Bulgaria | 2024 | Bulgaria | 308 |
EUROPE | Bulgaria | 2025 | Bulgaria | 309 |
EUROPE | Bosnia And Herzegovina | 2025 | Bosnia And Herzegovina | 316 |
SSEA-CIS-MEA | Azerbaijan | 2024 | Azerbaijan | 322 |
EUROPE | Albania | 2024 | Albania | 323 |
EA&A-DF | Australia | 2024 | Japan | 370 |
EA&A-DF | Australia | 2024 | Duty Free | 371 |
EA&A-DF | Australia | 2024 | Greece | 378 |
EUROPE | Albania | 2024 | Italy | 379 |
EUROPE | Albania | 2024 | Montenegro | 380 |
EUROPE | Albania | 2024 | Duty Free | 381 |
EUROPE | Austria | 2024 | Turkish Cyprus | 479 |
EUROPE | Austria | 2024 | Italy | 481 |
EUROPE | Austria | 2024 | Japan | 489 |
EUROPE | Austria | 2024 | Switzerland | 490 |
EUROPE | Austria | 2024 | Korea | 491 |
EUROPE | Austria | 2024 | Portugal | 492 |
EUROPE | Austria | 2024 | Albania | 493 |
EUROPE | Bulgaria | 2024 | Duty Free | 506 |
EUROPE | Bulgaria | 2024 | Italy | 507 |
EUROPE | Bulgaria | 2024 | Germany | 512 |
EUROPE | Bulgaria | 2025 | Serbia | 542 |
EUROPE | Bulgaria | 2025 | Greece | 543 |
EUROPE | Bulgaria | 2025 | Unspecified | 544 |
EUROPE | Bulgaria | 2025 | Poland | 551 |
EUROPE | Bulgaria | 2025 | Montenegro | 552 |
EUROPE | Bulgaria | 2025 | Duty Free | 553 |
EUROPE | Bulgaria | 2025 | Cyprus | 554 |
EUROPE | Austria | 2025 | Croatia | 576 |
EUROPE | Austria | 2025 | Hungary | 577 |
EUROPE | Austria | 2025 | Poland | 578 |
EUROPE | Austria | 2025 | Romania | 579 |
EUROPE | Austria | 2025 | Moldova | 583 |
EUROPE | Austria | 2025 | Serbia | 584 |
EUROPE | Austria | 2025 | Germany | 590 |
EUROPE | Austria | 2025 | Bulgaria | 591 |
EUROPE | Austria | 2025 | Japan | 593 |
EUROPE | Austria | 2025 | Portugal | 594 |
EUROPE | Austria | 2025 | Switzerland | 600 |
EUROPE | Cyprus | 2024 | Greece | 612 |
EUROPE | Cyprus | 2024 | Ukraine | 613 |
EUROPE | Austria | 2024 | Spain | 614 |
EUROPE | Austria | 2024 | Georgia | 615 |
EUROPE | Austria | 2024 | Armenia | 616 |
EUROPE | Croatia | 2024 | Bosnia And Herzegovina | 629 |
EUROPE | Croatia | 2024 | Germany | 630 |
EUROPE | Croatia | 2024 | Bulgaria | 631 |
EUROPE | Bulgaria | 2024 | Greece | 635 |
EUROPE | Bulgaria | 2024 | Moldova | 636 |
EUROPE | Bulgaria | 2024 | Serbia | 638 |
EUROPE | Bulgaria | 2024 | Macedonia | 642 |
EUROPE | Austria | 2025 | Canary Islands | 696 |
Solved! Go to Solution.
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,
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.
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:
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.
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:
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.
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:
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
User | Count |
---|---|
65 | |
60 | |
55 | |
54 | |
31 |
User | Count |
---|---|
180 | |
88 | |
72 | |
48 | |
46 |