Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Using Sequential Week Numbers to create Last Week and Previous Week Measures

I have created a Calculated Column for Sequential Week Number using the following DAX:     Week - Sequential Number = IF('Calendar'[Week - Year]=2019, 'Calendar'[Week - Number], 'Calendar'[Week -...
  • PaulDBrown's avatar
    6 years ago

    Anonymous 

    I'm not quite following you. 

    What is the expected result? (can you show a sample table with the data you are expecting?)

    From your screenshot, you seem to missing a YearWeek column (to be able to do calculations pertaining to years). To add a YearWeek column use:

    YearWeek = YEAR(Calendar Table[Date]) * 100 + WEEKNUM(Calendar Table[Date])

    With this, you can do calculations involving current week where current week is (use in measure):

    Current YearWeek = YEAR(TODAY()) *100 + WEEKNUM (TODAY())

    You can then create a YearWeek Index in a calculated column to use in calculations:

    YearWeek Index = RANX(Calendar, Calendar [YearWeek],,ASC, Dense)

    (I prefer to do the rank DESC since you can then establish that "Todays" week is 1, last week is 2...)

    Check out this thread to see how it works in practice to calculate the previous 8 weeks amounts based on a slicer selection:
    https://community.powerbi.com/t5/Desktop/Last-8-Weeks-Sales-spanning-previous-year/m-p/951682#M456017 

  • Anonymous's avatar
    Anonymous
    6 years ago

    I solved it using:

    Week - Current Seq = 
    VAR MaxDate = MAX(Quotes[Quote - Creation Date])
    VAR WeekNum = IF(WEEKNUM(MaxDate,1) = 53, 1, WEEKNUM(MaxDate,1))
    
    RETURN
    IF(
    	IF(WeekNum = 1 && MONTH(MaxDate) = 12, YEAR(MaxDate)+1, YEAR(MaxDate))=2019,
     	WeekNum, 
    	WeekNum+52
    )

    Thanks again Paul, you definitely got my brain on the right path!