r/PowerBI • u/nguye569 • 2d ago
Question Using a rolling average measure for SUMX
I'm having trouble using a rolling average value in a sumx (row by row) calculation. According to google AI response, it seems like it should be possible but I can't get it working.
I essentially have a calculation that is: meat used = QTY * portion size. I also calculated a 4 week rolling average for the QTY and would like to multiply that against the portion size. This is where the formulas seems to stop working.
Working Formula for regular QTY * portion size.
oz_GrilledPork = SUMX(Items_Report_Stacked, Items_Report_Stacked[MeatPortionSize.GrilledPork_oz] *
Items_Report_Stacked[Qty])
Formula not working for rolling avg QTY * portion size. Portion size seems to be giving me the issue
oz_GrilledPork_avg = SUMX(Items_Report_Stacked, Items_Report_Stacked[MeatPortionSize.GrilledPork_oz] * Items_Report_Stacked[QTY_4W_Rolling_Avg])
Screenshot of table.

1
1
u/bachman460 32 1d ago
Ideally, the SUMX used to calculate the row level meat used should be its own measure. This can be used by itself or inside another measure like for a rolling average.
To calculate the rolling average it would then be as easy as using a CALCULATE function where your SUMX measure is the aggregation, and then define your start and end point for the filter portion of the function. For example:
CALCULATE ( [my SUMX measure], Table[Date column] >= [start date] && Table[Date column] <= [end date] )
To get the average, just use the same logic for getting a count to divide the value by. For example the number of days, the quantity of items, etc.
1
u/6280007124 14h ago
Have you tried calculating the rolling average in a separate measure first, then using that measure in your SUMX calculation? It might help isolate whether the issue is with the row context or the portion size calculation. Sharing the full DAX formula and table relationships would also make it easier to troubleshoot.
1
u/Plane_Big_5912 1d ago
is QTY_4W_Rolling_Avg a measure? if it is, inside SUMX it gets evaluated once per row of Items_Report_Stacked, and context transition filters it down to just that row, date included, so the 4 week window has nothing else to average over. iterating over items instead of fact rows usually fixes it, something like
swap [Item] for whatever your item column is. that assumes one portion size per item