+ Start a Discussion
pbaezapbaeza 

ISO Week Calculation

In Sweden and some other parts of Europe the week numbering is often using the ISO 8601 standard that says that the first week of the year is the first week with at least 4 days in the new year. Some consequences of this is that the final dates of one year can be week 52, 53 or even week 1( in the following year). The same goes for the first dates of a year.

 

To be able to do weekly comparison report Year-to-Year with this weeknumbering it is necessary to create custom fields for the opportunity that calculates the Year and Week number for a given close date. Here is one solution using three custom formula fields.

 

Field: Global Sales Report Weekday

Description:  Day 1 = Sunday, 2 = Monday....., 7=Saturday

Formula:  MOD( CloseDate  - DATE(1900, 1, 7), 7)+1

 

Field: Global Sales Report Week Year

Formula: YEAR(CloseDate + (MOD(8- Global_Sales_Report_Weekday__c ,7)-3))

 

Field: Global Sales Report Week

Formula:

FLOOR((CloseDate - DATE( Global_Sales_Report_Week_Year__c ,1,1) +
MOD(
(MOD( DATE(Global_Sales_Report_Week_Year__c,1,1) - DATE(1900, 1, 7), 7)+1)
+1,7)-3) / 7 + 1)

 

Hope this can be of use for anyone else

 

Peter Baeza

InfoAction AB, Sweden

 

forceDotMomforceDotMom

This is pretty interesting.  I found it to work mostly, but I am a little confused.  When I have a date :  12/31/2008, the Global Report Year says 2009, and Global Report Sales Week says week 1.  Is that correct?

Peter.BaezaPeter.Baeza

Yes that is correct. the jan 1 2009 is a thursday so mon 29 to wed 31 2008 is in week 1 2009

/ Peter

eliotstock5eliotstock5

The above formula field for week number as Apex code:

 

public static Integer isoWeekNumber(Date value) {
		Integer daysSince1900_01_07 = Date.newInstance(1900, 1, 7).daysBetween(value);
		Integer dayNumber = Math.mod(daysSince1900_01_07, 7) + 1;
		
		Date dateForYear = value.addDays(Math.mod(8 - dayNumber, 7) - 3);
		Integer year = dateForYear.year();
		Date year_01_01 = Date.newInstance(year, 1, 1);
		
		Integer week = (Integer)Math.floor((year_01_01.daysBetween(value)
				+ Math.mod((Math.mod(Date.newInstance(1900, 1, 7).daysBetween(year_01_01), 7) + 1) + 1, 7) - 3) / 7 + 1);
				
		return week;
	}

 and some tests:

 

	@isTest
	public static void knownIsoWeekNumbersWorkAsExpected() {
		Date d;
		
		d = Date.newInstance(2012, 12, 30); // Sun
		System.assertEquals(52, isoWeekNumber(d));
		
		d = Date.newInstance(2012, 12, 31); // Mon
		System.assertEquals(1, isoWeekNumber(d));
		
		d = Date.newInstance(2013, 1, 1); // Tue
		System.assertEquals(1, isoWeekNumber(d));
		
		d = Date.newInstance(2013, 1, 2); // Wed
		System.assertEquals(1, isoWeekNumber(d));
		
		d = Date.newInstance(2013, 1, 3); // Thu
		System.assertEquals(1, isoWeekNumber(d));
		
		d = Date.newInstance(2013, 1, 4); // Fri
		System.assertEquals(1, isoWeekNumber(d));
		
		d = Date.newInstance(2013, 1, 5); // Sat
		System.assertEquals(1, isoWeekNumber(d));
		
		d = Date.newInstance(2013, 1, 6); // Sun
		System.assertEquals(1, isoWeekNumber(d));
		
		d = Date.newInstance(2013, 1, 7); // Mon
		System.assertEquals(2, isoWeekNumber(d));
		
		d = Date.newInstance(2013, 1, 8); // Tue
		System.assertEquals(2, isoWeekNumber(d));
	}

 Please post here if you spot anything wrong with this for other years. Thanks.

 

 

 

 

rklaassenrklaassen

Thanks eliotstock5, your method works like a charm!

Ken BKen B
@Peter.Baeza this is exactly what I was looking for. Thank you!

Ken BKen B
Is it possible to create a rolling 3 month report showing next 3 months compared to last year's same week numbers sales? I can't get any combination of report filters ot make it happen without showing every week in between.

Or is it possible to create a Viseualforce page that acocmplishes this?
Coral RacingCoral Racing
Hi What if my week began on a Friday, how would I adapt to suit this?
Thanks

Sonya
Luka CordasicLuka Cordasic
@eliotstock5
thank you!
Paul da SilvaPaul da Silva
Thanks a million @pbaeza it worked treat for me.  Much appreciated.
Daniel Khan 21Daniel Khan 21
How do I set up this APEX Code? Is it a Class / Trigger /etc. ? Thanks for any help!