Tableau:时差,不包括周末,营业时间和节假日

时间:2020-08-18 20:45:56

标签: sql date tableau-api

我正在Tableau工作。我有两个时间戳,我想找出两者之间的DD:HH:MM:SS差异,但不包括周末,营业时间,和假期。我找到了许多解决方案,可以解决周末和工作时间以外的问题,但到目前为止,还没有解决假期的问题。这可能吗?如果没有,我是否需要使用某种假日查询表在自定义SQL中进行计算?

2 个答案:

答案 0 :(得分:1)

是的,您需要类似的东西:

  • 每年的假期表
  • 营业时间表或硬编码
  • 您需要担心假期结束或其他非工作时间,并在减去两者之间的所有非工作时间之前,将参数向内调整为实际时间。
  • 您还需要注意或避免差异为负(时间顺序错误)。
  • 接下来要担心起点和终点之间的DST差异,也许是另一种算法,或者如果您的时代包括DST规律的变化,请使用表格。

答案 1 :(得分:0)

我对自己的问题有一个答案。它确实需要使用假期表或用于定义假期的某种日历表。可以是Excel,SQL等。您可能需要修改以下连接条件以适合表的具体情况。

我使用的示例自定义SQL查询。 DESCRIPTION是指假期的名称(“圣诞节”,“圣诞节前夕”等)

custom_sql1

SELECT  DISTINCT CALENDAR_YEAR,
        DESCRIPTION AS DESCRIPTION_ASSIGNED,
        HOLIDAY_DATE AS HOLIDAY_DATE_ASSIGNED
FROM YOUR_HOLIDAY_TABLE

custom_sql2

SELECT  DISTINCT CALENDAR_YEAR,
        DESCRIPTION AS DESCRIPTION_CREATED,
        HOLIDAY_DATE AS HOLIDAY_DATE_CREATED
FROM YOUR_HOLIDAY_TABLE

一旦有了,就可以开始。

此示例有两个日期:创建案例的时间(created_ts)和分配案例的时间(assigned_ts)。

(1)表格联接

将您的假期/日历表两次添加到主表中,每个时间戳一次。在下面的示例中,custom_sql_query是我的主表,custom_sql_query1custom_sql_query2是我的假期表。

Screenshot illustrating table joins

custom_sql_query1

主表的自定义联接条件:

注意:这假设您的工作日结束于8:00 pm(20:00)。相应地调整“ 19”。

IF      DATENAME('weekday',[assigned_ts]) = 'Friday'
        AND DATEPART('hour',[assigned_ts]) > 19
    THEN DATETRUNC('day',DATEADD('day',3,[assigned_ts]))

ELSEIF       DATENAME('weekday',[assigned_ts]) = 'Saturday'
    THEN DATETRUNC('day',DATEADD('day',2,[assigned_ts]))

ELSEIF       DATENAME('weekday',[assigned_ts]) = 'Sunday'
    THEN DATETRUNC('day',DATEADD('day',1,[assigned_ts]))

ELSEIF   DATEPART('hour',[assigned_ts]) > 19
    THEN DATETRUNC('day',DATEADD('day',1,[assigned_ts]))

ELSE  DATETRUNC('day',[assigned_ts])
END

假期表的自定义加入条件:DATETRUNC('day',[HOLIDAY_DATE_ASSIGNED])


custom_sql_query2

主表的自定义联接条件:

注意:这假设您的工作日结束于8:00 pm(20:00)。相应地调整“ 19”。

IF      DATENAME('weekday',[created_ts]) = 'Friday'
        AND DATEPART('hour',[created_ts]) > 19
    THEN DATETRUNC('day',DATEADD('day',3,[created_ts]))

ELSEIF       DATENAME('weekday',[created_ts]) = 'Saturday'
    THEN DATETRUNC('day',DATEADD('day',2,[created_ts]))

ELSEIF       DATENAME('weekday',[created_ts]) = 'Sunday'
    THEN DATETRUNC('day',DATEADD('day',1,[created_ts]))

ELSEIF   DATEPART('hour',[created_ts]) > 19
    THEN DATETRUNC('day',DATEADD('day',1,[created_ts]))

ELSE  DATETRUNC('day',[created_ts])
END

假期表的自定义加入条件:DATETRUNC('day',[HOLIDAY_DATE_CREATED])

(2)创建参数

创建用于定义工作日开始和结束的日期和分钟的参数。示例Day Open HourDay Open Minute参数如下。 Day Close HourDay Close Minute的创建方式完全相同,只是您将“当前值”选项更改为与工作日结束时间/分钟相对应的值:

[Day Open Hour]
Name: Day Open Hour
Data Type: Integer
Current Value: 8 (assumes workday starts at 8:00am)
Display format: Automatic
Allowable Values: Range
Range of values: Min = 0, Max = 23, Step = 1

[Day Open Minute]
Name: Day Open Minute
Data type: String
Current value: 00
Allowable values: List
List of values:
Value - Display As
0       00
30      30

(change minute increments as needed)

(3)输入您的时间戳记:第二天

创建计算字段

如果在工作时间以外分配/创建了某些内容,则此操作将舍入到第二天的工作开始。请注意,我们尚未排除周末或假期。

assigned_ts: next day

IF (DATEPART('hour', [assigned_ts]) * 60) + DATEPART('minute', [assigned_ts])  
        > (60 * ([Day Close Hour]) ) + INT([Day Close Minute])
THEN
// created after End of Day so round to start of next business day
DATEADD('minute', (60 * (24 + [Day Open Hour])) + INT([Day Open Minute]), 
        DATETRUNC('day',[assigned_ts])) 
ELSEIF
(DATEPART('hour', [assigned_ts]) * 60) + DATEPART('minute', [assigned_ts])  
        < (60 * [Day Open Hour]) + INT([Day Open Minute])
THEN
// created before Start of Day so round to start of business day
DATEADD('minute', (60 * [Day Open Hour]) + INT([Day Open Minute])
        , DATETRUNC('day',[assigned_ts])) 
ELSE
[assigned_ts]
END

created_ts: next day

IF (DATEPART('hour', [created_ts]) * 60) + DATEPART('minute', [created_ts])  
        > (60 * ([Day Close Hour]) ) + INT([Day Close Minute])
THEN
// created after End of Day so round to start of next business day
DATEADD('minute', (60 * (24 + [Day Open Hour])) + INT([Day Open Minute]), 
        DATETRUNC('day',[created_ts])) 
ELSEIF
(DATEPART('hour', [created_ts]) * 60) + DATEPART('minute', [created_ts])  
        < (60 * [Day Open Hour]) + INT([Day Open Minute])
THEN
// created before Start of Day so round to start of business day
DATEADD('minute', (60 * [Day Open Hour]) + INT([Day Open Minute]), DATETRUNC('day',[created_ts])) 
ELSE
[created_ts]
END

(4)更改时间戳记:排除周末

如果上面的“四舍五入”时间戳落在一个周末,这会将它们推到下一个星期一。创建另外两个计算字段:

assigned_ts: next weekday


IF DATENAME('weekday',[assigned_ts: next day]) = "Saturday" 
THEN 
// move to start of day Monday by adding two days (48 hrs) and the Monday morning hours
DATEADD('minute', (60 * (48 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [assigned_ts: next day]))
ELSEIF DATENAME('weekday',[assigned_ts: next day]) = "Sunday" 
THEN
// move to start of day Monday by adding a day (24 hrs) and the Monday morning hours
DATEADD('minute', (60 * (24 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [assigned_ts: next day]))
ELSE 
[assigned_ts: next day]
END

created_ts: next weekday

IF DATENAME('weekday',[created_ts: next day]) = "Saturday" 
THEN 
// move to start of day Monday by adding two days (48 hrs) and the Monday morning hours
DATEADD('minute', (60 * (48 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [created_ts: next day]))
ELSEIF DATENAME('weekday',[created_ts: next day]) = "Sunday" 
THEN
// move to start of day Monday by adding a day (24 hrs) and the Monday morning hours
DATEADD('minute', (60 * (24 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [created_ts: next day]))
ELSE 
[created_ts: next day]
END

(5)更改时间戳记:排除假日

如果时间戳记在工作时间之外,则首先将时间戳记推迟到第二天。然后,如果它在第一次推送之后的周末落到星期一,则将其进一步推送到星期一。现在,如果它是假期,您可以第三次推送它。请注意,我们考虑的是周五的假期。创建另外两个计算字段。

这些计算的第一个条件是假设您要在感恩节后的第二天开始。

assigned_final


IF      ([DESCRIPTION_ASSIGNED] = 'Thanksgiving Day' 
            OR [DESCRIPTION_ASSIGNED]= 'Christmas Eve')
        AND DATENAME('weekday',[assigned_ts: next weekday]) = "Thursday" 
THEN 
// move to start of day Monday by adding 4 days (96 hrs) and the Monday morning hours
DATEADD('minute', (60 * (96 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [assigned_ts: next weekday]))

ELSEIF  NOT ISNULL([HOLIDAY_DATE_ASSIGNED])
        AND DATENAME('weekday',[assigned_ts: next weekday]) = "Friday" 
THEN
// move to start of day Monday by adding 3 days (72 hrs) and the Monday morning hours
DATEADD('minute', (60 * (72 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [assigned_ts: next weekday]))

ELSEIF  [DESCRIPTION_ASSIGNED]= 'Christmas Eve'
THEN 
// move to start of next day by adding 2 days (48 hrs) and the morning hours
DATEADD('minute', (60 * (48 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [assigned_ts: next weekday]))

ELSEIF  NOT ISNULL([HOLIDAY_DATE_ASSIGNED])
THEN
// move to start of next day by adding 1 day (24 hrs) and the morning hours
DATEADD('minute', (60 * (24 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [assigned_ts: next weekday]))

ELSE 
[assigned_ts: next weekday]
END

created_final

IF      ([DESCRIPTION_CREATED] = 'Thanksgiving Day' 
            OR [DESCRIPTION_CREATED]= 'Christmas Eve')
        AND DATENAME('weekday',[created_ts: next weekday]) = "Thursday" 
THEN 
// move to start of day Monday by adding 4 days (96 hrs) and the Monday morning hours
DATEADD('minute', (60 * (96 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [created_ts: next weekday]))

ELSEIF  NOT ISNULL([HOLIDAY_DATE_CREATED])
        AND DATENAME('weekday',[created_ts: next weekday]) = "Friday" 
THEN
// move to start of day Monday by adding 3 days (72 hrs) and the Monday morning hours
DATEADD('minute', (60 * (72 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [created_ts: next weekday]))

ELSEIF  [DESCRIPTION_CREATED]= 'Christmas Eve'
THEN 
// move to start of next day by adding 2 days (48 hrs) and the morning hours
DATEADD('minute', (60 * (48 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [created_ts: next weekday]))

ELSEIF  NOT ISNULL([HOLIDAY_DATE_CREATED])
THEN
// move to start of next day by adding 1 day (24 hrs) and the morning hours
DATEADD('minute', (60 * (24 + [Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day', [created_ts: next weekday]))

ELSE 
[created_ts: next weekday]
END

(6)中间计算

  1. 计算每个工作日的分钟数。

minutes per workday

(60 * ([Day Close Hour] - [Day Open Hour]) + INT([Day Close Minute]) - INT([Day Open Minute]))

  1. 查找从一天开始到修改后的分配时间戳记之间的分钟数

assigned_final: time to SOD

DATEDIFF(  'minute',
    DATEADD('minute', (60 * ([Day Open Hour])) + INT([Day Open Minute])
        , DATETRUNC('day',[assigned_final])),
    [assigned_final]
)
  1. 查找修改后的创建时间戳记与工作日结束之间的分钟数

created_final: time to EOD

DATEDIFF(  'minute',
    [created_final], 
    DATEADD('minute', (60 * ([Day Close Hour])) + INT([Day Close Minute])
        , DATETRUNC('day',[created_final]))
)

(7)时差

以分钟为单位计算时间差。

Time to Assign

IF DATETRUNC('day', [created_final]) = 
        DATETRUNC('day', [assigned_final])
THEN
// the same day so just return the minutes between the two times on the same day
[DateDiff minutes]
ELSEIF DATETRUNC('week', [created_final]) = 
        DATETRUNC('week', [assigned_final])
THEN
// the two days are in the same week, so add the time on the created day, 
// the time on the end day, plus a full day for each day between
(([DateDiff days] - 1) * [minutes per workday] ) + 
        [created_final: time to EOD] + [assigned_final: time from SOD]

ELSE
// the days are in two different weeks, so get the same calc as above, 
// and subtract the weekends between.
// weekends between would be datediff weeks * 2 * work minutes per day
(([DateDiff days] - 1 - ([DateDiff weeks] * 2)) * [minutes per workday] ) + 
        [created_final: time to EOD] + [assigned_final: time from SOD]
END

极其复杂,但是可以使用。