Forum Discussion
Getting values from new column previous row in Excel power query
- 1 year ago
Hi gafoorgk
Thank you for engaging with the Microsoft Fabric Community.
In addition to Akash_Varuna for his response, as i have replicated the scenario and it works fine for me.
Could you please have look into the attached .pbix file.
Note: Power Query M code for reference
***********************************************************= let
Source = Table.FromRecords({
[Date=#datetime(2025, 2, 21, 14, 48, 0), Side="B", Qty=10, Amt=1574.60, RQ=10],
[Date=#datetime(2025, 2, 21, 15, 21, 0), Side="B", Qty=100, Amt=15825.00, RQ=110],
[Date=#datetime(2025, 2, 21, 15, 24, 0), Side="S", Qty=90, Amt=14131.44, RQ=20],
[Date=#datetime(2025, 2, 21, 15, 29, 0), Side="B", Qty=30, Amt=4741.50, RQ=50],
[Date=#datetime(2025, 2, 21, 15, 30, 0), Side="S", Qty=10, Amt=1582.10, RQ=40]
}),SortedTable = Table.Sort(Source, {{"Date", Order.Ascending}}),
DataList = Table.ToRecords(SortedTable),
ComputedList = List.Accumulate(
DataList,
[PBA=0, PBAMT=0, Result={}],
(state, currentRow) =>
let
PrevPBA = state[PBA],
PrevPBAMT = state[PBAMT],NBA = if currentRow[Side] = "B"
then if currentRow[Date] = DataList{0}[Date]
then currentRow[Amt] / currentRow[Qty]
else (PrevPBAMT + currentRow[Amt]) / currentRow[RQ]
else PrevPBA,
NBAMT = currentRow[RQ] * NBA,UpdatedRow = [
Date = currentRow[Date],
Side = currentRow[Side],
Qty = currentRow[Qty],
Amt = currentRow[Amt],
RQ = currentRow[RQ],
PBA = PrevPBA,
PBAMT = PrevPBAMT,
NBA = NBA,
NBAMT = NBAMT
],UpdatedResult = state[Result] & {UpdatedRow}
in
[PBA=NBA, PBAMT=NBAMT, Result=UpdatedResult]
),FinalTable = Table.FromRecords(ComputedList[Result]),
ChangeTypes = Table.TransformColumnTypes(FinalTable, {
{"Date", type datetime},
{"Side", type text},
{"Qty", Int64.Type},
{"Amt", type number},
{"RQ", Int64.Type},
{"PBA", type number},
{"PBAMT", type number},
{"NBA", type number},
{"NBAMT", type number}
})
in
ChangeTypes***********************************************************
If the above information helps you, please give us a Kudos and marked the reply Accept as a Solution.
Thanks,
Cheri Srikanth
Hi gafoorgk
Thank you for engaging with the Microsoft Fabric Community.
In addition to Akash_Varuna for his response, as i have replicated the scenario and it works fine for me.
Could you please have look into the attached .pbix file.
Note: Power Query M code for reference
***********************************************************
= let
Source = Table.FromRecords({
[Date=#datetime(2025, 2, 21, 14, 48, 0), Side="B", Qty=10, Amt=1574.60, RQ=10],
[Date=#datetime(2025, 2, 21, 15, 21, 0), Side="B", Qty=100, Amt=15825.00, RQ=110],
[Date=#datetime(2025, 2, 21, 15, 24, 0), Side="S", Qty=90, Amt=14131.44, RQ=20],
[Date=#datetime(2025, 2, 21, 15, 29, 0), Side="B", Qty=30, Amt=4741.50, RQ=50],
[Date=#datetime(2025, 2, 21, 15, 30, 0), Side="S", Qty=10, Amt=1582.10, RQ=40]
}),
SortedTable = Table.Sort(Source, {{"Date", Order.Ascending}}),
DataList = Table.ToRecords(SortedTable),
ComputedList = List.Accumulate(
DataList,
[PBA=0, PBAMT=0, Result={}],
(state, currentRow) =>
let
PrevPBA = state[PBA],
PrevPBAMT = state[PBAMT],
NBA = if currentRow[Side] = "B"
then if currentRow[Date] = DataList{0}[Date]
then currentRow[Amt] / currentRow[Qty]
else (PrevPBAMT + currentRow[Amt]) / currentRow[RQ]
else PrevPBA,
NBAMT = currentRow[RQ] * NBA,
UpdatedRow = [
Date = currentRow[Date],
Side = currentRow[Side],
Qty = currentRow[Qty],
Amt = currentRow[Amt],
RQ = currentRow[RQ],
PBA = PrevPBA,
PBAMT = PrevPBAMT,
NBA = NBA,
NBAMT = NBAMT
],
UpdatedResult = state[Result] & {UpdatedRow}
in
[PBA=NBA, PBAMT=NBAMT, Result=UpdatedResult]
),
FinalTable = Table.FromRecords(ComputedList[Result]),
ChangeTypes = Table.TransformColumnTypes(FinalTable, {
{"Date", type datetime},
{"Side", type text},
{"Qty", Int64.Type},
{"Amt", type number},
{"RQ", Int64.Type},
{"PBA", type number},
{"PBAMT", type number},
{"NBA", type number},
{"NBAMT", type number}
})
in
ChangeTypes
***********************************************************
If the above information helps you, please give us a Kudos and marked the reply Accept as a Solution.
Thanks,
Cheri Srikanth
- gafoorgk1 year agoFrequent Visitor
Hello Srikanth,
I just had a quick glance on your solution and tried. It works!. I must say I am very impressed. Thank you.
I need to dissect this solution and apply it in my bigger picture. Once it fits well, I will sure let you know with lots of Kudos.
Thanks you again,
- gafoorgk1 year agoFrequent Visitor
Your solution worked well for me Srikanth. PQ's column-by-colum flow is bypassed to be rows-by-rows flow by this List.Accummulate function. I didn't know this and couldn't find it anywhere as a solution for my problem when I searched.
Thanks again.