Power Query/M conditional row sums based on previous rows

jussivi

New Member
Joined
Mar 21, 2018
Messages
2
I have a data set containing dates and amounts for upcoming, cash flow causing accounts payable and accounts receivable transactions. Those are exported from accounting software and I use them for cash flow forecast. Other transactions are added manually to the forecast.

Problem with those AP-/AR-transactions is that they have arbitrary due dates on bank holidays or weekends and those amounts must be moved to the next upcoming banking day.

Is it possible to calculate so called “corrected banking day amount” -column in PQ where new amount for any bank holidays is zero and any possible original amounts are added to the next banking day amount? My current experience with M or custom queries is very limited but I am willing to learn.

Logic: IF Date = “not banking day" THEN conditional sum should be zero. IF Date = “banking day” THEN show amount where original row amount and amounts from any preceding bank holidays are summed. Positive amounts are cash inflow and negative amounts cash outflow.

Example:

DateAmountTypeBankingDay?“CorrectedBankingDayAmount”
26.3.2018100ARYES100
27.3.2018100ARYES100
28.3.2018100ARYES100
29.3.2018100ARYES100
30.3.2018100ARNO0
31.3.2018100ARNO0
1.4.2018100ARNO0
2.4.2018100ARNO0
3.4.2018100ARYES500
4.4.2018100ARYES100
5.4.2018100ARYES100
6.4.2018100ARYES100
7.4.2018100ARNO0
8.4.2018-500ARNO0
9.4.2018100ARYES-300
10.4.2018100ARYES100
11.4.2018100ARYES100
12.4.2018100ARYES100
13.4.2018100ARYES100
14.4.2018100ARNO0
15.4.2018100ARNO0

<tbody>
</tbody>

Goal is to have this table and other table containing manual transactions appended with PQ’s “append queries” feature.

I would be thankful for any help or insights!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Assuming your data is in Table1
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Amount", type number}, {"Type", type text}, {"BankingDay?", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Added Conditional Column" = Table.AddColumn(#"Added Index", "ToGroup", each if [#"BankingDay?"] = "YES" then [Index] else null),
    #"Filled Up" = Table.FillUp(#"Added Conditional Column",{"ToGroup"}),
    #"Grouped Rows" = Table.Group(#"Filled Up", {"ToGroup"}, {{"CorrectedBankingDayAmount", each List.Sum([Amount]), type number}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([ToGroup] <> null)),
    #"Merged Queries" = Table.NestedJoin(#"Filled Up",{"Index"},#"Filtered Rows",{"ToGroup"},"Almost",JoinKind.LeftOuter),
    #"Expanded {0}" = Table.ExpandTableColumn(#"Merged Queries", "Almost", {"CorrectedBankingDayAmount"}, {"CorrectedBankingDayAmount"}),
    #"Sorted Rows" = Table.Sort(#"Expanded {0}",{{"Index", Order.Ascending}}),
    #"Replaced Value" = Table.ReplaceValue(#"Sorted Rows",null,0,Replacer.ReplaceValue,{"CorrectedBankingDayAmount"}),
    #"Removed Columns" = Table.RemoveColumns(#"Replaced Value",{"Index", "ToGroup"})
in
    #"Removed Columns"

Is that what you need?
 
Last edited:
Upvote 0
Thank you for your neat solution! Seems to work perfectly. Now I just have to figure out what really happens there in the code.
 
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,821
Members
448,990
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top