Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
10 years ago
Solved

Drill down - Weeks

Is it possible to drill down to weeks? I see the option for years, months, and days. Thanks! 
  • Anonymous's avatar
    Anonymous
    10 years ago

    If you want to do the first day of the week* these are your options:

     

    Query: Week = Date.StartOfWeek([Date])

     

    DAX: Week = DateTable[Date] + 1 - WEEKDAY(DateTable[Date])

     

    Just for fun, here's a bonus trick:

     

    Problem: On a bunch of my reports I have the week as a slicer, and I always want it to default to having the current week selected. The user can move from there. If you just use the regular Week columns I've described and you select the current week, save and publish, everything will appear to be correct for the first week after you publish. But then that becomes last week, then the week before. If you're showing only current and future weeks it will end up with no default selection in the slicer which may give nonsense results on all the visuals. Or if you're still showing past weeks the default just drifts further and further into the past and becomes less useful.

     

    Solution: In the query editor, add a custom column called Week.

    Week = if Date.IsInCurrentWeek([Date]) 
    then "This Week"
    else Text.From(Date.EndOfWeek([Date]))

    This results in the same thing as the simpler version of Week, but because it's in a text data type you'll get the same dates for most weeks as before, but the current week will literally say "This Week". When you use that in a slicer you can select "This Week" and it will behave exactly the same as if it read for instance "6/4/2016". But since "This Week" is a unique text value and that text value automatically moves forward on refresh to cover a new set of 7 days every Sunday, your default slicer selection will always correspond to the current calendar week so your users don't have to think about it.

     

     

    * In all these examples I'm assuming a regular Sunday through Saturday week. If your business uses a different week structure like Monday through Sunday, there are ways to adapt these formulas. I leave that as an exercise to the reader.