Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
Hi, this is my sample datasheet where i have the monthly transactions of stores 1-1026 and customers A-IF. I'm trying to make a matrix table with stores as the first row, customer as second row and the sales etc as the values.
However, im trying to make it so that only the top and bottom 20 stores (based off sales) for first row and top 5 customers for second row are visible and could change depending on the time period of a slicer.
I can't seem to wrap my head around the measures as the query seems to exceed resources due to the sheer amount of rows so would love an alternative suggestion. Thanks.
Formatting of matrix table below:
Solved! Go to Solution.
Hi @derekli1700 ,
To dynamically display only the top and bottom 20 stores based on sales in the first row of a matrix and the top 5 customers within those stores in the second row, while keeping performance manageable, you should filter the matrix using RANKX logic inside virtual tables rather than forcing the matrix to process all combinations directly.
Start by creating a calculated table to isolate just the top and bottom 20 stores. This table can be created using the following DAX expression:
TopBottomStores =
VAR SalesPerStore =
ADDCOLUMNS(
SUMMARIZE('YourTable', 'YourTable'[Store]),
"TotalSales", CALCULATE(SUM('YourTable'[Sales]))
)
VAR TopStores =
TOPN(20, SalesPerStore, [TotalSales], DESC)
VAR BottomStores =
TOPN(20, SalesPerStore, [TotalSales], ASC)
RETURN
UNION(TopStores, BottomStores)
Next, to limit the customers displayed per store to only the top 5, you can create a measure that evaluates whether a customer is among the top 5 customers for a given store. This logic relies on SELECTEDVALUE and ALLEXCEPT to rank customers within each store context:
ShowCustomer =
VAR CurrentStore = SELECTEDVALUE('YourTable'[Store])
VAR CurrentCustomer = SELECTEDVALUE('YourTable'[Customer])
VAR SalesPerCustomer =
CALCULATETABLE(
ADDCOLUMNS(
VALUES('YourTable'[Customer]),
"CustomerSales", CALCULATE(SUM('YourTable'[Sales]))
),
ALLEXCEPT('YourTable', 'YourTable'[Store])
)
VAR RankedTable =
ADDCOLUMNS(
SalesPerCustomer,
"Rank", RANKX(SalesPerCustomer, [CustomerSales], , DESC)
)
VAR CustomerRank =
CALCULATE(
MAXX(
FILTER(RankedTable, [Customer] = CurrentCustomer),
[Rank]
)
)
RETURN
IF(CustomerRank <= 5, 1, 0)
You can then use the ‘Store’ field from the TopBottomStores table as your matrix row and apply a visual-level filter for [ShowCustomer] = 1 to restrict the customers displayed. This approach prevents generating the full store-customer combination set in memory and keeps things responsive under time-based slicers.
Best regards,
Hi @derekli1700,
May I ask if you have gotten this issue resolved?
If it is solved, please mark the helpful reply or share your solution and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster.
Regards,
Vinay Pabbu
Hi @derekli1700 ,
To dynamically display only the top and bottom 20 stores based on sales in the first row of a matrix and the top 5 customers within those stores in the second row, while keeping performance manageable, you should filter the matrix using RANKX logic inside virtual tables rather than forcing the matrix to process all combinations directly.
Start by creating a calculated table to isolate just the top and bottom 20 stores. This table can be created using the following DAX expression:
TopBottomStores =
VAR SalesPerStore =
ADDCOLUMNS(
SUMMARIZE('YourTable', 'YourTable'[Store]),
"TotalSales", CALCULATE(SUM('YourTable'[Sales]))
)
VAR TopStores =
TOPN(20, SalesPerStore, [TotalSales], DESC)
VAR BottomStores =
TOPN(20, SalesPerStore, [TotalSales], ASC)
RETURN
UNION(TopStores, BottomStores)
Next, to limit the customers displayed per store to only the top 5, you can create a measure that evaluates whether a customer is among the top 5 customers for a given store. This logic relies on SELECTEDVALUE and ALLEXCEPT to rank customers within each store context:
ShowCustomer =
VAR CurrentStore = SELECTEDVALUE('YourTable'[Store])
VAR CurrentCustomer = SELECTEDVALUE('YourTable'[Customer])
VAR SalesPerCustomer =
CALCULATETABLE(
ADDCOLUMNS(
VALUES('YourTable'[Customer]),
"CustomerSales", CALCULATE(SUM('YourTable'[Sales]))
),
ALLEXCEPT('YourTable', 'YourTable'[Store])
)
VAR RankedTable =
ADDCOLUMNS(
SalesPerCustomer,
"Rank", RANKX(SalesPerCustomer, [CustomerSales], , DESC)
)
VAR CustomerRank =
CALCULATE(
MAXX(
FILTER(RankedTable, [Customer] = CurrentCustomer),
[Rank]
)
)
RETURN
IF(CustomerRank <= 5, 1, 0)
You can then use the ‘Store’ field from the TopBottomStores table as your matrix row and apply a visual-level filter for [ShowCustomer] = 1 to restrict the customers displayed. This approach prevents generating the full store-customer combination set in memory and keeps things responsive under time-based slicers.
Best regards,
Hi,
The stores seem to work well but is there a way to make the top 5 customers under each store show their respective sales? As it currently just says the same volume as the store. I'm assuming the relationships need to be changed but i'm playing around with it and nothing seems to match up.
Hi @derekli1700,
Ensure the relationships between the tables are correct so the context flows properly.
Regards,
Vinay Pabbu
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
76 | |
75 | |
54 | |
38 | |
31 |
User | Count |
---|---|
99 | |
56 | |
50 | |
42 | |
40 |