Forum Discussion
Power Query SUMIFS with Multiple Conditions
- 1 year ago
- You will need to list only the columns that are needed to create a unique grouping, so probably NO.
- For table type argument, you should list all of the columns that are in the "Grouped Table"
- In that case, I would use a different approach.
- I don't know how you will access [Original Balance].
- In the code below, I arbitrarily set it to 2x the value of the first [Ending Balance] so the results would be the same.
- You will need to modify depending on how you have your "Period 1 Starting Balance"
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{ {"UID", type text}, {"Cost Code", type text}, {"Period", Int64.Type}, {"Ending Balance", Int64.Type}}), //Index column to be able to restore processed results to original order #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type), //Group by UID and Cost Code // then compute the SUMIFS using a shifted column // ASSUMES data sorted in Period order #"Grouped Rows" = Table.Group(#"Added Index", {"UID", "Cost Code"}, { {"Desired Result", (t)=> let #"Original Balance" = t{0}[Ending Balance] * 2, #"Add Index2" = Table.AddIndexColumn(t,"Index2",0,1,Int64.Type), #"Add Results" = Table.AddColumn(#"Add Index2", "Desired Results", each if [Index2]=0 then #"Original Balance"-[Ending Balance] else [Ending Balance] - #"Add Index2"{[Index2]-1}[Ending Balance], type number) in #"Add Results", type table[UID=text, Cost Code=text, Period=Int64.Type, Ending Balance=number, Index=Int64.Type, Index2=Int64.Type, Desired Results = number]}}), #"Expanded Desired Result" = Table.ExpandTableColumn(#"Grouped Rows", "Desired Result", {"Period", "Ending Balance", "Index", "Desired Results"}, {"Period", "Ending Balance", "Index", "Desired Results"}), #"Sorted Rows" = Table.Sort(#"Expanded Desired Result",{{"Index", Order.Ascending}}), #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index"}) in #"Removed Columns"
Omid_Motamedise Thanks for replying to my question.
For your code here, would you be able to explain to me the logic for the SUMIFS?
I'm assuming your code here works because of my dummy data which is simplified and no duplicates?
EDIT: Tried to implement your solution, however unable to verify if it works as it's been 15 minutes and it's till trying to "refresh" the preview. Thus had to abort it.
Sure,
in the proposed solution consider this part.
Table.SelectRows(Source,(x)=> _[Cost Cod]=x[Cost Cod] and x[Period]<_[Period] and _[UID]=x[UID])
for each row of table, it going to filter all the rows of that table with the same Cost code and UID but and with equal or less than period value .
by adding [Ending Balance] at the end of filtered table, the result is converted into a list including all the values in this column and then by list.last the will be selected.
to use this formula, your data should be sorted based on the period, otherwise you can use the next formula.
if [Period]=1 then [Ending Balance] else ([Ending Balance]-List.Last(Table.SelectRows(Source,(x)=> _[Cost Cod]=x[Cost Cod] and (x[Period]=_[Period]-1) and _[UID]=x[UID])[Ending Balance]))