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 need the sum to reset to zero under two specific conditions:

 

1. Large Negative Values: If the cumulative sum goes below zero due to a large negative value, it should reset to zero.

2. Store Change: When the `STORE` changes, the cumulative sum should reset, starting fresh with the first value for the new store.

 

For example, in the attached screenshot, you'll see that the cumulative sum (shown in the `Remaining Boxes` column) behaves as follows:

- If the cumulative sum is positive and a subsequent large negative value brings it below zero, the sum resets to zero.

- When the store changes, the cumulative sum starts over from the value corresponding to that store.

 

Could someone guide me on how to write the DAX formula to achieve this?

 

  • 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

3 Replies

  • ananyarshetty , You can create a calculated column for this

     

    Remaining Boxes =
    VAR CurrentStore = 'YourTable'[STORE]
    VAR CurrentDate = 'YourTable'[Date] -- Assuming you have a date column to order the rows
    VAR CurrentBoxesGoal = 'YourTable'[Boxes-Goal]

    VAR CumulativeSum =
    CALCULATE(
    SUMX(
    FILTER(
    'YourTable',
    'YourTable'[STORE] = CurrentStore &&
    'YourTable'[Date] <= CurrentDate
    ),
    'YourTable'[Boxes-Goal]
    )
    )

    VAR ResetCumulativeSum =
    IF(CumulativeSum < 0, 0, CumulativeSum)

    RETURN
    ResetCumulativeSum

    • ananyarshetty's avatar
      ananyarshetty
      Regular Visitor

      Thank You for the solution,
      but it is not giving the expected output

  • Anonymous's avatar
    Anonymous
    Not applicable

    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