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 of previous 4 weeks by using the week number

Hello everyone, 

 

Im trying to calculate the moving average of sales of the previous 4 weeks by using the week number. In addition, my database consists of many entrys, that contain the the number of the week, the year, the retail that is selling, the store of the retail and the SKU of the product. 

 

Thanks everyone for the help!

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

Per the description, I create some sample data and do calculation based on it.

vcazhengmsft_0-1650262190386.png

 

Create a Measure to calculate average of last two weeks.

SalesAVG(TwoWeeks) =
CALCULATE (
    AVERAGE ( 'Table'[Sell (units)] ),
    FILTER (
        ALLEXCEPT (
            'Table',
            'Table'[Year],
            'Table'[Store],
            'Table'[Retail],
            'Table'[SKU]
        ),
        (
            'Table'[Week]
                >= MAX ( 'Table'[Week] ) - 2
        )
            && ( 'Table'[Week] < MAX ( 'Table'[Week] ) )
    )
)

 

Then, the result looks like this.

vcazhengmsft_1-1650262190389.png

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let me know. Thanks a lot!

 

Best Regards,

Community Support Team _ Caiyun

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Here is a sample of the database: 

 

YearWeekRetailStoreSKUSell (units)
202212A12TV50INCH5
202212A43TV42INCH10
202213A43TV50INCH7
202213A12TV50INCH15

 

Assuming for this case, many number of SKU, retail and stores, i would like to add a column that gives me the sales average for each entry. In this case, for the newest entry (W14, and obviously for the previous ones) i would like to have something like this (in this case moving average of 2 weeks due to this short table):

 

YearWeekRetailStoreSKU Sales (units)SalesAVG(2weeks)
202214A12TV50INCH710 ((15+5)/2))

 

Best regards for all of you!

Hi @Anonymous ,

 

Per the description, I create some sample data and do calculation based on it.

vcazhengmsft_0-1650262190386.png

 

Create a Measure to calculate average of last two weeks.

SalesAVG(TwoWeeks) =
CALCULATE (
    AVERAGE ( 'Table'[Sell (units)] ),
    FILTER (
        ALLEXCEPT (
            'Table',
            'Table'[Year],
            'Table'[Store],
            'Table'[Retail],
            'Table'[SKU]
        ),
        (
            'Table'[Week]
                >= MAX ( 'Table'[Week] ) - 2
        )
            && ( 'Table'[Week] < MAX ( 'Table'[Week] ) )
    )
)

 

Then, the result looks like this.

vcazhengmsft_1-1650262190389.png

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let me know. Thanks a lot!

 

Best Regards,

Community Support Team _ Caiyun

Hello:

 

Please see the file for full solution.https://drive.google.com/file/d/1WzVWHmsjXXe_ncwOLQnRZh00W2WOvO11/view?usp=sharing 

 

I wanted to mention I added some tables that could be helpful to you. You will notice the Sales(Fact table) has a couple of columns you would typically delete but I left them there so you can see what is going on. 

 

Essentially, we created a ID field to connects your stores and products to their own dimension table. I added in some more data, a store and a product or two.

You could use the fields from the stores or products table to create visuals(vs. from Fact Table). The date calendar offers you a lot of flexibilty as the current week number is at zero and it tells you if week is complete or not.(See Filter panel). It is joined on date but could join to sales on wek number but date is generally where to connect, if you get sales at that grain.  The Date Table is continuous and marked as a date table. Here is a picture of results:

 

Whitewater100_0-1649898221323.png

 

 

Basically just broke up a flat file with everything and normalized it a bit. 

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.

Top Solution Authors