Forum Discussion
Cumulative sum with conditons and reset sum
- 3 years ago
Hello, TK12345
let text_table = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdI5CsMwEAXQu6j+gVm0trmGSRGygBsXwb5/jJSQSEjgQkjFYxD/zzQZPvFJSMjAPK+3ddte+4v3Iwp1+32el+WxmAsmIyOrFmprqyPLziGlGtvhJ0gRQ43dD99ej/u8fm0gcDPYjwY7j5jtNq8fG4Y/Jjit58ahFQduokhjnGB9jZmGWhWhyaLbnmQcQU0Y3foytgJLB/or2CNIHV23v4wdg5o4ev3JZzHKxv0Gd/srOME3YXQLzNgrUjzQYMER2oTRbTDjIPB8pMGiPdL/Jl3e", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Document = _t, Customer = _t, Amount = _t, Status = _t]), amount_type = Table.TransformColumnTypes(text_table, {{"Amount", Currency.Type}}, "de-DE"), Source = Table.TransformColumns(amount_type, {"Date", each Date.FromText(_, [Format="M-d-yyyy", Culture="de-DE"])}), fx_outcome = (tbl as table) => let amounts = List.Buffer(tbl[Amount]), sts = List.Buffer(tbl[Status]), positions = List.Buffer(List.Skip(List.Positions(sts))), column_names = Table.ColumnNames(tbl) & {"outcome"}, outcome = List.Accumulate( positions, {if sts{0} = "Buiten" then 0 else amounts{0}}, (s, c) => s & {if sts{c} = "Buiten" then 0 else amounts{c} + List.Last(s)} ), final = Table.FromColumns(Table.ToColumns(tbl) & {outcome}, column_names) in final, groups = Table.Group(Source, {"Customer"}, {{"all", each fx_outcome(Table.Sort(_, {"Date"}))}}), out = Table.Combine(groups[all]) in out
To achieve the expected outcome in Power BI, you can follow these steps:
- Import the data into Power BI and load it into the Power Query Editor.
- Sort the data by date and customer by right-clicking on each column header and selecting Sort Ascending.
- Add a custom column by clicking on the Add Column tab and selecting Custom Column. Name the column "Cumulative Sum."
- Add another custom column named "Expected Outcome" and use the following formula:In the formula bar, enter the following formula:
if [Status] = "Binnen" then List.Accumulate(
Table.Range(#"Sorted Rows", 0, [Index]),
0,
(state, current) => state + current[Amount]
)
else 0This formula creates a cumulative sum for each row where the status is "Binnen" and resets to 0 for each row where the status is "Buiten." 5. Expand the custom column by clicking on the expand icon next to its header and selecting the "Cumulative Sum" column.
- Add another custom column named "Expected Outcome" and use the following formula:In the formula bar, enter the following formula:
if [Status] = "Binnen" then [Cumulative Sum]
else 0
This formula sets the Expected Outcome column equal to the cumulative sum for each row where the status is "Binnen" and 0 for each row where the status is "Buiten." 7. Close and apply the Power Query Editor changes and create a table visualization in Power BI with the columns Date, Document, Customer, Amount, Status, and Expected Outcome.
This should result in a table with the expected outcome values calculated as desired, with the cumulative sum resetting to 0 for each row where the status is "Buiten" and calculated separately for each customer.