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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

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

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors