Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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?
Solved! Go to Solution.
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
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
@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
Proud to be a Super User! |
|
Thank You for the solution,
but it is not giving the expected output
Check out the July 2025 Power BI update to learn about new features.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
User | Count |
---|---|
26 | |
10 | |
10 | |
9 | |
6 |