Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

WTD for same week last year

Hello everyone, I am currently struggling with this for hours and the other posts in the community did not really help.   I have to calculate the WTD running Total for last years week (Monday-Sunda...
  • lbendlin's avatar
    5 years ago

    None of your formulas use the week number. It will be much simpler if you took the week number, and then just sum up all the days in that week until the current day of the week - for both years.

  • Anonymous's avatar
    Anonymous
    5 years ago
    // To do this properly, you have to
    // create some columns in your Dates table:
    // YearOfWeek - an int, the year the week (to which
    //              the day belongs to)
    // YearWeekID   - an int, the id (consecutive from 1..52)
    //                of the week in the year
    // DayNumberInWeek - an int, the day (1,2,...,7) in
    //                   the week
    // Once you have these, you can write:
    
    [Measure WTD LY] =
    var __currentYear = SELECTEDVALUE( Dates[YearOfWeek] )
    var __currentWeek = SELECTEDVALUE( Dates[YearWeekID] )
    var __lastDayNumberInWeek = MAX( Dates[DayNumberInWeek] )
    var __lastYear = __currentYear - 1
    var __prevWeek = __currentWeek
    var __output =
        CALCULATE(
            [Sales],
            Dates[YearOfWeek] = __lastYear,
            Dates[YearWeekID] = __prevWeek,
            Dates[DayNumberInWeek] <= __lastDayNumberInWeek,
            ALL( Dates )
        )
    return
        __output

    Bear in mind that you'll have to give a special treatment to the first week in the second year or/and the last week in the last year since the first week in the first year in your Dates table might not have all days and the last week in your last year may not have all days. These are the edge cases that you have to tackle reasonably.