Forum Discussion
Cumulative Sum with Conditional Reset for Negative Values and Store Change in Power BI
- Anonymous2 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 DAXlet 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 TeamIf 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
Thank You for the solution,
but it is not giving the expected output