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
kjanderson2
Regular Visitor

Pulling a Rolling Average - Dynamic Time

Hello!

 

I'm seeing a lot of posts on this, however I'm not finding a solution for my specific situation. I've got 3 relevant columns : Date, Item, Sales. I want an average of the last 6 weeks sales by item. I need the average to read blank as 0, and the last 6 weeks is not as much time based as it is the last 6 weeks that are present in the data set.

 

The date may not be the value I use to filter the data -  I'm thinking I need to add a week column that would filter the 6 weeks between todays week (helper column) - 6, but I don't know if that's possible. I have nearly every week of data, there are just times that I don't have the past two weeks (data sent biweekly) and I need to pull the previous two weeks. 

 

I'm pretty new to all this so all help is appreciated. Please let me know what I can provide to help solve this. 

1 ACCEPTED SOLUTION
v-juanli-msft
Community Support
Community Support

Hi @kjanderson2 

Create calculated columns

year-week = FORMAT([date],"yyyy-ww")

Create measures

sum per week =
CALCULATE (
    SUM ( Sheet3[value] ),
    FILTER (
        ALLSELECTED ( Sheet3 ),
        Sheet3[item]
            = MAX ( Sheet3[item] )
            && Sheet3[year-week]
                = MAX ( Sheet3[year-week] )
    )
)

sum last 6 weeks =
CALCULATE (
    SUM ( Sheet3[value] ),
    FILTER (
        ALLSELECTED ( Sheet3 ),
        DATEDIFF (
            Sheet3[date],
            TODAY (),
            WEEK
        ) < 6
            && Sheet3[item]
                = MAX ( Sheet3[item] )
    )
)


count_last6weeks =
CALCULATE (
    DISTINCTCOUNT ( Sheet3[year-week] ),
    FILTER (
        ALLSELECTED ( Sheet3 ),
        DATEDIFF (
            Sheet3[date],
            TODAY (),
            WEEK
        ) < 6
            && Sheet3[item]
                = MAX ( Sheet3[item] )
            && [sum per week] <> 0
    )
)

average = [sum last 6 weeks]/[count_last6weeks]

If there are no missing data

Capture3.JPG

If there is missing data,

Capture4.JPG

Best Regards
Maggie
Community Support Team _ Maggie Li
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

1 REPLY 1
v-juanli-msft
Community Support
Community Support

Hi @kjanderson2 

Create calculated columns

year-week = FORMAT([date],"yyyy-ww")

Create measures

sum per week =
CALCULATE (
    SUM ( Sheet3[value] ),
    FILTER (
        ALLSELECTED ( Sheet3 ),
        Sheet3[item]
            = MAX ( Sheet3[item] )
            && Sheet3[year-week]
                = MAX ( Sheet3[year-week] )
    )
)

sum last 6 weeks =
CALCULATE (
    SUM ( Sheet3[value] ),
    FILTER (
        ALLSELECTED ( Sheet3 ),
        DATEDIFF (
            Sheet3[date],
            TODAY (),
            WEEK
        ) < 6
            && Sheet3[item]
                = MAX ( Sheet3[item] )
    )
)


count_last6weeks =
CALCULATE (
    DISTINCTCOUNT ( Sheet3[year-week] ),
    FILTER (
        ALLSELECTED ( Sheet3 ),
        DATEDIFF (
            Sheet3[date],
            TODAY (),
            WEEK
        ) < 6
            && Sheet3[item]
                = MAX ( Sheet3[item] )
            && [sum per week] <> 0
    )
)

average = [sum last 6 weeks]/[count_last6weeks]

If there are no missing data

Capture3.JPG

If there is missing data,

Capture4.JPG

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.