My version of comparison table is easily transformed for any setup. The logic of calculations is:
1. The pair of years to be compared are selected;
2. Years to be compared are calculated;
3. For both years, the January 1st, and its weekday number is calculated;
4. For both years, the date of 1st day of 1st week is calculated based in weekday of January 1st;
5. Following same logic as in p.3 and p.4. the dates of last day of last week are calculated (you calculate 1st day of week for next year and distract 1 from result).
6. All dates from 1st day of 1st week to last day of last week for both compared years are calculated. When date is bigger than last day of last week, an empty string is returned instead;
7. Calculate year/quarter/Month/week numbers for both columns. Easiest is with week numbers. You divide the value of (8 - datarange row number) with seven, and return integer part of result. 1st seven dates will get week number 1, next 7 dates week number 2, etc.. The year will be also simple - all dates in one date column belong to one year, all dates in another date column to another year. For quarter and month calculation, you must apply your own rules, like integer part of (14-week number) divided by 13 for quarter number (and when week number is bigger than 52, the quarter number will be 4). With months it is not so easy. The number of weeks may differ for different years (unless you left out some dates when this occurs). With ISO week numbers, a year has 52 or 53 weeks - with your system probably also. You reserve 8 weeks for 2 first months, and 5 weeks for last month. Remains 39 - 40 weeks for 9 months. Neither 39 nor 40 are not dividing exactly with 9 - the result is between 4 and 5. So you must have 4 months with 4 weeks, and additional 3 - 4 months with 5 weeks (the number depends on number of weeks in year) Or you use quarter numbers - when quarter has 13 weeks (a possible exception is 4th quarter), then in every quarter you must have at least one month with 5 weeks. This is most logical way, as a quarter is defined as 3-month period. I'm not even trying to imagine, how you'll compare two months, when one has 4 weeks, and another 5 weeks worth of dates (with my rules, this problem remains too - there is no way avoid this, except by leaving some dates out);
8. Anyway after that remains the last step - you calculate amounts and quantities for dates in compared years from your source table.
|