Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
ananyarshetty
Regular Visitor

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

Screenshot (791).png

 

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?

 

1 ACCEPTED SOLUTION
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

vyaningymsft_0-1724665640599.png

 

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

View solution in original post

3 REPLIES 3
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

vyaningymsft_0-1724665640599.png

 

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

bhanu_gautam
Super User
Super User

@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




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






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

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.