Forum Discussion

PowerMarra's avatar
PowerMarra
Regular Visitor
5 years ago
Solved

Calculating rolling 52 weeks

Hi,    I try to find a rolling 52 weeks measure. I want to calculate value sales for the previous last 52 weeks (value sales from todays date to the same date last year).  I also want to calculate ...
  • v-luwang-msft's avatar
    5 years ago

    Hi PowerMarra ,

    If only  rolling 52 weeks measure,try the following steps:

    base data:

    Step1, create new column:

    weekStart = 
    var dayOfWeek = WEEKDAY('Table'[Date])
    return
    'Table'[Date] - dayOfWeek +1

    Step2,use the below dax to create weekendnum:

    runningMonthIndex = 
    var MinWeekStart = min('Table'[weekStart]) 
    var weekNumber = roundup((DATEDIFF(MinWeekStart , [Date] , DAY)+1) / 7,0)
    return 
    weekNumber

    Step 3,use the below measure to get the sales 52 weeks ago:

    test =
    CALCULATE (
        SUM ( 'Table'[sale] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[runningMonthIndex]
                >= MAX ( 'Table'[runningMonthIndex] ) - 52
                && 'Table'[runningMonthIndex] <= MAX ( 'Table'[runningMonthIndex] )
        )
    )

    Final get:

     

    About this solutions,you could refer: https://community.powerbi.com/t5/Desktop/Rolling-52-week-sales-and-preceding-52-week-sales/m-p/720148 

    And if you want to roll by years,try the measurte:

    test1 = 
    CALCULATE (
        SUM ( 'Table'[sale] ),
         DATESBETWEEN('Table'[Date],
            DATE(YEAR(MAX('Table'[Date]))-1,MONTH(MAX('Table'[Date])),DAY(MAX('Table'[Date])))  ,MAX('Table'[Date]))
               
        )
    

     

    I tested your dax, but the data did not change in the new year:

     

    Wish it is helpful for you!

     

    Best Regards

    Lucien