Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Moving Average Using an Index Not Working

Fruit Dataset   EDIT - INCLUDED RAW DATASET AND EXAMPLE OUTCOME

 

Hi, I'm working on a dynamic moving average for the prices of fruit. I've written a measure that works correctly in some cases but not all. 

 

Please can someone help me with troubleshooting this. 

 

The idea is the user can select a number of days (Purchase Date) to average over which can be built into a slicer. Technically I want it to be a number of datapoints rather than days (so it will ignore weekends and bank hols where there are no prices for example). 

 

Some added complication - the average should be calculated for each fruit and for each "Delivery Date" which should be able to aggregate from months >quarters > seasons >years. Purchase date determines the average period, delivery date can be seen as a different category. 

I've highlighted an example where it starts to go wrong compared to manually calculating this in excel. 

 

 

The DAX is below:

 

0.Moving Avg =


Var Current_Date = FIRSTNONBLANK('Fruit Data'[Purchase Date],1)
Var Number_Of_Days = SELECTEDVALUE('Average Days'[Average Days])
Var Offset_Date = filter(
ALL('Calendar'[Date]),
RANKx(FILTER(ALL('Calendar'),'Calendar'[Date]<(Current_Date-1)),'Calendar'[Date],,DESC)=Number_Of_Days)

VAR avrg =
CALCULATE( AVERAGE('Fruit Data'[Price]),
FILTER ( ALL ('Fruit Data'[Purchase Date]), 'Fruit Data'[Purchase Date]< Current_Date && 'Fruit Data'[Purchase Date] > Offset_Date))

return avrg
 
 
 
mlwills_0-1641397214561.png

 

mlwills_1-1641397562770.png

 

1 ACCEPTED SOLUTION

Hi @Anonymous ,

According to your new sample, here's my solution.

Create two measures.

Avg of Price = 
CALCULATE (
    AVERAGE ( 'Fruit Data'[Price] ),
    FILTER (
        ALL ( 'Fruit Data' ),
        'Fruit Data'[Purchase Date] = MAX ( 'Fruit Data'[Purchase Date] )
            && 'Fruit Data'[Product] = MAX ( 'Fruit Data'[Product] )
            && 'Fruit Data'[Delivery FY] = MAX ( 'Fruit Data'[Delivery FY] )
    )
)
Moving Avg = 
VAR Current_Date =
    MAX ( 'Fruit Data'[Purchase Date] )
VAR Number_Of_Days =
    SELECTEDVALUE ( 'Average Days'[Average Days] )
VAR Offset_Date =
    FILTER (
        ALL ( 'Calendar'[Date] ),
        RANKX (
            FILTER ( ALL ( 'Calendar' ), 'Calendar'[Date] < ( Current_Date ) ),
            'Calendar'[Date],
            ,
            DESC
        ) = Number_Of_Days
    )
VAR avrg =
    AVERAGEX (
        FILTER (
            ALL ( 'Fruit Data' ),
            'Fruit Data'[Delivery FY] = MAX ( 'Fruit Data'[Delivery FY] )
                && 'Fruit Data'[Product] = MAX ( 'Fruit Data'[Product] )
                && 'Fruit Data'[Purchase Date] < Current_Date
                && 'Fruit Data'[Purchase Date] >= Offset_Date
        ),
        [Avg of Price]
    )
RETURN
    avrg

Get the expected result.

vkalyjmsft_0-1642041876286.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-yanjiang-msft
Community Support
Community Support

Hi @Anonymous ,

In your below formula, you should not use "Current_Date-1", as the Purchas Date is not continuous.

FILTER(ALL('Calendar'),'Calendar'[Date]<(Current_Date-1))

Here's my solution.

1.The formula of Calendar table.

Calendar = VALUES('Fruit Data'[Purchase Date])

2.The formula of Moving Avg measure.

Moving Avg =
VAR Current_Date =
    MAX ( 'Fruit Data'[Purchase Date] )
VAR Number_Of_Days =
    SELECTEDVALUE ( 'Average Days'[Average Days] )
VAR Offset_Date =
    FILTER (
        ALL ( 'Calendar'[Date] ),
        RANKX (
            FILTER ( ALL ( 'Calendar' ), 'Calendar'[Date] < ( Current_Date ) ),
            'Calendar'[Date],
            ,
            DESC
        ) = Number_Of_Days
    )
VAR avrg =
    CALCULATE (
        AVERAGE ( 'Fruit Data'[Price] ),
        FILTER (
            ALL ( 'Fruit Data' ),
            'Fruit Data'[Delivery Date] = MAX ( 'Fruit Data'[Delivery Date] )
                && 'Fruit Data'[Product] = MAX ( 'Fruit Data'[Product] )
                && 'Fruit Data'[Purchase Date] < Current_Date
                && 'Fruit Data'[Purchase Date] >= Offset_Date
        )
    )
RETURN
    avrg

 Get the result.

vkalyjmsft_0-1641882649307.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi  thanks so much for looking at my problem, I really appreciate the help! 

I've tested this on an example where we are looking at Grapes for the Delivery FT 2023. I think the problem is where we need the single Delivery Date averages to be aggregated to one delivery FY.

 

I've created a new sheet here Fruit Prices Model Test  which I think demonstrated the problem and expected outcome if you look in the "Grapes Test Sample" tab. I've screenshotted the output your model gives under these conditions vs the excel calcs. 

 

 

mlwills_0-1641904090957.png

 

Hi @Anonymous ,

According to your new sample, here's my solution.

Create two measures.

Avg of Price = 
CALCULATE (
    AVERAGE ( 'Fruit Data'[Price] ),
    FILTER (
        ALL ( 'Fruit Data' ),
        'Fruit Data'[Purchase Date] = MAX ( 'Fruit Data'[Purchase Date] )
            && 'Fruit Data'[Product] = MAX ( 'Fruit Data'[Product] )
            && 'Fruit Data'[Delivery FY] = MAX ( 'Fruit Data'[Delivery FY] )
    )
)
Moving Avg = 
VAR Current_Date =
    MAX ( 'Fruit Data'[Purchase Date] )
VAR Number_Of_Days =
    SELECTEDVALUE ( 'Average Days'[Average Days] )
VAR Offset_Date =
    FILTER (
        ALL ( 'Calendar'[Date] ),
        RANKX (
            FILTER ( ALL ( 'Calendar' ), 'Calendar'[Date] < ( Current_Date ) ),
            'Calendar'[Date],
            ,
            DESC
        ) = Number_Of_Days
    )
VAR avrg =
    AVERAGEX (
        FILTER (
            ALL ( 'Fruit Data' ),
            'Fruit Data'[Delivery FY] = MAX ( 'Fruit Data'[Delivery FY] )
                && 'Fruit Data'[Product] = MAX ( 'Fruit Data'[Product] )
                && 'Fruit Data'[Purchase Date] < Current_Date
                && 'Fruit Data'[Purchase Date] >= Offset_Date
        ),
        [Avg of Price]
    )
RETURN
    avrg

Get the expected result.

vkalyjmsft_0-1642041876286.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thank you so much!! Awesome it finally works.

lbendlin
Super User
Super User

Please provide sanitized sample data that fully covers your issue. Paste the data into a table in your post or use one of the file services. Please show the expected outcome.

Anonymous
Not applicable

Thanks for your help here is the raw data and an example of how I want the calculation to work:


 Fruit Dataset 


In terms of how I'd like it to look in PowerBI, I would need to plot the data in both a table and on a graph where for each Purchase Data, and each Delivery Date there would be a dynamically calculated rolling average that could be 5,10,15,20 days (etc). I should also be able to aggregate up the delivery date to a delivery month or delivery year and the average should still calculate. In the example in the sheet the delivery period is Summer 2024.


As long as the average calculates correctly in a table I can figure out the rest from there though.

 Fruit Dataset 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.