-
Notifications
You must be signed in to change notification settings - Fork 19
/
get-WorkingMDBetweenDatesAndByWorkingHours.pq
109 lines (109 loc) · 5.07 KB
/
get-WorkingMDBetweenDatesAndByWorkingHours.pq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
let
output =
(sDT as datetime, eDT as datetime, sH as time, eH as time) as number =>
let
startDateTime = sDT,
startDate = Date.From(startDateTime),
startTime = Time.From(startDateTime),
startHour = Time.Hour(startTime),
startWorkHour = sH,
endDateTime = eDT,
endDate = Date.From(endDateTime),
endTime = Time.From(endDateTime),
endHour = Time.Hour(endTime),
endWorkHour = eH,
maxHoursPerDay = endWorkHour - startWorkHour,
realStartTime =
if startTime < startWorkHour then
startWorkHour
else if startTime > endWorkHour then
endWorkHour
else
startTime,
realEndTime =
if endTime < startWorkHour then
startWorkHour
else if endTime > endWorkHour then
endWorkHour
else
endTime,
diversionOfTime = realEndTime - realStartTime,
output =
if startDate = endDate then
if diversionOfTime > maxHoursPerDay then
maxHoursPerDay
else
diversionOfTime
else
let
dt =
Table.Buffer(
Table.SelectRows(
#table(
type table [
Date = date,
Weekend = logical
],
List.Transform(
List.Dates(
startDate,
(
Number.From(endDate)
- Number.From(startDate)
)
+ 1,
#duration(1, 0, 0, 0)
),
each
{
_,
if
Date.DayOfWeek(_, Day.Monday)
+ 1
= 6
or Date.DayOfWeek(_, Day.Monday)
+ 1
= 7
then
true
else
false
}
)
),
each [Weekend] = false
)
),
addon =
Table.AddColumn(
dt,
"Addon",
each
if [Date] = startDate then
endWorkHour - realStartTime
else if [Date] = endDate then
endWorkHour - realEndTime
else
maxHoursPerDay,
Int64.Type
),
summarization = List.Sum(addon[Addon])
in
summarization
in
output,
documentation = [
Documentation.Name = " get-WorkingMDBetweenDatesAndByWorkingHours.pq ",
Documentation.Description = "Returning Time between StartDateTime and EndDateTime by Working hours and working days (without weekends). ",
Documentation.Source = "https://www.jaknapowerbi.cz . ",
Documentation.Version = " 1.0 ",
Documentation.Author = " Štěpán Rešl "
]
in
Value.ReplaceType(
output,
Value.ReplaceMetadata(
Value.Type(output),
documentation
)
)