Adjusting Week Numbering in Custom Time Hierarchies for Year-End Continuity

I created a report using the ‘5-4-4’ code from the ‘Custom time hierarchies’ feature, and it has been quite useful. However, I encountered a few issues:

The week numbering for December 31, 2024, and January 1, 2025, is the same, which is problematic. I would like to have December 31, 2024, as the last week(52rd week), and January 1, 2025, start as Week 1. Specifically, I aim for the following structure:

  • December 31, 2024: Week 52
  • January 1, 2025: Week 1
  • December 31, 2025: Week 52
  • January 1, 2026: Week 1

Could you please advise on how I can achieve this specific week numbering in my reports?

1 Like

I am unable to edit the post.

  • I want the last day of each year to display as 52, 53, or more, depending on the case.
  • example)
    • 2024-12-29: 53W
    • 2024-12-30: 53W
    • 2024-12-31: 53W
    • 2025-01-01: 1W
    • 2025-01-02: 1W
    • 2025-01-05: 2W

Hi @NextLife

Please try the following JavaScript code for the 5-4-4 hierarchy with W1 always starting in Jan 1:

var weekStartDate = new Date(timeValue);
// Week starts on Sunday.
weekStartDate.setDate(weekStartDate.getDate() - weekStartDate.getDay());
var year = timeValue.getFullYear();
var timezoneoffset = weekStartDate.getTimezoneOffset();
var nextYearStartDate = new Date(year + 1, 0, 1, 0, - timezoneoffset);
var daysTillNextYear = (nextYearStartDate.getTime() - weekStartDate.getTime()) / 86400000.0;

// 1st January is in the first week.
// The last days of December that falls into the same week when 1st January are counted for the the first week of the next year.
year = daysTillNextYear < 1 ? year + 1 : year;
var yearStartDate = new Date(year, 0, 1);

// 1st January should be in the first week.
// Weeks starts on Sunday.
yearStartDate.setDate(yearStartDate.getDate() - yearStartDate.getDay());
var dayInWeek = Math.ceil((timeValue.getTime() - weekStartDate.getTime()) / 86400000.0) + 1;
var days = Math.ceil((weekStartDate.getTime() - yearStartDate.getTime()) / 86400000.0);
var week = Math.floor(days / 7) + 1;

// 13 weeks in quarter with monthly split 5 4 4.
var quarter = Math.ceil(week / 13);
if (quarter == 5) {
  quarter = 4;
  var month = 12
} else {
  var weekInQuarter = (week % 13) == 0 ? 13 : (week % 13);
  var monthInQuarter = weekInQuarter == 1 ? 1 : Math.ceil((weekInQuarter - 1) / 4);
  month = (quarter - 1) * 3 + monthInQuarter
}

const monthName = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

return {
  year: year,
  year_name: "Y " + year,
  quarter: quarter,
  quarter_name: "Q" + quarter + " " + year,
  month: month,
  month_name: monthName[month - 1] + " " + year,
  week: week,
  week_name: "W" + week,
  day: dayInWeek,
  day_name: strftime("%b %d %Y", timeValue)
}

Let me know if this works as expected!
​Best regards,
​Nauris