Skip to main content
Announcements
Global Transformation Awards! Applications are now open. Submit Entry
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Counting working days between dates

Hi all,

Table A is my fact table with orders:

OrderOrderDateShipmentDate
A2013-02-072013-02-12
B2013-12-302014-01-03

Table B is my date table:

DateWorkingDay
2013-02-081
2013-02-090
2013-02-100
2013-02-111
2013-02-121
..........
2013-12-310
2014-01-010
2014-01-021
2014-01-031

I would like to count the shipment working days. The correct number of working days should be 3 for order A... and 2 for order B as New years eve and New years day is not working days.

How can I count this? I've tried the NewWorkDays() function, but it doesn't handle our holidays. I've also tried the IntervalMatch() function without proceeding.

Please help me.

Best regards,

Filip

1 Solution

Accepted Solutions
Not applicable
Author

Filip,

you may try script like this:

tbl1:

LOAD Autonumber(Num(OrderDate) & '|' & Num(ShipmentDate)) as IntervalID,*, ShipmentDate-OrderDate as initialDNr INLINE [

Order, OrderDate, ShipmentDate

A,2013-02-07,2013-02-12

B,2013-12-30,2014-01-03

];

wd:

LOAD * INLINE [

Date,Wd

2013-02-08,1

2013-02-09,0

2013-02-10,0

2013-02-11,1

2013-02-12,1

2013-12-31,0

2014-01-01,0

2014-01-02,1

2014-01-03,1

];

wd_tmp:

load Date as NonWorkDate Resident wd where Wd=0;

bridge:

IntervalMatch (NonWorkDate) Load distinct OrderDate, ShipmentDate resident tbl1;

Join (wd_tmp)

Load NonWorkDate, Autonumber(Num(OrderDate) & '|' & Num(ShipmentDate)) as IntervalID

Resident bridge;

join (tbl1)

load IntervalID, count(NonWorkDate) as nwd_nbr Resident wd_tmp Group by IntervalID;

table:

load Order, OrderDate, ShipmentDate, initialDNr-nwd_nbr as wd_nbr Resident tbl1;

DROP Tables  bridge,wd_tmp,tbl1;

View solution in original post

7 Replies
tresesco
MVP
MVP

Networkdays() has a third parameter which accepts your list of holidays. Have you tried that?

Not applicable
Author

You may do IntervalMatch or your own intersection table using while clause in load script.

Anonymous
Not applicable
Author

OK, how do I do that?

The best of all would be if I could get the sum of working days as a new column in table A.

Not applicable
Author

Filip,

you may try script like this:

tbl1:

LOAD Autonumber(Num(OrderDate) & '|' & Num(ShipmentDate)) as IntervalID,*, ShipmentDate-OrderDate as initialDNr INLINE [

Order, OrderDate, ShipmentDate

A,2013-02-07,2013-02-12

B,2013-12-30,2014-01-03

];

wd:

LOAD * INLINE [

Date,Wd

2013-02-08,1

2013-02-09,0

2013-02-10,0

2013-02-11,1

2013-02-12,1

2013-12-31,0

2014-01-01,0

2014-01-02,1

2014-01-03,1

];

wd_tmp:

load Date as NonWorkDate Resident wd where Wd=0;

bridge:

IntervalMatch (NonWorkDate) Load distinct OrderDate, ShipmentDate resident tbl1;

Join (wd_tmp)

Load NonWorkDate, Autonumber(Num(OrderDate) & '|' & Num(ShipmentDate)) as IntervalID

Resident bridge;

join (tbl1)

load IntervalID, count(NonWorkDate) as nwd_nbr Resident wd_tmp Group by IntervalID;

table:

load Order, OrderDate, ShipmentDate, initialDNr-nwd_nbr as wd_nbr Resident tbl1;

DROP Tables  bridge,wd_tmp,tbl1;

Not applicable
Author

Hi, Filip!

You can use this code:

Orders:

LOAD * INLINE [

  Order, OrderDate, ShipmentDate

  A, 2013-02-07, 2013-02-12

  B, 2013-12-30, 2014-01-03

];

WorkDays:

LOAD * INLINE [

  Date, WorkingDay

  2013-02-08, 1

  2013-02-09, 0

  2013-02-10, 0

  2013-02-11, 1

  2013-02-12, 1

  2013-12-31, 0

  2014-01-01, 0

  2014-01-02, 1

  2014-01-03, 1

];

temp:

IntervalMatch(Date) LOAD OrderDate,ShipmentDate Resident Orders;

Left Join(temp)

LOAD * Resident Orders;

Left Join(temp)

LOAD * Resident WorkDays;

DROP Tables Orders,WorkDays;

orders_work_days:

LOAD Order, OrderDate, ShipmentDate, Sum(WorkingDay) as WorkongDays

Resident temp

Group By Order, OrderDate, ShipmentDate;

DROP Table temp;

Anonymous
Not applicable
Author

Thank you very much, this works great!

Can you please explain the stages with the bridge, that I didn't understand (though it worked)...

Anonymous
Not applicable
Author

Thanks for your post, this works well if you only have order headers... My example was a bit simplified, when assigning this to my real data with order rows, I got duplicate values on each row using the sum() function. Dariusz example above works perfect with rows as well!