Forum Discussion
Moving average
- 1 year ago
Hi ManuMunozi
Please use the following DAX mesures
Cumulative Sales = VAR CurrentYear = SELECTEDVALUE('Calendario'[Año]) VAR CurrentMonth = SELECTEDVALUE('Calendario'[Mes]) RETURN CALCULATE( SUMX( 'Order Details', 'Order Details'[UnitPrice] * 'Order Details'[Quantity] * (1 - 'Order Details'[Discount]) ), FILTER( ALL('Calendario'), 'Calendario'[Año] = CurrentYear && 'Calendario'[Mes] <= CurrentMonth ) )Running Month Count = VAR CurrentYear = SELECTEDVALUE('Calendario'[Año]) VAR CurrentMonth = SELECTEDVALUE('Calendario'[Mes]) RETURN CALCULATE( DISTINCTCOUNT('Calendario'[Mes]), FILTER( ALL('Calendario'), 'Calendario'[Año] = CurrentYear && 'Calendario'[Mes] <= CurrentMonth && CALCULATE(COUNTROWS('Order Details')) > 0 ) )Running Average Per Year = DIVIDE( [Cumulative Sales], [Running Month Count], 0 )and use Date from your date column and Running Average Per Year mesure in a table visual
Hi ManuMunozi ,
You’re trying to create a moving monthly average that resets at the start of every year, based on your cumulative sales (Total Acumulado) divided by the number of months so far in that year. You've already built the cumulative total per year, which is half the battle. Now what you need is a dynamic counter for how many months have passed up to the current one, within the same year.
You can achieve this cleanly with DAX using a variable to count the number of months up to and including the current month for that year. Then, simply divide the cumulative total by that count. Here's the DAX formula you should use:
RunningAveragePerYear =
VAR CurrentYear = SELECTEDVALUE('YourDateTable'[Año])
VAR CurrentMonth = SELECTEDVALUE('YourDateTable'[Mes])
VAR Acumulado = CALCULATE(
SUM('YourSalesTable'[Ventas]),
FILTER(
ALL('YourDateTable'),
'YourDateTable'[Año] = CurrentYear &&
'YourDateTable'[Mes] <= CurrentMonth
)
)
VAR NumMeses =
CALCULATE(
COUNTROWS('YourDateTable'),
FILTER(
ALL('YourDateTable'),
'YourDateTable'[Año] = CurrentYear &&
'YourDateTable'[Mes] <= CurrentMonth
)
)
RETURN
DIVIDE(Acumulado, NumMeses)
This formula first captures the current year and month based on your context. It then calculates the cumulative sales for that year up to the current month, and separately counts how many months have passed. Finally, it divides the two, safely, using DIVIDE so you don’t end up with an error if there’s a zero.
Honestly, this approach is like giving your DAX a coffee and telling it to "wake up and count properly" every January. Clean, logical, and resets exactly the way you want it.
If you want, we can even make it fancier later — like excluding months with no sales or dynamically handling gaps — but for now, this will perfectly match your example.
Best regards,
- ManuMunozi1 year agoNew Member
Thank you for helping me. But I still have the problem. I tried your solution, and the problem is the divisor month
RunningAveragePerYear = VAR CurrentYear = SELECTEDVALUE('Calendario'[Año]) VAR CurrentMonth = SELECTEDVALUE('Calendario'[Mes]) VAR Acumulado = CALCULATE( [VENTAS], FILTER( ALL('Calendario'), 'Calendario'[Año] = CurrentYear && 'Calendario'[Mes] <= CurrentMonth ) ) VAR NumMeses = CALCULATE( COUNTROWS('Calendario'), FILTER( ALL('Calendario'), 'Calendario'[Año] = CurrentYear && 'Calendario'[Mes] <= CurrentMonth ) ) RETURN NumMeses
Like you see, that part of these measures shows me a wrong number. For example, for July 1996, it should show me 1, and then 2, and 3...
I share my proyect
https://drive.google.com/file/d/15sMKo2Y-nO-lw3cA5IL0EyH1Xzf1JBTZ/view?usp=sharing