Forum Discussion
Anonymous
3 years agoNot applicable
My queries are running so slowly!
Hi Guys Firstly, huge thanks with all the help you've provided so far. Piecing bits together from other answers has enabled me to get to this point, but now I'm stuck and I'm at my wits end. Be...
ImkeF
Community Champion
3 years agoHi Anonymous ,
you might have special characters in your column name?
Please find a file attached where the running total function is working with that column name. Hopefully this will help you detect the error in your file.
- Anonymous3 years agoNot applicable
Thank you so much. I'm still struggling to make it work. 7 hours so far today! Will keep you posted.
- Anonymous3 years agoNot applicable
I'm losing my pateince this this!!!! Where am I going wrong?!! Everything I'm trying seems to take ages to run. I also need to replace all the nulls in the Running Total column with zeros. I tried pulling in Replace Values, but that took ages to run.
fxSecRunningTotal
(Table as table, SortColumn as text, AmountColumn as text) => let /* Debug parameters Table = #"4c - Sec Data Table", SortColumn = "CaseNo", AmountColumn = "Sec Avg Activity SEI", */ // Sort table and buffer it Sorted = Table.Buffer(Table.AddIndexColumn(Table.Sort(Table,{{SortColumn, Order.Ascending}}), "Index",1,1)), // Select the Columns SelectColumns = Table.SelectColumns(Sorted, {SortColumn, AmountColumn, "Index"}), // Extract Amount column and buffer it ExtractAmountColumn = List.Buffer(Table.Column(SelectColumns, AmountColumn)), // Calculate a list with all running Totals RunningTotal = List.Skip(List.Generate( ()=> [ListItem=0, Counter=0] ,each [Counter] <= List.Count(ExtractAmountColumn) ,each [ ListItem = ExtractAmountColumn{[Counter]}+[ListItem], Counter = [Counter]+1 ] ),1), ConvertedTable = Table.FromList(RunningTotal, Splitter.SplitByNothing(), null, null, ExtraValues.Error), ExpandedColumn = Table.ExpandRecordColumn( ConvertedTable, "Column1", {"ListItem", "Counter"}, {"ListItem", "Counter"}), MergedQueries = Table.NestedJoin(Sorted,{"Index"}, ExpandedColumn,{"Counter"},"Expanded Column1",JoinKind.LeftOuter), Expand = Table.ExpandTableColumn( MergedQueries, "Expanded Column1", {"ListItem"}, {"RunningTotal"}), #"Removed Columns" = Table.RemoveColumns(Expand,{"Index"}), #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"RunningTotal", type number}}) in #"Changed Type"1 - Data Table
let Source = Table.NestedJoin(#"0d - Staging", {"Activity"}, #"0e - AVG", {"Activity"}, "2 - AVG", JoinKind.LeftOuter), #"Expanded 2 - AVG" = Table.ExpandTableColumn(Source, "2 - AVG", {"Ops", "MT", "Sec"}, {"Ops", "MT", "Sec"}), #"Renamed Columns" = Table.RenameColumns(#"Expanded 2 - AVG",{{"Ops", "Ops Avg Activity SEI"}, {"MT", "MT Avg Activity SEI"}, {"Sec", "Sec Avg Activity SEI"}}), //Sort rows by case number and activity order, then remove top two rows as blank #"Sorted Rows" = Table.Sort(#"Renamed Columns",{{"CaseNo", Order.Ascending}, {"Activity Order", Order.Ascending}}), #"Inserted CaseNoActOrderID" = Table.AddColumn(#"Sorted Rows", "CaseNoActOrderID", each Text.Combine({[CaseNo], Text.From([Activity Order], "en-GB")}, ":"), type text), #"Reordered Columns" = Table.ReorderColumns(#"Inserted CaseNoActOrderID",{"AC", "CaseNo", "HighestCaseType", "FCCCreatedDate", "Activity", "Activity Order", "CaseNoActOrderID", "Ops SEI Value", "MT SEI Value", "Sec SEI Value", "Ops Avg Activity SEI", "MT Avg Activity SEI", "Sec Avg Activity SEI"}), //Add in total of the average SEI values for the case, for that division #"Added Ops Tot Avg SEI" = Table.AddColumn(#"Reordered Columns", "Ops Total Avg SEI", each let _item = [CaseNo] in List.Sum(Table.SelectRows(#"Reordered Columns", each [CaseNo] = _item)[#"Ops Avg Activity SEI"])), #"Added MT Tot Avg SEI" = Table.AddColumn(#"Added Ops Tot Avg SEI", "MT Total Avg SEI", each let _item = [CaseNo] in List.Sum(Table.SelectRows(#"Reordered Columns", each [CaseNo] = _item)[#"MT Avg Activity SEI"])), #"Added Sec Tot Avg SEI" = Table.AddColumn(#"Added MT Tot Avg SEI", "Sec Total Avg SEI", each let _item = [CaseNo] in List.Sum(Table.SelectRows(#"Reordered Columns", each [CaseNo] = _item)[#"Sec Avg Activity SEI"])) in #"Added Sec Tot Avg SEI"2 - Max Activity Table
let Source = #"1 - Data Table", #"Buffer Table" = Table.Buffer(Source), #"Grouped Rows" = Table.Group(#"Buffer Table", {"CaseNo"}, {{"MaxActivity", each List.Max([Activity Order]), type nullable number}}) in #"Grouped Rows"3c - Sec OverUnder Index
let Source = #"1 - Data Table", #"Buffered Table" = Table.Buffer(Source), // Calculate Running Total #"Grouped Rows" = Table.Group(#"Buffered Table", {"CaseNo"}, {{"All", each _, type table [Column1=nullable text, Sec Avg Activity SEI=nullable number, Date=nullable date]}}), #"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "fxSecRunningTotal", each fxSecRunningTotal([All], "CaseNo", "Sec Avg Activity SEI")), #"Expanded fxSecRunningTotal1" = Table.ExpandTableColumn(#"Invoked Custom Function", "fxSecRunningTotal", {"AC", "CaseNo", "HighestCaseType", "FCCCreatedDate", "Activity", "Activity Order", "CaseNoActOrderID", "Sec SEI Value", "Sec Avg Activity SEI", "Sec Total Avg SEI", "Sec Avg Running Total"}, {"AC", "CaseNo.1", "HighestCaseType", "FCCCreatedDate", "Activity", "Activity Order", "CaseNoActOrderID", "Sec SEI Value", "Sec Avg Activity SEI", "Sec Total Avg SEI", "Sec Avg Running Total"}), // Replace Nulls with zeros #"Replaced Value" = Table.ReplaceValue(#"Expanded fxSecRunningTotal1",null,0,Replacer.ReplaceValue,{"Sec Avg Running Total"}), // Remove 'All' Column as not required #"Remove All Column" = Table.RemoveColumns(#"Replaced Value",{"All"}), // Change data types to numbers #"Changed Type" = Table.TransformColumnTypes(#"Remove All Column",{{"Sec SEI Value", type number}, {"Sec Avg Activity SEI", type number}, {"Sec Total Avg SEI", type number}, {"Sec Avg Running Total", type number}}), // Calculate over/under SEI #"Add OverUnder" = Table.AddColumn(#"Changed Type", "Sec OverUnder", each if [Sec Avg Running Total] > [Sec SEI Value] then "Over" else if [Sec Avg Running Total] < [Sec SEI Value] then "Under" else "Equal"), // Group OverUnder by Case #"Grouped Rows1" = Table.Group(#"Add OverUnder", {"CaseNo.1", "Sec OverUnder"}, {{"Count", each _, type table [CaseNo=nullable text, AC=text, CaseNo.1=text, HighestCaseType=text, FCCCreatedDate=datetime, Activity=text, Activity Order=number, CaseNoActOrderID=text, Sec SEI Value=nullable number, Sec Avg Activity SEI=nullable number, Sec Total Avg SEI=nullable number, Sec Avg Running Total=nullable number, Sec OverUnder=any]}}), // Add index value to OverUnder with Case #"Added Custom1" = Table.AddColumn(#"Grouped Rows1", "Custom", each Table.AddIndexColumn([Count], "Index1", 1)), #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom1", "Custom", {"CaseNo", "CaseNoActOrderID", "Index1"}, {"Custom.CaseNo", "Custom.CaseNoActOrderID", "Custom.Index1"}), // Remove unrequired columns #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Sec OverUnder", "Count", "Custom.CaseNo"}) in #"Removed Columns"4c - Sec Data Table
let Source = #"1 - Data Table", #"Buffered Table" = Table.Buffer(Source), // Running Total #"Grouped Rows" = Table.Group(#"Buffered Table", {"CaseNo"}, {{"All", each _, type table [Column1=nullable text, Sec Avg Activity SEI=nullable number, Date=nullable date]}}), #"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "fxSecRunningTotal", each fxSecRunningTotal([All], "CaseNo", "Sec Avg Activity SEI")), #"Expanded fxSecRunningTotal1" = Table.ExpandTableColumn(#"Invoked Custom Function", "fxSecRunningTotal", {"AC", "CaseNo", "HighestCaseType", "FCCCreatedDate", "Activity", "Activity Order", "CaseNoActOrderID", "Sec SEI Value", "Sec Avg Activity SEI", "Sec Total Avg SEI", "Sec Avg Running Total"}, {"AC", "CaseNo.1", "HighestCaseType", "FCCCreatedDate", "Activity", "Activity Order", "CaseNoActOrderID", "Sec SEI Value", "Sec Avg Activity SEI", "Sec Total Avg SEI", "Sec Avg Running Total"}), #"Remove All Column" = Table.RemoveColumns(#"Expanded fxSecRunningTotal1",{"All"}), // Calculate over/under SEI #"Added Over/Under" = Table.AddColumn(#"Remove All Column", "Sec OverUnder", each if [Sec Avg Running Total] > [Sec SEI Value] then "Over" else if [Sec Avg Running Total] < [Sec SEI Value] then "Under" else "Equal"), #"Changed Type" = Table.TransformColumnTypes(#"Added Over/Under",{{"Sec Total Avg SEI", type number}, {"Sec Avg Running Total", type number}}), // Calculate remaining balance #"Calculate Remaining Balance" = Table.AddColumn(#"Changed Type", "Sec SEI Balance", each [Sec SEI Value] - [Sec Avg Running Total], type number), // Calculate Final Activity and display in new column #"Max Activity Find" = Table.NestedJoin(#"Calculate Remaining Balance", {"CaseNo"}, #"2 - Max Activity Table", {"CaseNo"}, "Max Activity Table", JoinKind.LeftOuter), #"Max Activity Display" = Table.ExpandTableColumn(#"Max Activity Find", "Max Activity Table", {"MaxActivity"}, {"MaxActivity"}), // Calculate updated Running Total #"Grouped Rows2" = Table.Group(#"Max Activity Display", {"CaseNo"}, {{"All", each _, type table [Column1=nullable text, Sec Avg Activity SEI=nullable number, Date=nullable date]}}), #"Invoked Custom Function2" = Table.AddColumn(#"Grouped Rows2", "fxSecRunningTotal", each fxSecRunningTotal([All], "CaseNo", "Sec Avg Activity SEI")), #"Expanded fxSecRunningTotal2" = Table.ExpandTableColumn(#"Invoked Custom Function2", "fxSecRunningTotal", {"AC", "CaseNo", "HighestCaseType", "FCCCreatedDate", "Activity", "Activity Order", "CaseNoActOrderID", "Sec SEI Value", "Sec Avg Activity SEI", "Sec Total Avg SEI", "Sec Avg Running Total2"}, {"AC", "CaseNo.1", "HighestCaseType", "FCCCreatedDate", "Activity", "Activity Order", "CaseNoActOrderID", "Sec SEI Value", "Sec Avg Activity SEI", "Sec Total Avg SEI", "Sec Avg Running Total2"}), // Remove 'All' column as not required #"Remove All Column2" = Table.RemoveColumns(#"Expanded fxSecRunningTotal2",{"All"}), // Calculates the additional value to force final number to balance back to total SEI #"Calculate additional value for max activity" = Table.AddColumn(#"Remove All Column2", "Sec Over SEI Value", each if [Sec OverUnder] = "Over" then ([Sec Avg Running Total2] + [Sec SEI Balance]) - [Sec Avg Activity SEI] else 0), // Indexes activities within a case and whether they are over or under the SEI for the case. This is to establish additional apportionment values #"Added Index to Case and Over/Under" = Table.NestedJoin(#"Calculate additional value for max activity", {"CaseNoActOrderID"}, #"3c - SecOverUnder Index", {"Custom.CaseNoActOrderID"}, "3c - SecOverUnder Index", JoinKind.LeftOuter), #"Expanded 3c - SecOverUnder Index" = Table.ExpandTableColumn(#"Added Index to Case and Over/Under", "3c - SecOverUnder Index", {"Custom.Index1"}, {"Sec OverUnder Index"}), // SEI value if running total is greater than the total SEI for that case #"Handling of SEI when running total is greater than total value" = Table.AddColumn(#"Expanded 3c - SecOverUnder Index", "Custom", each if [Sec OverUnder] = "Over" and [Sec OverUnder Index] = 1 then [Sec Avg Activity SEI] + [Sec SEI Balance] else 0), #"Renamed Columns1" = Table.RenameColumns(#"Handling of SEI when running total is greater than total value",{{"Custom", "Sec Over SEI New Value"}}), // SEI value if running total is under and it's the final activity #"Handling of final activity if running total is under total value" = Table.AddColumn(#"Renamed Columns1", "Custom", each if [Sec OverUnder] = "Under" and [Activity Order] = [MaxActivity] then [Sec Avg Activity SEI] + [Sec SEI Balance] else 0), #"Renamed Columns2" = Table.RenameColumns(#"Handling of final activity if running total is under total value",{{"Custom", "Sec Under SEI New Value"}}), // Final Sec SEI Value #"Add Sec Final SEI" = Table.AddColumn(#"Renamed Columns2", "Sec Final SEI Value", each if [Sec OverUnder] = "Under" and [Activity Order] <> [MaxActivity] then [Sec Avg Activity SEI] else if [Sec OverUnder] = "Under" and [Activity Order] = [MaxActivity] then [Sec Under SEI New Value] else if [Sec OverUnder] = "Over" and [Sec OverUnder Index] = 1 then [Sec Over SEI New Value] else if [Sec OverUnder] = "Over" and [Sec OverUnder Index] <> 1 then [Sec Over SEI New Value] else 0) in #"Add Sec Final SEI"