Forum Discussion
Help needed with transforming stock table in database
So im working with a database belonging to a company my employer just aquired,
The database has a table with all in and out transactions i.e. sales, purchase to stock , product conversions and internal movements between locations/warehouses.
The strange thing with the database is that when they do a physical stock count the result of the stock count is plugged in as a total number, and not the delta between all previous transactions and the result of the stock count. (which im used to)
This means that I need to, in some way, "reset" the counter everytime there is a stock count done.
What I dont get my head around is how to approach resetting:
I can get it done heavliy manually for one product on one warehouse, but when i start to involve several products and warehouses my logic breaks.
My dream-goal is to generate a fact table in power query that contains all actual in and out transactions and a delta result of the stock count, instead of the actual quantity of the stock count. This way i can summarize the fact table with a running total measure on any given date to get a balance per product and warehouse
Any help is highly appreciated!!
The example data below show how the data looks in the database.
- There are not daily data, only when transactions happen.
- Most products do not have a stock count way after they appear in the warehouse (stock count is done yearly at a minimum, i.e. if a product is introduced in february, there might be no stock count in 10 months +/- i.e. no stock count to "reset" the counter)
| RowID | Date | ProductNumber | MovementType | Quantity | Warehouse |
| 1 | 01.01.2024 | A | Purchase | 25 | 1 |
| 2 | 02.01.2024 | A | Sales | -5 | 1 |
| 3 | 03.01.2024 | A | Sales | -2 | 1 |
| 4 | 05.01.2024 | A | Sales | -2 | 1 |
| 5 | 06.01.2024 | A | Stock Count | 14 | 1 |
| 6 | 07.01.2024 | A | Purchase | 10 | 1 |
| 7 | 01.01.2024 | B | Stock Count | 105 | 2 |
| 8 | 02.01.2024 | B | Purchase | 10 | 2 |
| 9 | 03.01.2024 | B | Sales | -100 | 2 |
| 10 | 05.01.2024 | B | Internal Transfer | -10 | 2 |
| 11 | 05.01.2024 | B | Internal Transfer | 10 | 1 |
| 12 | 06.01.2024 | B | Stock Count | 3 | 2 |
| 13 | 06.01.2024 | B | Stock Count | 10 | 1 |
| 14 | 07.01.2024 | B | Purchase | 10 | 2 |
| 15 | 08.01.2024 | A | Product Conversion | -1 | 1 |
| 16 | 08.01.2024 | A1 | Product Conversion | 1 | 3 |
- If you ran a report on Product A warehouse 1 on 04.01.2024 the balance should be 18, but if you ran the same report on 07.01.2024 it should return 24
- Product B warehouse 2 on 05.01.2024 shoud return 5 and on 06.01.2024 return 3
- Anonymous2 years ago
Hi, AlphaEcho85
Thanks for the reply from amitchandak , please allow me to provide another insight:You can try the following M expression in Power Query:
let Source = YourDataSource, ChangedType = Table.TransformColumnTypes(Source,{{"Quantity", type number}}), AddedCustom = Table.AddColumn(ChangedType, "Adjusted Quantity", each if [MovementType] = "Stock Count" then [Quantity] - List.Sum(Table.SelectRows(ChangedType, (inner) => inner[ProductNumber] = [ProductNumber] and inner[Warehouse] = [Warehouse] and inner[Date] < [Date])[Quantity]) else [Quantity]), SortedRows = Table.Sort(AddedCustom,{{"Date", Order.Ascending}}), AddedIndex = Table.AddIndexColumn(SortedRows, "Index", 0, 1), AddedRunningTotal = Table.AddColumn(AddedIndex, "Running Total", each List.Sum(Table.SelectRows(AddedIndex, (inner) => inner[ProductNumber] = [ProductNumber] and inner[Warehouse] = [Warehouse] and inner[Index] <= [Index])[Adjusted Quantity])) in AddedRunningTotalHow to Get Your Question Answered Quickly
Best Regards
Yongkang Hua
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
2 Replies
- amitchandakSuper User
AlphaEcho85 , I think you need build inventory using the running total, with help from date table joined with date of your table
Inventory / OnHand =
CALCULATE(SUM(Table[Quantity]),filter(all(date),date[date] <min(date[date])), filter(Table, Table[MovementType] in {"Purchased", "Internal Conversion"}) -
CALCULATE(SUM(Table[Quantity]),filter(all(date),date[date] <min(date[date])), filter(Table, Table[MovementType] in {"Sales"})Correct Movement type as per need
Power BI Inventory On Hand: https://youtu.be/nKbJ9Cpb-Aw
- AnonymousNot applicable
Hi, AlphaEcho85
Thanks for the reply from amitchandak , please allow me to provide another insight:You can try the following M expression in Power Query:
let Source = YourDataSource, ChangedType = Table.TransformColumnTypes(Source,{{"Quantity", type number}}), AddedCustom = Table.AddColumn(ChangedType, "Adjusted Quantity", each if [MovementType] = "Stock Count" then [Quantity] - List.Sum(Table.SelectRows(ChangedType, (inner) => inner[ProductNumber] = [ProductNumber] and inner[Warehouse] = [Warehouse] and inner[Date] < [Date])[Quantity]) else [Quantity]), SortedRows = Table.Sort(AddedCustom,{{"Date", Order.Ascending}}), AddedIndex = Table.AddIndexColumn(SortedRows, "Index", 0, 1), AddedRunningTotal = Table.AddColumn(AddedIndex, "Running Total", each List.Sum(Table.SelectRows(AddedIndex, (inner) => inner[ProductNumber] = [ProductNumber] and inner[Warehouse] = [Warehouse] and inner[Index] <= [Index])[Adjusted Quantity])) in AddedRunningTotalHow to Get Your Question Answered Quickly
Best Regards
Yongkang Hua
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.