Search code examples
powerbidaxvisualizationpowerbi-desktopmeasure

Power BI :: calculate AVERAGE of previous 30 days


StackOveflow helped me create this beautiful metrics: the delta between what we pay and what we could have paid.

In the picture you can see how we took an action on the 31 of January 2023 and after that date we calculate the delta of it (the purple area):

enter image description here

But that fixed amount is based on the date 31/01/2013:

Realised Savings prod Fixed Amount = 
 var fixedAmount = CALCULATE([Realised Savings prod], FILTER(ALL(Data), Data[UsageDate]= DATE(2023,1,31)))

 RETURN  IF(NOT(ISBLANK([Realised Savings prod])) && MIN(Data[UsageDate])>= DATE(2023,1,31), fixedAmount)

Instead I would like to set that bar on the AVERAGE of the 30 days previous of the 31/01/2023. Because this will give a more realistic metrics.

ChatGPT suggests:

Realised Savings prod Fixed Amount =
VAR StartDate = DATE(2023, 1, 31) - 30
VAR FixedAmount =
    CALCULATE(
        [Realised Savings prod],
        FILTER(ALL(Data), Data[UsageDate] >= StartDate && Data[UsageDate] <= DATE(2023, 1, 31))
    )
RETURN
    IF(
        NOT(ISBLANK([Realised Savings prod])) && MIN(Data[UsageDate]) >= StartDate,
        FixedAmount
    )

But that just brakes the lines of the chart :(

Here the files:


Solution

  • Is it not just this:

    Realised Savings prod Fixed Amount = 
     var fixedAmount = CALCULATE([Realised Savings prod], FILTER(ALL(Data), Data[UsageDate]<= DATE(2023,1,31) && Data[UsageDate]>= DATE(2023,1,1)))
     var ave = fixedAmount/30
    
     RETURN   IF(NOT(ISBLANK([Realised Savings prod])) && MIN(Data[UsageDate])>= DATE(2023,1,31), ave)