Forum Discussion

ananyarshetty's avatar
ananyarshetty
Regular Visitor
2 years ago
Solved

Cumulative Sum with Conditional Reset for Negative Values and Store Change in Power BI

  Hi everyone,   I'm working on a Power BI report and need help creating a DAX expression for a calculated column. My goal is to calculate a cumulative sum of a column (`Boxes-Goal`), but I ...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi, ananyarshetty 

    Thanks for bhanu_gautam reply. You can use M language to achieve your need. It is so difficult to realize this requirement using DAX

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY9LEsQgCETv4jpW8Y+eJeX9rzG2ziIh2WDDoxGuq/BZjiIkxjSF6g7juBOeggUpByUmSzhs4YlhGHeb0b0nhmq1QN4lMUyrrHjydwEkRB8IonoA+WvNtuDa1tpm7X54j/aPT4bTzWBjND4hylU7oJ6coO4jllVm0xg/", BinaryEncoding.Base64), Compression.Deflate)), 
        let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Store = _t, WeekID = _t, #"Boxes-Goal" = _t, #"Remaining Boxes" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Store", Int64.Type}, {"WeekID", Int64.Type}, {"Boxes-Goal", Int64.Type}, {"Remaining Boxes", Int64.Type}}),
        FX = (values as list) as list =>
            let
                GRTList = List.Generate(
                    () => [GRT = values{0}, i = 0],
                    each [i] < List.Count(values),
                    each 
                        let
                            nextGRT = if [GRT] > 0 then [GRT] + values{[i] + 1} else values{[i] + 1}
                        in
                            [GRT = nextGRT, i = [i] + 1],
                    each [GRT]
                )
            in
                GRTList,
     
        // Group by Store and apply transformations
        Grouped = Table.Group(
            #"Changed Type",
            {"Store"},
            {
                {"Transformed", each 
                    let
                        weekidlist = List.Buffer([WeekID]),
                        boxesgoallist = List.Buffer([#"Boxes-Goal"]),
                        result = Table.FromColumns(
                            {weekidlist, boxesgoallist, List.Transform(FX(boxesgoallist), each if _ < 0 then 0 else _)},
                            {"WeekID", "Boxes-Goal", "Output"}
                        )
                    in
                        result
                }
            }
        ),
        #"Expanded Transformed" = Table.ExpandTableColumn(Grouped, "Transformed", {"WeekID", "Boxes-Goal", "Output"}, {"WeekID", "Boxes-Goal", "Output"})
    in
        #"Expanded Transformed"

     

    Best Regards,
    Yang

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know.
    Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum