Forum Discussion

CarlosOlmos29's avatar
2 years ago
Solved

Column accumulated by categories in power query

Hello power BI community. I am trying to create an accumulated column in power query. In another post I found this formula to accumulate: List.Sum(List.Range(#“Added Index”[Actual],0,[Index])) Thi...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi CarlosOlmos29 

     

    Here is a solution by using your formula. The logic is to group the table by category column and add an Index column to each group table first. Then add a column to each group table with your formula to get the running total within a group. Finally expand the group table column. 

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSgQCJR0lQ6VYHfI4SUlJQLYR6exYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, Actual = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Actual", Int64.Type}}),
        
        // add the following steps
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Category"}, {{"GroupData", each let actualValues = _[Actual] in Table.AddColumn(Table.AddIndexColumn(_, "Index", 1, 1), "RunningTotal", each List.Sum(List.Range(actualValues,0,[Index])))}}),
        #"Expanded GroupData" = Table.ExpandTableColumn(#"Grouped Rows", "GroupData", {"Actual", "Index", "RunningTotal"}, {"Actual", "Index", "RunningTotal"})
    in
        #"Expanded GroupData"

     

    PhilipTreacy 's blog link shares another solution. You can have a try!

     

    Best Regards,
    Jing
    If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!