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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Thorfirrr
New Member

I need to "fill" a suggested sales quantity with boxes that contain random amounts of the product.

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_IDPRODUCT_IDTOTAL_BOX_QUANTITYSIZESIZE QUANTITY
LE9466000002935230J10714001110611_0003J107140011106352
LE9466000002935230J10714001110611_0003J107140011106372
LE9466000002935230J10714001110611_0003J107140011106362
LE9466000002935230J10714001110611_0002J107140011106352
LE9466000002935230J10714001110611_0002J107140011106372
LE9466000002935230J10714001110611_0002J107140011106362

 


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.


2 REPLIES 2
Sahir_Maharaj
Super User
Super User

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
    AssignedBoxes

5. 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.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ About: https://sahirmaharaj.com/about.html
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

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. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.