Forum Discussion
BaxterT
2 years agoFrequent Visitor
Subtracting Cumulative Data
Hi all, I'll try and make this as clear as possible - I receive data that is already summed cumulatively each month for each contract that begins each financial year. I need to subtract each ...
- 2 years ago
Hello - this is how you can do it in Power Query. This solution uses ImkeF's custom function for referencing the previous row.
let fn = (MyTable as table, MyColumnName as text) => let Source = MyTable, ShiftedList = {null} & List.RemoveLastN(Table.Column(Source, MyColumnName),1), Custom1 = Table.ToColumns(Source) & {ShiftedList}, Custom2 = Table.FromColumns(Custom1, Table.ColumnNames(Source) & {"Previous Row"}) in Custom2 , documentation = [ Documentation.Name = " Table.PreviousRow ", Documentation.Description = " Superfast way to reference previous row ", Documentation.LongDescription = " Superfast way to reference previous row ", Documentation.Category = " Table ", Documentation.Source = " www.TheBIccountant.com . http://tiny.cc/hhus5y . ", Documentation.Version = " 1.0 ", Documentation.Author = " Imke Feldmann: www.TheBIccountant.com. http://tiny.cc/hhus5y . ", Documentation.Examples = {[Description = " ", Code = " ", Result = " "]}], Result = Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation)), Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdFBDoMgEIXhu7A2gXkDU3sWw/2vUSACUp/tYoKLL8gPx+HEbQ7w0LKKjx4BKJ+pTd6+QepAqpBAiHWCsG6iHnH+peKmCEgd7GWMAOtA4hDgJa82dzBL6jGVCFta0xT3kLbHeRvgKY+bjJbLfSlvsRGrP1oiEeujnCfV/zH6EFOfQ4hYY94u5w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Contract Number" = _t, #"Financial Year" = _t, Date = _t, #"New Patients" = _t, #"Outcome Required" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Contract Number", Int64.Type}, {"Financial Year", type text}, {"Date", type date}, {"New Patients", Int64.Type}, {"Outcome Required", Int64.Type}}), #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Contract Number", Order.Ascending}, {"Financial Year", Order.Ascending}, {"Date", Order.Ascending}}), #"Grouped Rows" = Table.Group( #"Sorted Rows", {"Contract Number", "Financial Year"}, { {"Data", each fn(_, "New Patients") } } ), #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"Date", "New Patients", "Outcome Required", "Previous Row"}, {"Date", "New Patients", "Outcome Required", "Previous Row"}), #"Added Custom" = Table.AddColumn(#"Expanded Data", "New", each [New Patients] - ( [Previous Row] ?? 0 )) in #"Added Custom" - 2 years ago
Hi,
Another solution with Table.NestedJoin
let
Source = YourSource,
Index = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
Next_Month = Table.TransformColumns(Index,{{"Date", each Date.AddMonths(_,1), type date}}),
NestedJoin = Table.NestedJoin(
Index, {"Contract Number", "Financial Year", "Date"},
Next_Month, {"Contract Number", "Financial Year", "Date"}, "Prev_Month", JoinKind.LeftOuter),
Expand = Table.ExpandTableColumn(NestedJoin, "Prev_Month", {"New Patients"}, {"New Patients.1"}),
Subtracting = Table.AddColumn(Expand, "Outcome Required", each List.Sum({[New Patients], -[New Patients.1]}), type number),
Sort = Table.Sort(Subtracting,{{"Index", Order.Ascending}}),
SelectColumns = Table.SelectColumns(Sort,{"Contract Number", "Financial Year", "Date", "New Patients", "Outcome Required"})
in
SelectColumnsStéphane
slorin
2 years agoSuper User
You can unpivot your 7 columns before join