Forum Discussion
Week commencing in DAX
- 8 years ago
Hey,
just try this "simple" DAX statement
SoWDate = 'Calendar'[Date] - WEEKDAY('Calendar'[Date],2) +1The second parameter of the WEEKDAY()-function indicates if Sunday or Monday is your first day of the week, for me this works like a charm, maybe you have to use a different correction part.
Just using your Date column and the "Day of Week" column helps to adjust the above mentioned formula if necessary.
and this calculates the End Date of the weekEoWDate = 'Calendar'[Date] + 7 - WEEKDAY([DATE],2)
Hope this helps
Regards
This is great. Could someone please explain the logic behind this.
Hey,
thanks for your kind words!
The thinking (not sure if Mr Spock would call this logic) behind this is as follows:
- A week spans 7 days
- Each day belongs to a single week
- If my week starts on Monday Weekday("2018-07-04",2) returns 3, a Wednesday is the 3rd day of a week
- The date of the starting week is calculating like this: Subtracting 3 Adding 1 from "2018-07-04" returns the date of the Monday closest to the date in question "2018-07-04"
- A similar logic is used to calculate the date of the next Sunday
Hopefully this explains the reasoning behind both DAX statements a little better.
Regards
Tom
- KenSkinner7 years agoHelper I
Brilliant! Spock would be proud. Straight forward logic and you can change the number for subtraction and addition by multiples of 7 to get the starting/ending dates for any number of weeks prior or future. Thanks!