Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now
Hi there!
I'm self taught at PBI and mostly I can get things by researching but this one has stumped me.
I have a table similar to below which shows every product we stock, the cost and profit. What I'm wanting to do is have a table that shows the top combinations of products for profit depending on how much a customer wants to spend - limited to 2 or 3. Ie a jacket costs $60 and makes $30 profit but purchasing a shirt and a skirt costs $60 and makes $40 profit.
Current data:
| Product | Cost | Profit |
| Shirt | $30 | $20 |
| Skirt | $20 | $10 |
| Jacket | $60 | $30 |
| Pants | $30 | $20 |
Desired outcome data:
The hope would be to have a report page where people can filter by max cost and get top product combinations to maximise profit.
Thank you in advance for any help 🙂
I hope this message finds you well. I've noticed that this solution remain unresolved. If any of you have managed to find a resolution to the issue, I kindly request that you share your solution for the benefit of the entire community. Alternatively, if you're still facing challenges, please do let us know as well.
Your insights and updates will greatly assist others who might be encountering the same challenge.
Hi @lauren777
From your description, it sounds like you want to solve the Knapsack problem, specifically the 0-1 Knapsack problem.
Just checking, is the below summary correct?
I'm thinking you could either
Do you want to be able to apply filters in the report to limit the allowed Products?
Will post back with some further ideas 🙂
@OwenAuger Thank you for your reply! 🙂
The summary is correct! each Product could appear twice actually. To make it easier to explain, an electronic store is probably closer to the Products. The store could sell one speaker for $200 and make $80 profit, or they could sell 2 x hmdi cables, 1 x set top box and 2 x remotes for $200 and make $150 profit. I would say only consider up to say 3-4 products as majority of customers wouldn't go higher than that.
I would like to have a filter applied on cost, stock category or stock levels at locations.
Hopefully this helps! I've seen someone do it with Python and was hoping to implement it into PowerBi.
Hi again @lauren777
Thanks for your patience! I was a little busy since your reply and you may well have looked at this yourself since then.
I have attached a PBIX with one possible approach to this.
DAX is not as powerful as general programming languages such as Python, so it is generally not possible to implement algorithms that involve looping (with variables that update in each iteration) or recursion. For example, we can't implement a dynamic programming algorithm or other standard knapsack problem algorithms.
The method I have used is to pre-compute a table containing all possible combinations of Products, subject to:
This table is called CombinationProduct, and specifies, for each Combination ID, which Products are present and how many of each:
There are different ways this table could be generated, and you can see how I did it in Power Query (with parameters MaxRepetitions = 2 and MaxProducts = 4). If you update the Product table, the rest of the queries will update accordingly.
CombinationProduct is then related to the Product and Combination dimension tables:
The model also includes a Cost Limit parameter table, with a corresponding measure Cost Limit Value (created with Modeling > New Parameter > Numeric Range).
We can then write a measure that determines the Profit of the Combination that produces maximum Profit, subject to the Cost Limit. Here is a list of the relevant measures
Basic measures:
Product Cost =
SUM ( 'Product'[Cost] )
Product Profit =
SUM ( 'Product'[Profit] )
Combination Cost =
SUMX (
CombinationProduct, CombinationProduct[Count] * [Product Cost]
)
Combination Profit =
SUMX (
CombinationProduct, CombinationProduct[Count] * [Product Profit]
)
Top Combination Profit measure:
Top Combination Profit =
VAR CostLimit = [Cost Limit Value]
VAR CombinationsFiltered =
CALCULATETABLE (
FILTER (
ADDCOLUMNS (
-- Combinations that exist for filtered Products
SUMMARIZE ( CombinationProduct, Combination[Combination ID] ),
"@Profit", [Combination Profit],
"@Cost", [Combination Cost]
),
-- Cost constraint
[@Cost] <= CostLimit
),
-- Overall CombinationProduct filter, includes Combination & Product in expanded table
-- so that, if a visual is grouped by fields from CombinationProduct/Combination/Product,
-- the values will be nonblank only for top Combination.
ALLSELECTED ( CombinationProduct )
)
VAR TopCombination =
INDEX (
1,
CombinationsFiltered,
-- Top combination by Profit, breaking ties by Combination ID
ORDERBY ( [@Profit], DESC, Combination[Combination ID], ASC )
-- omit other arguments
)
VAR Result =
CALCULATE (
[Combination Profit],
KEEPFILTERS ( TopCombination )
)
RETURN
Result
There is a similar Top Combination Cost measure too.
These measures can be placed in visuals that group by columns of Combination/CombinationProduct/Product.
Some notes:
Let me know if this is along the lines of what you were looking for. There are undoubtedly other approaches, such as pre-computing a Combination's total Profit/Cost, which I chose not to do.
Regards
Check out the November 2025 Power BI update to learn about new features.
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
| User | Count |
|---|---|
| 11 | |
| 9 | |
| 9 | |
| 5 | |
| 4 |
| User | Count |
|---|---|
| 27 | |
| 22 | |
| 20 | |
| 17 | |
| 12 |