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

How To Get A Sliceable Rolling 14-day Average Sales Count By Day?

I am attempting to create a measure that shows the 14-day average sales count over time by day. Given the sensitivity of the data I am handling, I cannot post that data here, but here is an analogous example of the sales of a generic product that models what I am trying to do:

 

Sale DateBrandColorSize
01/01/2001ARedSmall
01/05/2001ABlueMedium
01/05/2001ABlueLarge
01/14/2001BBlueMedium
01/16/2001BRedSmall
01/17/2001AYellowLarge
01/19/2001ABlueLarge
01/22/2001BBlueMedium
01/24/2001ABlueLarge
01/24/2001ABlueLarge
01/27/2001BBlueSmall
01/28/2001BRedLarge
01/31/2001BYellowSmall

 

I want to be able to count the average number of units sold per brand the last 14 days for every day in the date range. Further, there can be and often will be more than one sale on any given day in the dataset. My end goal is I want to be able to filter with slicers on different details. I may want to see a stacked line graph of the 14-day rolling average with brand sales broken down by brand in the legend and a slicer for color and size, and the average count must adjust according to the slicer selection. For example, if I set the slicer to blue only, it should show a rolling average count of sales for brand A of 0.21 and brand B of 0.07 units sold per day (given 3 and 1 sold respectively over 14 days, from 01/01/2001 through 01/14/2001).

When I attempt this in DAX like so:

 

 

14-Day Rolling Average Sales Count= 
COUNTROWS (
    FILTER (
        'Product Sales',
        'Product Sales'[Sale Date] <= 'Product Sales'[Sale Date]
            && 'Product Sales'[Sale Date] > DATEADD ( 'Product Sales'[Sale Date], -14, DAY )
    )
) / 14

 

 

Expectedly, I get only the current day's sales count divided by 14. How can I indicate in DAX that I want to look to previous dates up to 14 days back on any given date and get the average daily sales count? Again, I want to be able to maintain live filterability of the average with slicers. That is why I try to let the outer filters apply in the next example.

 

I attempt this, but the count fails and the graph shows up as blank.

 

 

14-Day Rolling Average Sales Count =
CALCULATE (
    COUNTROWS ( 'Product Sales' ),
    KEEPFILTERS (
        FILTER (
            ALL ( 'Product Sales' ),
            COUNTX (
                KEEPFILTERS (
                    FILTER (
                        'Product Sales',
                        EARLIER ( 'Product Sales'[Sale Date] ) < 'Product Sales'[Sale Date]
                            && EARLIER ( 'Product Sales'[Sale Date] ) >= 'Product Sales'[Sale Date] - 13
                    )
                ),
                'Product Sales'[Brand]
            )
        )
    )
) / 14

 

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous , Using a Date table

 

14-Day Rolling Average Sales Count = CALCULATE(AverageX(Values('Date'[Date]),calculate(Countrows('Product Sales')))
,DATESINPERIOD('Date'[Date ],MAX('Date'[Date ]),-14,DAY))

 

Rolling Days Formula: https://youtu.be/cJVj5nhkKBw

View solution in original post

2 REPLIES 2
amitchandak
Super User
Super User

@Anonymous , Using a Date table

 

14-Day Rolling Average Sales Count = CALCULATE(AverageX(Values('Date'[Date]),calculate(Countrows('Product Sales')))
,DATESINPERIOD('Date'[Date ],MAX('Date'[Date ]),-14,DAY))

 

Rolling Days Formula: https://youtu.be/cJVj5nhkKBw

Anonymous
Not applicable

Thanks! Perfect. Another problem I was also having was my date dimension had a date range extending outside of the supported range of Power BI. Once I added a filter to pare it down to size, it worked.

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