Defusing a Time Bomb - PowerPivot/SQL new year change

mrshl9898

Well-known Member
Joined
Feb 6, 2012
Messages
1,951
Hi all,

I'm building a report that needs to include housebills(consignments) and revenue last week, last week last year, last 4 weeks and last 4 weeks last year. These need to be Mon-Fri, Sat and Sun aren't a big concern which week they fall into.

Have managed a solution. But it's nowhere close to perfect.

Could only manage to pull in 465 days of data into PowerPivot to make sure I had it all. And group it by week.

SQL:
set transaction isolation level read uncommitted;

Select
CustomerCode,
CustomerName,
WeekNumber,
YearNumber,
COUNT(Housebillnumber) as Cons,
SUM(TotalInvoiced) as Revenue
from
(
    SELECT
    CustomerCode,
    CustomerName,
    DATEPART(wk, shipmentdate) AS WeekNumber,
    YEAR(shipmentdate) as YearNumber,
    Housebillnumber,
    TotalInvoiced
    FROM mytable
    WHERE ShipmentDate >= DATEADD(DAY, -465, GETDATE())
) AS sst

GROUP BY
CustomerCode,
CustomerName,
WeekNumber,
YearNumber

Then added the following into PowerPivot to get the results:

RevenueLastWeek =IF(Data[YearWeek]=Data[ThisWeekNumber]-1,Data[Revenue],0)
RevenueLast4Weeks =IF(AND(Data[YearWeek]>=Data[ThisWeekNumber]-4,Data[YearWeek]<=Data[ThisWeekNumber]-1),Data[Revenue],0)
RevenueLastWeekLastYear =IF(Data[YearWeek]=Data[ThisWeekNumber]-101,Data[Revenue],0)
RevenueLast4WeeksLastYear =IF(AND(Data[YearWeek]>=Data[ThisWeekNumber]-104,Data[YearWeek]<=Data[ThisWeekNumber]-101),Data[Revenue],0)

The biggest challenge here is in week 202101 this will be useless for a few weeks.

Can anyone suggest a better method?

Many thanks as always,
mrshl
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,784
Messages
6,121,539
Members
449,038
Latest member
Guest1337

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top