Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
Hi,
I'm looking for a solution to implement the below table in m-Query in Power BI:
FileDate | Index | Opening Balance | Quantity | Consumption | AvailableStock | Closing Balance |
01-Jan-24 | 0 | 100 | 10 | -20 | 110 | 90 |
01-Feb-24 | 1 | 90 | 20 | -25 | 110 | 85 |
01-Mar-24 | 2 | 85 | 15 | -17 | 100 | 83 |
01-Apr-24 | 3 | 83 | 10 | -12 | 93 | 81 |
Working:
1. The Opening Balance for Jan 2024 is fixed = 100
2. Available Stock = Opening Balance + Quantity
3. Closing Balance = Available Stock + Consumption
4. The Closing Balance for each month becomes the Opening Balance for the next month
I would appreciate any pointers in this implementation.
Best Regads,
Kartik
Solved! Go to Solution.
let
ob_start = 100,
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
s = ((rows) => List.Generate(
() => [i = 0, r = rows{0}, ob = ob_start, av_stock = ob + r{1}, cl_bal = av_stock + r{2}],
(x) => x[i] < List.Count(rows),
(x) => [i = x[i] + 1, r = rows{i}, ob = x[cl_bal], av_stock = ob + r{1}, cl_bal = av_stock + r{2}],
(x) => x[r] & {x[i], x[ob], x[av_stock], x[cl_bal]}
))(Table.ToList(Source, each _)),
z = Table.FromList(s, each _, Table.ColumnNames(Source) & {"Index", "Opening Balance", "Available Stock", "Closing Balance"})
in
z
Hi @iktsharma ,
Thank you for choosing the Microsoft fabric Community Forum!
Thank you @AlienSx for the helpful response.
Upon reviewing the provided information, below steps might assist you in the getting the expected output.
// Sorting by Index for correct calculation
SortedTable = Table.Sort(Source, {{"Index", Order.Ascending}}),
// Opening Balance
OpeningBalanceInitial = 100,
// Convert table into a list
TableList = Table.ToRecords(SortedTable),
// List.Accumulate to calculate balances iteratively
ComputedList = List.Accumulate(
TableList,
{OpeningBalanceInitial, {}},
(state, current) =>
let
PreviousClosingBalance = state{0},
OpeningBalance = PreviousClosingBalance,
AvailableStock = OpeningBalance + current[Quantity],
ClosingBalance = AvailableStock + current[Consumption],
NewRecord = [
FileDate = current[FileDate],
Index = current[Index],
OpeningBalance = OpeningBalance,
Quantity = current[Quantity],
Consumption = current[Consumption],
AvailableStock = AvailableStock,
ClosingBalance = ClosingBalance
]
in
{ClosingBalance, state{1} & {NewRecord}}
){1}, // Extract computed records
// Convert the computed list back to table
ResultTable = Table.FromRecords(ComputedList)
in
ResultTable
Refer the attached file and the screenshot for detailed understanding.
If this post helps, please give us Kudos and consider Accept it as a solution to help the other members find it more quickly.
Regards,
Pallavi.
let
ob_start = 100,
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
s = ((rows) => List.Generate(
() => [i = 0, r = rows{0}, ob = ob_start, av_stock = ob + r{1}, cl_bal = av_stock + r{2}],
(x) => x[i] < List.Count(rows),
(x) => [i = x[i] + 1, r = rows{i}, ob = x[cl_bal], av_stock = ob + r{1}, cl_bal = av_stock + r{2}],
(x) => x[r] & {x[i], x[ob], x[av_stock], x[cl_bal]}
))(Table.ToList(Source, each _)),
z = Table.FromList(s, each _, Table.ColumnNames(Source) & {"Index", "Opening Balance", "Available Stock", "Closing Balance"})
in
z
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
17 | |
9 | |
8 | |
7 | |
7 |