Forum Discussion
rajasekaro
11 months agoHelper III
Inventory opening closing
i have inventory data need to calculate opeing closing to create detail ledger Item Docdate QTY TYPE docid BP Monitor 19-03-2024 150 RCPT do1 BP Monitor 17-04-2024 10 ISSU do2 ...
- Anonymous10 months ago
Hi rajasekaro ,
Let me give you the ready-to-use Power Query (M) solution that will works for you.
- Sort the data by Docdate.
- Create Received and Issued columns.
- Calculate Running Closing balance.
- Calculate Opening (previous closing, first row = 0).
- Produce two outputs.
- Detail Ledger (row-wise)
- Summary Statement (Opening=0, total Received, total Issued, Closing).
use the bellow M code.
let
// Replace with your source table name
Source = YourTable,
// 1. Sort by Docdate
Sorted = Table.Sort(Source, {{"Docdate", Order.Ascending}}),
// 2. Add Received & Issued columns
AddReceived = Table.AddColumn(Sorted, "Received", each if [TYPE] = "RCPT" then [QTY] else 0, type number),
AddIssued = Table.AddColumn(AddReceived, "Issued", each if [TYPE] = "ISSU" then [QTY] else 0, type number),
// 3. Add Index column
AddIndex = Table.AddIndexColumn(AddIssued, "Index", 1, 1, Int64.Type),
// 4. Add Closing as running balance
AddClosing = Table.AddColumn(AddIndex, "Closing", each
List.Sum(List.FirstN(AddIndex[Received],[Index]))
- List.Sum(List.FirstN(AddIndex[Issued],[Index]))
, type number),
// 5. Add Opening = Previous row Closing (first row = 0)
AddOpening = Table.AddColumn(AddClosing, "Opening", each
if [Index] = 1 then 0
else AddClosing[Closing]{[Index]-2}
, type number),
// 6. Reorder columns
DetailLedger = Table.ReorderColumns(AddOpening, {"Item","Docdate","Opening","Received","Issued","Closing"}),
// 7. Create Summary Statement
OpeningVal = 0,
ReceivedVal = List.Sum(DetailLedger[Received]),
IssuedVal = List.Sum(DetailLedger[Issued]),
ClosingVal = OpeningVal + ReceivedVal - IssuedVal,
Statement = #table({"Item","Docdate","Opening","Received","Issued","Closing"}, {{"Statement","",OpeningVal,ReceivedVal,IssuedVal,ClosingVal}})
in
[DetailLedger = DetailLedger, Statement = Statement]
If you still face any issues, let us know happt to help.
Thanks,
Akhil
rajasekaro
11 months agoHelper III
i have inventory data need to calculate opeing closing to create detail ledger
Table: STOCKVALUE
| Item | Docdate | QTY | TYPE | docid |
| BP Monitor | 19-03-2024 | 150 | RCPT | do1 |
| BP Monitor | 17-04-2024 | 10 | ISSU | do2 |
| BP Monitor | 01-07-2024 | 150 | RCPT | do3 |
| BP Monitor | 03-07-2024 | 10 | ISSU | do4 |
| BP Monitor | 04-07-2024 | 300 | RCPT | do5 |
| BP Monitor | 02-12-2024 | 100 | ISSU | do6 |
| BP Monitor | 27-12-2024 | 250 | RCPT | do7 |
| BP Monitor | 21-01-2025 | 250 | RCPT | do8 |
| BP Monitor | 19-02-2025 | 150 | RCPT | do9 |
| BP Monitor | 13-03-2025 | 100 | RCPT | do10 |
| BP Monitor | 15-04-2025 | 250 | RCPT | do11 |
| BP Monitor | 30-04-2025 | 200 | ISSU | do12 |
| BP Monitor | 05-07-2025 | 300 | ISSU | do13 |
| BP Monitor | 21-08-2025 | 200 | RCPT | do14 |
EXPECTED OUTPUT
1.Detail Ledger
2.Statement
mh2587
11 months agoSuper User
You already posted same thread,
https://community.fabric.microsoft.com/t5/Desktop/Inventory-opening-closing/m-p/4814555#M1431892