Forum Discussion
Anonymous
2 years agoNot applicable
Sumifs in power query
Hi all! I'm working in the automation of a report. Everything has gone well, but I haven't found a work around for the next problem. The dataset I'm working with looks like this (with other ...
- 2 years ago
Replaced with slightly faster version.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XcyxDcAgDAXRXVwj2XyMUycZw2L/NQiJpQDd6RXnTiclymBllFFVhFpyula24HthSHBMct58XMAwho6q0+XnMl8ePj7WbRJcZZ0E28utAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"week start date" = _t, #"Forecast value" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"week start date", type date}, {"Forecast value", Int64.Type}}), sumifs = (tbl as table) => Table.AddColumn( tbl, "12 week forecast", each List.Sum(Table.SelectRows( tbl, (r) => r[week start date] > [week start date] and r[week start date] <= Date.AddDays([week start date],84) )[Forecast value] ) ) , group1 = Table.Group(#"Changed Type", {"ID", "week start date"}, {"Forecast value", each List.Sum([Forecast value])}), // first group by ID and dates then by just id. you could skip this grouping if there you do not have multiple rows with the same ID and date. group2 = let buffer = Table.Buffer(group1) in Table.Group( buffer, "ID", {"sumifs", each sumifs(_)}), #"Removed Other Columns" = Table.SelectColumns(group2,{"sumifs"}), #"Expanded sumifs" = Table.ExpandTableColumn(#"Removed Other Columns", "sumifs", {"ID", "week start date", "Forecast value", "12 week forecast"}, {"ID", "week start date", "Forecast value", "12 week forecast"}) in #"Expanded sumifs" - 2 years ago
hi, Anonymous a little bit complicated but I tried to speed things up. Replace your_table with your table or last step name.
// this function calculates adds "weeks fc" column as running total for upcoming number of weeks wks_forecast = (tbl, weeks) => [rows = List.Buffer(Table.ToRecords(tbl)), values = List.Buffer(tbl[Forecast value]), gena = List.Generate( () => [i = 0, fc = try List.Sum(List.Range(values, 1, weeks)) otherwise 0, rec = rows{i} & [weeks fc = fc]], (x) => rows{x[i]}? <> null, (x) => [i = x[i] + 1, fc = x[fc] - values{i} + (values{i + weeks}? ?? 0), rec = rows{i} & [weeks fc = fc]], (x) => x[rec] ), t = Table.FromRecords(gena)][t], // here we group by ID ans run our function g = Table.Group(your_table, "ID", {"fcast", each wks_forecast(Table.Sort(_, "week start date"), 12)}), // combine things together z = Table.Combine(g[fcast])
AlienSx
2 years agoSuper User
hi, Anonymous a little bit complicated but I tried to speed things up. Replace your_table with your table or last step name.
// this function calculates adds "weeks fc" column as running total for upcoming number of weeks
wks_forecast = (tbl, weeks) =>
[rows = List.Buffer(Table.ToRecords(tbl)),
values = List.Buffer(tbl[Forecast value]),
gena = List.Generate(
() =>
[i = 0,
fc = try List.Sum(List.Range(values, 1, weeks)) otherwise 0,
rec = rows{i} & [weeks fc = fc]],
(x) => rows{x[i]}? <> null,
(x) =>
[i = x[i] + 1,
fc = x[fc] - values{i} + (values{i + weeks}? ?? 0),
rec = rows{i} & [weeks fc = fc]],
(x) => x[rec]
),
t = Table.FromRecords(gena)][t],
// here we group by ID ans run our function
g = Table.Group(your_table, "ID", {"fcast", each wks_forecast(Table.Sort(_, "week start date"), 12)}),
// combine things together
z = Table.Combine(g[fcast])