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!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
I have a DAX measure that will tell me per Client_ID, Product_ID and Size how much stock they should have to maximize sales potential. Now I need to assign boxes of products, ideally in a way that returns the least amount of not needed products, to fulfill the recommended stock. I can easily answer how much of each size I need for each client, but the "Assigning" part I have no idea on how to proceed.
As follows is a sample of the box data I have access to:
| BOX_ID | PRODUCT_ID | TOTAL_BOX_QUANTITY | SIZE | SIZE QUANTITY |
| LE9466000002935230J10714001110611_0003 | J10714001110 | 6 | 35 | 2 |
| LE9466000002935230J10714001110611_0003 | J10714001110 | 6 | 37 | 2 |
| LE9466000002935230J10714001110611_0003 | J10714001110 | 6 | 36 | 2 |
| LE9466000002935230J10714001110611_0002 | J10714001110 | 6 | 35 | 2 |
| LE9466000002935230J10714001110611_0002 | J10714001110 | 6 | 37 | 2 |
| LE9466000002935230J10714001110611_0002 | J10714001110 | 6 | 36 | 2 |
The expected result is that, to fulfill the recommended amount of 4 for size 36 product J10714001110, I am able to return a string of " LE9466000002935230J10714001110611_0003, LE9466000002935230J10714001110611_0002 " and the efficiency of that suggestion, which would be 33% (4 out of 12 products) in this case, or 100% if the client happened to need the other sizes as well, for example.
If anyone knows the correct direction to point me towards, It'd be very helpful.
Hello @Thorfirrr,
Can you please try:
1. Create a DAX measure that calculates the recommended stock quantity per Client_ID, Product_ID, and Size, as you already mentioned you have.
2. Load the box data into Power BI as a table ("BoxData")
3. Create a new table ("Assignments,") that will store the box combinations for each recommendation, with columns: Client_ID, Product_ID, Size, BoxCombination, and Efficiency.
4. Write a Power Query (M) function that will generate all possible combinations of boxes from the "BoxData" table for a given Client_ID, Product_ID, and Size.
let
GetCombinations = (boxList as list, targetQuantity as number, currentCombination as list, combinations as table) as table =>
let
currentItem = List.First(boxList),
remainingItems = List.RemoveFirstN(boxList, 1),
currentItemQuantity = currentItem[SIZE QUANTITY],
newItem = [BOX_ID = currentItem[BOX_ID], SIZE = currentItem[SIZE], SIZE QUANTITY = 1],
updatedCombination = if currentCombination = null then {newItem} else currentCombination & {newItem},
updatedQuantity = List.Sum(Table.Column(updatedCombination, "SIZE QUANTITY")),
newCombinations = if updatedQuantity = targetQuantity then Table.InsertRows(combinations, 0, {updatedCombination})
else if updatedQuantity < targetQuantity then GetCombinations(remainingItems, targetQuantity, updatedCombination, combinations)
else combinations,
newCombinations = GetCombinations(remainingItems, targetQuantity, currentCombination, combinations)
in
newCombinations,
AssignedBoxes = Table.FromRecords(GetCombinations(BoxData, TargetQuantity, null, {}))
in
AssignedBoxes5. Use DAX to call the Power Query function for each recommendation and calculate the efficiency of each box combination.
Assignments =
VAR RecommendedStock = [YourRecommendedStockMeasure]
VAR TargetSize = [Size]
VAR TargetProduct = [Product_ID]
VAR TargetClient = [Client_ID]
VAR Combinations = GetCombinationsByTarget(TargetClient, TargetProduct, TargetSize, RecommendedStock)
VAR MaxEfficiency = MAXX(Combinations, [SIZE QUANTITY] / RecommendedStock)
VAR MaxEfficiencyCombination = FILTER(Combinations, [SIZE QUANTITY] / RecommendedStock = MaxEfficiency)
RETURN
CONCATENATEX(
MaxEfficiencyCombination,
[BOX_ID],
", "
)
Should you require further assistance please do not hesitate to reach out to me.
Hello, thank you for your answer! I am in the middle of testing it out. Is "newCombinations" supposed to be defined twice on the function? That is returning an error.
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
Check out the November 2025 Power BI update to learn about new features.
| User | Count |
|---|---|
| 20 | |
| 10 | |
| 9 | |
| 4 | |
| 4 |
| User | Count |
|---|---|
| 32 | |
| 31 | |
| 18 | |
| 12 | |
| 11 |