Forum Discussion
Allocate correct PO from the Stock Table based on stock availability
Good afternoon!
I have two tables in Power BI:
1. Stock Table: This table contains details of stock orders, including the Purchase Order (PO) number, the quantity ordered, and the transaction date.
2. Fitted Table: This table tracks the stock that has been fitted, including the call number (which is an identifier for the fitting event), the date it was fitted, and the quantity fitted.
Problem:
I need to link the fitted quantities in the Fittings Table with the correct PO(REFERENCE) from the Stock Table based on stock availability. Specifically:
- When stock is ordered, I want to allocate that stock to the fittings in the same order it was fitted.
- For example, if I order 3 units under a specific PO, I want to assign that PO to the first 3 fitting events (calls) that use this stock.
- Once the stock from that PO is fully allocated, I want to start associating the next fitting events with the next PO that has available stock, and so on.
The common column between the two tables is the PartNumber. Please see example of how the tables look, including just one product:
Stock Table:
| PARTNUMBER | REFERENCE | TRANDATE | QUANTITY |
| 30CXX | XH169641 | 02/01/2024 | 1 |
| 30CXX | XD17804 | 16/04/2024 | 1 |
| 30CXX | XH171119 | 12/06/2024 | 2 |
| 30CXX | XH171128 | 17/06/2024 | 7 |
| 30CXX | XD17838 | 17/06/2024 | 2 |
| 30CXX | XH171119 | 17/06/2024 | 2 |
| 30CXX | XH171144 | 18/06/2024 | 1 |
| 30CXX | XD17837 | 03/07/2024 | 6 |
| 30CXX | XD17838 | 04/07/2024 | 1 |
| 30CXX | XD17837 | 22/07/2024 | 2 |
Fitted Table
| CALLNUMBER | CALLDATE | PARTNUMBER | PARTFIT |
| 161386 | 22/03/2024 | 30CXX | 1 |
| 149785 | 12/04/2024 | 30CXX | 1 |
| 231541 | 28/05/2024 | 30CXX | 1 |
| 143524 | 28/05/2024 | 30CXX | 1 |
| 178624 | 28/05/2024 | 30CXX | 1 |
| 455369 | 28/05/2024 | 30CXX | 1 |
| 322705 | 29/05/2024 | 30CXX | 1 |
| 339088 | 29/05/2024 | 30CXX | 1 |
| 199522 | 29/05/2024 | 30CXX | 1 |
| 198464 | 12/06/2024 | 30CXX | 0 |
| 500354 | 21/06/2024 | 30CXX | 1 |
| 479535 | 21/06/2024 | 30CXX | 1 |
| 244471 | 21/06/2024 | 30CXX | 1 |
| 292740 | 21/06/2024 | 30CXX | 1 |
| 296188 | 21/06/2024 | 30CXX | 1 |
| 144533 | 21/06/2024 | 30CXX | 1 |
| 484361 | 21/06/2024 | 30CXX | 1 |
| 199625 | 28/06/2024 | 30CXX | 1 |
| 200264 | 01/07/2024 | 30CXX | 1 |
| 247219 | 03/07/2024 | 30CXX | 1 |
| 356208 | 03/07/2024 | 30CXX | 1 |
| 485738 | 05/07/2024 | 30CXX | 1 |
Expected Result
| CALLNUMBER | CALLDATE | PARTNUMBER | PARTFIT | REFERENCE |
| 161386 | 22/03/2024 | 30CXX | 1 | XH169641 |
| 149785 | 12/04/2024 | 30CXX | 1 | XD17804 |
| 231541 | 28/05/2024 | 30CXX | 1 | XH171119 |
| 143524 | 28/05/2024 | 30CXX | 1 | XH171119 |
| 178624 | 28/05/2024 | 30CXX | 1 | XH171128 |
| 455369 | 28/05/2024 | 30CXX | 1 | XH171128 |
| 322705 | 29/05/2024 | 30CXX | 1 | XH171128 |
| 339088 | 29/05/2024 | 30CXX | 1 | XH171128 |
| 199522 | 29/05/2024 | 30CXX | 1 | XH171128 |
| 198464 | 12/06/2024 | 30CXX | 0 | |
| 500354 | 21/06/2024 | 30CXX | 1 | XH171128 |
| 479535 | 21/06/2024 | 30CXX | 1 | XH171128 |
| 244471 | 21/06/2024 | 30CXX | 1 | XD17838 |
| 292740 | 21/06/2024 | 30CXX | 1 | XD17838 |
| 296188 | 21/06/2024 | 30CXX | 1 | XH171119 |
| 144533 | 21/06/2024 | 30CXX | 1 | XH171119 |
| 484361 | 21/06/2024 | 30CXX | 1 | XH171144 |
| 199625 | 28/06/2024 | 30CXX | 1 | XD17837 |
| 200264 | 01/07/2024 | 30CXX | 1 | XD17837 |
| 247219 | 03/07/2024 | 30CXX | 1 | XD17837 |
| 356208 | 03/07/2024 | 30CXX | 1 | XD17837 |
| 485738 | 05/07/2024 | 30CXX | 1 | XD17837 |
I have already tried this in DAX, creating 2 calculated Columns, Cumulative Stock and Cummulative Fitted and a column for the Allocated PO, however I don't get accurate allocation and the POs are wierdly distributed.
I am wondering maybe there are ways to achieve this in Power Query?
I would really appreciate your help.
Thanks in advance
Note: MDX2 has a higher index than MDX3.
This approach breaks down/will be unreliable when you dip below zero stock.
6 Replies
- lbendlin
Super User
You have multiple references for the same TRANDATE. Neither Power Query nor Power BI guarantees a sort order. You must provide your own index column.
What should happen when a fitting is attempted while no stock is available?
- AnonymousNot applicable
Thank you lbendlin. Yes, I have to use an index column. And in my calculation in Dax I am using that, but still don't get the correct allocation.
Good point that I have to deal with the situation there is no stock..
- lbendlin
Super User
Please provide sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.