This is a Power Query (M language) script used in Power BI or Excel Power Query to process and transform data. Let's break down what this code does step-by-step, and explain its logic and purpose.
🧾 Summary of What the Code Does
This code is matching inventory quantities from one table (Таблица2) against required quantities in another table (Таблица1), and it allocates inventory until it runs out. It simulates a FIFO (First-In, First-Out) allocation or inventory deduction logic.
🧮 Input Tables
- Таблица1 ("Таблица" = Table in Russian)
- Likely contains required items, like:
art (article/articul)
need (how much is required)
- Таблица2
- Likely contains available inventory, like:
art (article/articul)
have (how much is available)
The goal is to allocate available inventory from Таблица2 to fulfill the needs in Таблица1.
🧠 Key Concepts Used
Record.TransformFields – modifies specific fields in a record.
Record.RemoveFields – removes a field from a record.
Record.Field – gets a field value from a record.
Table.Group – groups rows by a key.
List.Generate – generates a list by iterating with custom logic (like a loop).
Table.FromList – converts a list into a table.
📋 Step-by-Step Explanation
1. Load Tables
f = (x) => Excel.CurrentWorkbook(){[Name=x]}[Content]
- A helper function to get a table by name from the current workbook.
gr = Table.Group(f("Таблица2"),"art",{"tmp",(x)=>Record.FromList(x[have],x[inv])})
dict = Record.FromList(gr[tmp],gr[art])
2. Define Helper Function h
h = (i, fl, ost, k) => ...
- This is a stateful function used inside
List.Generate.
- It performs one iteration of allocation:
i: current index in Таблица1
fl: flag indicating if previous allocation was fulfilled
ost: remaining inventory for current item
k: current inventory dictionary
It:
- Picks the next required item from
Таблица1
- Tries to allocate inventory from
dict
- Updates inventory accordingly
- Returns new state for next iteration
3. Use List.Generate to Simulate a Loop
lst = List.Buffer(Table.ToList(f("Таблица1"),(x)=>x))
- Converts
Таблица1 into a list of records (for iteration)
gen = List.Generate(
() => h(0, false, lst{0}{1}, dict), // Initial state
(x) => x[i] < List.Count(lst), // Continue while not all items processed
(x) => h(x[i], x[o] or x[n]=0, x[jj]-x[n], x[tr]), // Update state
(x) => (...) // Output transformation
)
- Iterates through
Таблица1 and allocates inventory from dict
- For each item, it tries to fulfill the need using available inventory
- If inventory is insufficient, it carries over or continues
4. Convert Result to Table
to = Table.FromList(gen, (x)=>x, {"Артикул","Требуется всего","Закрыли","Счёт","ещё артикул","ещё закрыли"})
- Converts the generated list of allocations into a final table with these columns:
- Артикул – Article
- Требуется всего – Total Required
- Закрыли – Amount Allocated
- Счёт – Remaining Inventory (or something like that)
- ещё артикул – Another Article (possibly leftover)
- ещё закрыли – Another Allocation
🧾 Example Output
| Артикул | Требуется всего | Закрыли | Счёт | ещё артикул | ещё закрыли |
|---|
| A | 30 | 30 | 70 | | |
| B | 60 | 50 | 20 | A | 10 |
| C | 40 | 20 | 0 | B | 40 |
This shows how each required item was filled using available inventory.
✅ Use Case
This is useful for:
- Inventory allocation
- Order fulfillment
- FIFO inventory deduction
- Matching demand and supply
🔍 Final Notes
- The code is compact and complex, typical for Power Query scripts that simulate loops.
- It uses functional recursion and stateful iteration via
List.Generate.
- You can adapt this to your data by ensuring:
Таблица1 has art and need
Таблица2 has art, have, and possibly inv (inventory ID or name)
If you'd like, I can help you simplify this, rename variables, or adapt it to your own data structure. Let me know!