Forum Discussion
Jerid421
Helper II
2 years agoMake Negative Values Zero at a Rolled Up Level
I am trying develop a measure, OpenQuantityRT, to recreate a business process that is currently being done manually in a spreadsheet. The end user sums up quantities ("In-Stock Materials"; 'SupplyDe...
Jerid421
Helper II
2 years agoThis is the code I am currently working with:
OpenQuantityRT_AllScopes =
VAR __IsMaterialLevel =
ISINSCOPE('SupplyDemandItems'[Material])
VAR __IsElementCategoryLevel =
ISINSCOPE('SupplyDemandItems'[MRPElementCategoryShortName])
VAR __CurrentYearWeek =
MAX('Date Calendar'[YearWeekNum])
VAR __RegularRollingTotal =
CALCULATE(
SUM('SupplyDemandItems'[MRPElementOpenQuantity]),
FILTER(
ALL('Date Calendar'),
'Date Calendar'[YearWeekNum] <= __CurrentYearWeek
)
)
VAR __MaterialLevelandUpRollingTotal =
IF(
__IsMaterialLevel,
CALCULATE(
__RegularRollingTotal,
ALLEXCEPT('SupplyDemandItems', 'SupplyDemandItems'[Material])
),
BLANK()
)
RETURN
IF(
__IsElementCategoryLevel,
__RegularRollingTotal, // Detailed level shows actuals, including negatives
MAX(__MaterialLevelandUpRollingTotal, 0) // Material level and up zero out if negative
)- Jerid4212 years ago
Helper II
I came at it a different way and I think it's better, but now it doesn't seem to be calculating the rolling total correctly (at all):
OpenQuantityRT =// Determines if the current context is at the MRPElementCategoryShortName levelVAR __IsElementCategoryLevel =ISINSCOPE('SupplyDemandItems'[MRPElementCategoryShortName])// Retrieves the maximum YearWeekNum from the Date Calendar table for the current context, to implement the "Running Total"VAR __CurrentYearWeek =MAX('Date Calendar'[YearWeekNum])// Corrects the SupplyDemandItems quantities by setting them to zero if their cumulative total is negativeVAR __CorrectedSupplyDemandItems =ADDCOLUMNS('SupplyDemandItems',"CorrectedQuantity",IF(CALCULATE(SUM('SupplyDemandItems'[MRPElementOpenQuantity]),FILTER(ALL('SupplyDemandItems'), 'SupplyDemandItems'[Material] = EARLIER('SupplyDemandItems'[Material]))) < 0,0,'SupplyDemandItems'[MRPElementOpenQuantity]))// Calculates the rolling total without negatives for the Material level and upVAR __RollingTotalNoNegs =CALCULATE(SUMX(__CorrectedSupplyDemandItems,[CorrectedQuantity]),FILTER(ALL('Date Calendar'),'Date Calendar'[YearWeekNum] <= __CurrentYearWeek))// Calculates the regular rolling total for the ElementCategory levelVAR __RegularRollingTotal =CALCULATE(SUM('SupplyDemandItems'[MRPElementOpenQuantity]),FILTER(ALL('Date Calendar'),'Date Calendar'[YearWeekNum] <= __CurrentYearWeek))// Returns the appropriate rolling total based on the current context levelRETURNIF(__IsElementCategoryLevel,__RegularRollingTotal,MAX(__RollingTotalNoNegs, 0))- Greg_Deckler2 years ago
Community Champion
Jerid421 Any chance you can share some sample data? Seems like you have a mix of a running total problem as well as measure totals. This may help the most: Matrix Measure Total Triple Threat Rock & Roll - Microsoft Fabric Community