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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
ddave84
Frequent Visitor

Rolling Avg for Measured Value 7 and 14 Day

Hello,  

 

I have some shipping data and I'm trying to roll up the 7 and 14 day rolling avg for a measured value.  I have created a separate calendar table.  Also, I have created many one to many relationship for the calendar table to the shipping order date.  To get my cycle times I take the Avg of each week then get the ratio based off those values.  I have reviewed the previous threads and tried their solutions, but I keep getting either null or invalid data.  I have tried to attach the .pbix but for some reason it will not let me.  Thank you 

Shipping_exam.jpg

Shipping_exam_Real.jpg

1 ACCEPTED SOLUTION

I made some improvements to your calendar table : 

CalTable = 

ADDCOLUMNS (
    CALENDAR (DATE(2020,1,1), DATE(2025,12,31)), -- Adjust these dates to your needs
    "Year", YEAR([Date]),
    "Month", FORMAT([Date], "MMMM"),
    "MonthNo", MONTH([Date]),
    "DOW", WEEKDAY([Date]), 
    "Quarter", "Q" & QUARTER([Date]),
    "QuarterNumber", QUARTER([Date]),
    "WorkWeek", 
    VAR CorrectYear = 
        IF(
            MONTH([Date]) = 12 && WEEKNUM([Date],2) = 1,
            YEAR([Date]) + 1,
            IF(
                MONTH([Date]) = 1 && WEEKNUM([Date],2) > 50,
                YEAR([Date]) - 1,
                YEAR([Date])
            )
        )
    VAR IsoWeek = 
        IF(
            MONTH([Date]) = 12 && WEEKNUM([Date],2) = 1,
            1,
            IF(
                MONTH([Date]) = 1 && WEEKNUM([Date],2) > 50,
                WEEKNUM(DATE(YEAR([Date])-1, 12, 31),2),
                WEEKNUM([Date],2)
            )
        )
    RETURN
        FORMAT(CorrectYear, "0000") & FORMAT(IsoWeek, "00")
)

the relationship was between a datetime and date column (Shipping and CalTable)

so I created this column in the fact table :

OrderDateFormatted = DATEVALUE(Shipping[Order Date])

and made the join like below : 

AmiraBedh_0-1696937803689.png

 

Also for the Workweek the column was displaying values like 202027 (here the 27th week is concatenated to the year 2020) but in some cases you had 20201 which is in reality 2020 concatenated to the 1st week so I made the adjusement for the weeks between [1-9] to include the zero.

Now for the measures : 

Shipping Order Ratio = 
DIVIDE(
    AVERAGE('Shipping'[Ordery Cycle Time_Hrs]),
    AVERAGE('Shipping'[Fullfilment Cycle Time_Hrs])
)
____________________________
7 Day Rolling Avg = 
VAR CurrentDate = MAX('CalTable'[Date])

RETURN
IF(
    COUNTROWS(FILTER('Shipping', 'Shipping'[OrderDateFormatted] <= CurrentDate)) > 0,
    AVERAGEX(
        FILTER(
            ALL('Shipping'),
            COUNTROWS(
                FILTER(
                    'CalTable',
                    'CalTable'[Date] <= CurrentDate && 
                    'CalTable'[Date] > CurrentDate - 7
                )
            ) > 0
        ),
        [Shipping Order Ratio]
    ),
    BLANK()
)
_____________________________________________
14-day Rolling Avg = 
AVERAGEX(
    DATESINPERIOD(
        'CalTable'[Date],
        LASTDATE('CalTable'[Date]),
        -14,
        DAY
    ),
    [Shipping Order Ratio]
)

Find attached the new version of your pbix


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

7 REPLIES 7
AmiraBedh
Super User
Super User

Can you please share your pbix file or your dataset it is better than sharing an image?


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

I made some improvements to your calendar table : 

CalTable = 

ADDCOLUMNS (
    CALENDAR (DATE(2020,1,1), DATE(2025,12,31)), -- Adjust these dates to your needs
    "Year", YEAR([Date]),
    "Month", FORMAT([Date], "MMMM"),
    "MonthNo", MONTH([Date]),
    "DOW", WEEKDAY([Date]), 
    "Quarter", "Q" & QUARTER([Date]),
    "QuarterNumber", QUARTER([Date]),
    "WorkWeek", 
    VAR CorrectYear = 
        IF(
            MONTH([Date]) = 12 && WEEKNUM([Date],2) = 1,
            YEAR([Date]) + 1,
            IF(
                MONTH([Date]) = 1 && WEEKNUM([Date],2) > 50,
                YEAR([Date]) - 1,
                YEAR([Date])
            )
        )
    VAR IsoWeek = 
        IF(
            MONTH([Date]) = 12 && WEEKNUM([Date],2) = 1,
            1,
            IF(
                MONTH([Date]) = 1 && WEEKNUM([Date],2) > 50,
                WEEKNUM(DATE(YEAR([Date])-1, 12, 31),2),
                WEEKNUM([Date],2)
            )
        )
    RETURN
        FORMAT(CorrectYear, "0000") & FORMAT(IsoWeek, "00")
)

the relationship was between a datetime and date column (Shipping and CalTable)

so I created this column in the fact table :

OrderDateFormatted = DATEVALUE(Shipping[Order Date])

and made the join like below : 

AmiraBedh_0-1696937803689.png

 

Also for the Workweek the column was displaying values like 202027 (here the 27th week is concatenated to the year 2020) but in some cases you had 20201 which is in reality 2020 concatenated to the 1st week so I made the adjusement for the weeks between [1-9] to include the zero.

Now for the measures : 

Shipping Order Ratio = 
DIVIDE(
    AVERAGE('Shipping'[Ordery Cycle Time_Hrs]),
    AVERAGE('Shipping'[Fullfilment Cycle Time_Hrs])
)
____________________________
7 Day Rolling Avg = 
VAR CurrentDate = MAX('CalTable'[Date])

RETURN
IF(
    COUNTROWS(FILTER('Shipping', 'Shipping'[OrderDateFormatted] <= CurrentDate)) > 0,
    AVERAGEX(
        FILTER(
            ALL('Shipping'),
            COUNTROWS(
                FILTER(
                    'CalTable',
                    'CalTable'[Date] <= CurrentDate && 
                    'CalTable'[Date] > CurrentDate - 7
                )
            ) > 0
        ),
        [Shipping Order Ratio]
    ),
    BLANK()
)
_____________________________________________
14-day Rolling Avg = 
AVERAGEX(
    DATESINPERIOD(
        'CalTable'[Date],
        LASTDATE('CalTable'[Date]),
        -14,
        DAY
    ),
    [Shipping Order Ratio]
)

Find attached the new version of your pbix


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

I just verified the CalTable and I noticed the workweek are off by N+1.  Would this make any difference?  The reason I ask is that I'm experimenting with calculating the previous 1 and 4 weeks of data.   

ddave84_0-1696967482404.png

 

Can you please define the expected output for your dim calendar ?


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Thank you for the help and guidance.  

welcome 🙂


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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