Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Rolling Average Grouped by Week

 Hey,

 

I am working with two tables, related on the below Date1 column.  I want to ultimately get a rolling 7 day average (represented as a line) on the Line and Stacked Column. This line needs to be grouped by week (ideally the graph would show the starting date of the week represented). I am able to get a rolling average if I don't try to group each date into a week (see below screenshot) but I can't quite seem to get a 7 day rolling average and also have the bars grouped by week.

 

Screenshot

 

 

 

Data Table:

IDZip CodeDate1Date2AgeRaceEthnicitySexStatusIn
1280273/11/20203/11/202039WhiteNot Hispanic or LatinoMaleRD 
3280253/11/20203/16/202047UnknownNot Hispanic or LatinoFemaleRD 
4280273/13/20203/18/202048WhiteNot Hispanic or LatinoFemaleRD 

 

Date Table:

Date
2/23/2020 0:00
2/24/2020 0:00
2/25/2020 0:00
2/26/2020 0:00
2/27/2020 0:00
2/28/2020 0:00
2/29/2020 0:00
3/1/2020 0:00
3/2/2020 0:00
3/3/2020 0:00
3/4/2020 0:00
3/5/2020 0:00
3/6/2020 0:00
3/7/2020 0:00
3/8/2020 0:00
3/9/2020 0:00
3/10/2020 0:00
3/11/2020 0:00
3/12/2020 0:00
3/13/2020 0:00
3/14/2020 0:00
3/15/2020 0:00
3/16/2020 0:00
3/17/2020 0:00
3/18/2020 0:00
3/19/2020 0:00
3/20/2020 0:00
3/21/2020 0:00
3/22/2020 0:00
3/23/2020 0:00
3/24/2020 0:00
3/25/2020 0:00
3/26/2020 0:00
3/27/2020 0:00
3/28/2020 0:00
3/29/2020 0:00
3/30/2020 0:00
3/31/2020 0:00
  • Hi, Anonymous 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

    Table:

     

    Calendar(a calculated table):

    Calendar = CALENDAR(DATE(2019,1,1),DATE(2020,12,31))

     

    You may create calculated columns and a measure as below.

    Calculated column:
    Weeknum = WEEKNUM('Calendar'[Date])
    Startofweek = 
    CALCULATE(
        MAX('Calendar'[Date]),
        FILTER(
            ALL('Calendar'),
            'Calendar'[Date]<=EARLIER('Calendar'[Date])&&
            WEEKDAY('Calendar'[Date])=1
        )
    )
    
    Measure:
    Result = 
    var _result = 
    CALCULATE(
        AVERAGE('Table'[Value]),
        FILTER(
           ALL('Calendar'),
           'Calendar'[Date]>=MIN('Calendar'[Date])-7&&
           'Calendar'[Date]<MIN('Calendar'[Date])
        )
    )
    return
    IF(
        ISBLANK(_result),
        AVERAGE('Table'[Value]),
        _result
    )
    
    

     

    Result:

     

    Best Regards

    Allan

     

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

3 Replies

  • v-alq-msft's avatar
    v-alq-msft
    Community Support

    Hi, Anonymous 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

    Table:

     

    Calendar(a calculated table):

    Calendar = CALENDAR(DATE(2019,1,1),DATE(2020,12,31))

     

    You may create calculated columns and a measure as below.

    Calculated column:
    Weeknum = WEEKNUM('Calendar'[Date])
    Startofweek = 
    CALCULATE(
        MAX('Calendar'[Date]),
        FILTER(
            ALL('Calendar'),
            'Calendar'[Date]<=EARLIER('Calendar'[Date])&&
            WEEKDAY('Calendar'[Date])=1
        )
    )
    
    Measure:
    Result = 
    var _result = 
    CALCULATE(
        AVERAGE('Table'[Value]),
        FILTER(
           ALL('Calendar'),
           'Calendar'[Date]>=MIN('Calendar'[Date])-7&&
           'Calendar'[Date]<MIN('Calendar'[Date])
        )
    )
    return
    IF(
        ISBLANK(_result),
        AVERAGE('Table'[Value]),
        _result
    )
    
    

     

    Result:

     

    Best Regards

    Allan

     

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

  • v-alq-msft's avatar
    v-alq-msft
    Community Support

    Hi, Anonymous 

     

    If you take the answer of someone, please mark it as the solution to help the other members who have same problems find it more quickly. If not, let me know and I'll try to help you further. Thanks.

     

    Best Regards

    Allan

     

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