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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
james_pease
Helper III
Helper III

Calculate 7 Day Rolling Average (by day totals)

Hello all, this is probably a very simple fix. I am trying to calcualte a rolling average for the last 7 days  (R7) to smooth out our daily sales and transactions graphs.

 

While the measure I created is functiong properly, it is averaging all of the individual sales. What I am looking for, is to average the total daily sales. I have them grouped by type as well (GrubHub, Door Dash, Kiosk, Mobile, and Uber Eats). So again my formula is giving me the R7 but its giving me the R7 for order size versus daily totals. Please see below:

R7 Sales =
CALCULATE(
    AVERAGE('Sales (unpivoted)'[Value]), GROUPBY('Sales (unpivoted)','Sales (unpivoted)'[Type]),
    DATESINPERIOD('Sales (unpivoted)'[Date], LASTDATE ('Sales (unpivoted)'[Date]), -7, DAY),
    ALLEXCEPT('Sales (unpivoted)','Sales (unpivoted)'[Store])
)
 
james_pease_0-1664297037929.png

I have tried creating calculated columns for the 5 groups but the R7 spikes at the end. So I created 1 measure then used the Type coulmn as a way to group by.

 

Here is an example of a calculated column which lead to a spike on the last day:

 

Door Dash R7 Sales =
    Calculate(
        AVERAGE('Sales (unpivoted)'[Value]),
        DATESINPERIOD('Sales (unpivoted)'[Date], Lastdate('Sales (unpivoted)'[Date]),-7, Day),
        Filter('Sales (unpivoted)', [type] = "Door Dash"))
 
james_pease_1-1664297237954.png

 


Im sure there are multiple ways to solve this. We can solve the measure (R7 Sales) or the calculated columns (Door Dash R7 Sales). Either option is fine with me. Thank you in advance!

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

Hi @james_pease ,

You can create a measure as below to get the rolling average values:

R7 Sales =
VAR _max =
    MAX ( 'Sales (unpivoted)'[Date] )
VAR _min = _max - 6
RETURN
    IF (
        _min < MINX ( ALLSELECTED ( 'Sales (unpivoted)' ), 'Sales (unpivoted)'[Date] ),
        BLANK (),
        DIVIDE (
            CALCULATE (
                COUNTROWS ( 'Sales (unpivoted)' ),
                FILTER (
                    ALLSELECTED ( 'Sales (unpivoted)' ),
                    'Sales (unpivoted)'[Date] >= _min
                        && 'Sales (unpivoted)'[Date] <= _max
                )
            ),
            7
        )
    )

In addition, you can refer the following links to get it.

Rolling Averages In Power BI

Moving Average - 7 day

Rolling AVG - 7 days =
CALCULATE (
    SUM ( val_summary_by_day_source_tv_region[vals_booked] ) / 7,
    DATESINPERIOD (
        'Calendar Reference'[Date_2],
        LASTDATE ( 'Calendar Reference'[Date_2] ),
        -7,
        DAY
    ),
    ALLEXCEPT (
        val_source_ref_lookup,
        val_source_ref_lookup[val_business_category]
    )
)

If the above one can't help you get the desired result, please provide some sample data in your table 'HVDN table call volume(main)' (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
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

3 REPLIES 3
v-yiruan-msft
Community Support
Community Support

Hi @james_pease ,

You can create a measure as below to get the rolling average values:

R7 Sales =
VAR _max =
    MAX ( 'Sales (unpivoted)'[Date] )
VAR _min = _max - 6
RETURN
    IF (
        _min < MINX ( ALLSELECTED ( 'Sales (unpivoted)' ), 'Sales (unpivoted)'[Date] ),
        BLANK (),
        DIVIDE (
            CALCULATE (
                COUNTROWS ( 'Sales (unpivoted)' ),
                FILTER (
                    ALLSELECTED ( 'Sales (unpivoted)' ),
                    'Sales (unpivoted)'[Date] >= _min
                        && 'Sales (unpivoted)'[Date] <= _max
                )
            ),
            7
        )
    )

In addition, you can refer the following links to get it.

Rolling Averages In Power BI

Moving Average - 7 day

Rolling AVG - 7 days =
CALCULATE (
    SUM ( val_summary_by_day_source_tv_region[vals_booked] ) / 7,
    DATESINPERIOD (
        'Calendar Reference'[Date_2],
        LASTDATE ( 'Calendar Reference'[Date_2] ),
        -7,
        DAY
    ),
    ALLEXCEPT (
        val_source_ref_lookup,
        val_source_ref_lookup[val_business_category]
    )
)

If the above one can't help you get the desired result, please provide some sample data in your table 'HVDN table call volume(main)' (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

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

The link you provided helped a ton! This is the resulting formula that did work, (the others provided sadly didnt).

R7 Sales =
CALCULATE(
            Sum('Sales (unpivoted)'[Value]), Datesinperiod(Date_Value[DateValue], LASTDATE(Date_Value[DateValue]),7,DAY))
            /
            CALCULATE(DISTINCTCOUNT(Date_Value[DateValue]), Datesinperiod(Date_Value[DateValue], LASTDATE(Date_Value[DateValue]),7,DAY))
amitchandak
Super User
Super User

@james_pease , Use a date table, joined with 'Sales (unpivoted)'[Date]

 

and try meausre like

 

 

R7 Sales =
CALCULATE(
AVERAGEX(Values('Sales (unpivoted)','Sales (unpivoted)'[Type]),calculate(Sum('Sales (unpivoted)'[Value])))
DATESINPERIOD('date'[Date], LASTDATE ('date'[Date]), -7, DAY),
)

 

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

Average of Rolling, Average of Snapshots: https://youtu.be/_pZRdLAJxxA

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

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

Top Solution Authors