Forum Discussion
jackj
3 years agoHelper I
Sum By User ID Between Two Dynamic Dates
Hi, I have a very simple data table and I am looking to create a custom column in power query, to sum the subtotal of sales for a particular user ID in each row between dynamic dates as per each row....
- 3 years ago
Hi jackj ,
for performance reasons, I would to this on grouped data (user level). So the code gets a lit complicated, unfortunately:let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtIBkvrGhvpGBkZGQI6KoYGegYGCUqwOkrShAVzaFEnWBCJrZILQjEUaSbMRstmmGLqhmmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UserID = _t, Date = _t, Sales = _t]), #"Changed Type" = Table.TransformColumnTypes(Source, {{"UserID", Int64.Type}, {"Date", type date}, {"Sales", Currency.Type}}), Custom1 = Table.Group( #"Changed Type", {"UserID"}, { { "UserID_Partition", (P) => Table.AddColumn( Table.Buffer(P), "SalesM", each [Sales14 = List.Sum(Table.SelectRows( P, (Partition) => Partition[Date] >= Date.AddDays(_[Date], - 14) and Partition[Date] <= _[Date] )[Sales]), Sales7 = List.Sum(Table.SelectRows( P, (Partition) => Partition[Date] >= Date.AddDays(_[Date], - 7) and Partition[Date] <= _[Date] )[Sales]) ] ) } } ), #"Expanded UserID_Partition" = Table.ExpandTableColumn(Custom1, "UserID_Partition", {"Date", "Sales", "SalesM"}, {"Date", "Sales", "SalesM"}), #"Expanded SalesM" = Table.ExpandRecordColumn(#"Expanded UserID_Partition", "SalesM", {"Sales14", "Sales7"}, {"Sales14", "Sales7"}) in #"Expanded SalesM"Pls also check the file enclosed.
ImkeF
3 years agoCommunity Champion
Hi jackj ,
for performance reasons, I would to this on grouped data (user level). So the code gets a lit complicated, unfortunately:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtIBkvrGhvpGBkZGQI6KoYGegYGCUqwOkrShAVzaFEnWBCJrZILQjEUaSbMRstmmGLqhmmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UserID = _t, Date = _t, Sales = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source, {{"UserID", Int64.Type}, {"Date", type date}, {"Sales", Currency.Type}}),
Custom1 = Table.Group(
#"Changed Type",
{"UserID"},
{
{
"UserID_Partition",
(P) =>
Table.AddColumn(
Table.Buffer(P),
"SalesM",
each [Sales14 = List.Sum(Table.SelectRows(
P,
(Partition) => Partition[Date] >= Date.AddDays(_[Date], - 14) and Partition[Date] <= _[Date]
)[Sales]),
Sales7 = List.Sum(Table.SelectRows(
P,
(Partition) => Partition[Date] >= Date.AddDays(_[Date], - 7) and Partition[Date] <= _[Date]
)[Sales])
]
)
}
}
),
#"Expanded UserID_Partition" = Table.ExpandTableColumn(Custom1, "UserID_Partition", {"Date", "Sales", "SalesM"}, {"Date", "Sales", "SalesM"}),
#"Expanded SalesM" = Table.ExpandRecordColumn(#"Expanded UserID_Partition", "SalesM", {"Sales14", "Sales7"}, {"Sales14", "Sales7"})
in
#"Expanded SalesM"
Pls also check the file enclosed.