How do I calculate duration between the dates in Confluence excluding weekends
Asked by Gireesha Jeera:

I am trying to display duration column with difference between 2 dates in Confluence. I have tried below:

select \*,(CAST(ROUND(ceiling(DATEDIFF(DAY,'From Date','To Date')))as int))+1+ " days" as 'Duration' from T1

this gives the result, however given the dates 25-Jun-2020 to 26-Jun-2020 - duration displayed as 1 instead of 2.

your help is appreciated here. thanks.


Answers:
author: Andrey Khaneev _StiltSoft_
body: Hi [@Gireesha Jeera](/t5/user/viewprofilepage/user-id/4028302), I used your query in the Table Transformer macro and it works as expected:

![2020-07-27_14h12_29.png](https://community.atlassian.com/t5/image/serverpage/image-id/104388i195738CFD8D44C65/image-size/large?v=v2&px=999 "2020-07-27_14h12_29.png")

What version of [Table Filter and Charts](https://marketplace.atlassian.com/apps/27447/table-filter-and-charts-for-confluence?hosting=cloud&tab=overview) do you use?

Did you set the correct date format dd-M-yy in the macro settings?

comments: author: Gireesha Jeera
body: Hi [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) ,

Thanks for the quick response, in my case table looks as below:

for me this still displayed as 1 days, can you please help here ?

![confluence`.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/104392iFFDECC0BD5D9DC53/image-size/large?v=v2&px=999 "confluence`.jpg")


author: Gireesha Jeera
body: Also [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) it works may be because i have added + 1 for the below query. however this query doesnt work if there are blank dates and displayed as 1 days.

select \*,(CAST(ROUND(ceiling(DATEDIFF(DAY,'From Date','To Date')))as int))**+1**+ " days" as 'Duration' from T1

so try this and let me know, thank you.

**select \*,(CAST(ROUND(ceiling(DATEDIFF(DAY,'From Date','To Date')))as int))+ " days" as 'Duration' from T1**


author: Andrey Khaneev _StiltSoft_
body: Strange... Could you try this query:

```
select *,('To Date' - 'From Date' + "1d") / "1d" + " days" as 'Duration' from T1
```


author: Andrey Khaneev _StiltSoft_
body: Your second query outputs "1 days" for the same dates.

```
select *,(CAST(ROUND(ceiling(DATEDIFF(DAY,'From Date','To Date')))as int))+ " days" as 'Duration' from T1
```


author: Gireesha Jeera
body: [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696)

this query gives output of **4.428571428571429** days in Duration column. instead of day in whole number format (should be 2 days) thank you.


author: Andrey Khaneev _StiltSoft_
body: ```
select *,('To Date' - 'From Date' + "24h") / "24h" + " days" as 'Duration' from T1
```

Please, try this. By default "1d" equals to 8 hours (working day). You can change it in the Worklog settings:

![2020-07-27_15h11_29.png](https://community.atlassian.com/t5/image/serverpage/image-id/104393i407E73677C461303/image-size/large?v=v2&px=999 "2020-07-27_15h11_29.png")


author: Gireesha Jeera
body: [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) Perfect works mate, thank you.

is there any solution to exclude weekends while calculating the number of days between the dates using SQL query ?

Thank you once again mate :)


author: Andrey Khaneev _StiltSoft_
body: Glad to help you!

Yes, it is possible, but the query is quite complex. [See the docs](https://docs.stiltsoft.com/display/public/TFAC/Custom+Transformation+-+use+cases+with+advanced+SQL+queries#CustomTransformation-usecaseswithadvancedSQLqueries-Calculatingthenumberofworkdaysinaperiodoftime). Let me know if you have any questions.


author: Gireesha Jeera
body: [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) perfect, that works perfectly. thank you once again.


author: Gireesha Jeera
body: [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) sorry to trouble you again,

i have tried between the dates 19 Oct 2020 to 30 Oct 2020, its results in 12.041666666666666 days

can you help here to round off please ?

**i am using below query currently:**

select \*,('To Date' - 'From Date' + "24h") / "24h" + " days" as 'Duration' from T1


author: Andrey Khaneev _StiltSoft_
body: [@Gireesha Jeera](/t5/user/viewprofilepage/user-id/4028302), that's because of one extra hour due to daylight saving time. Please, ROUND the calculated value:

```
select *,ROUND(('To Date' - 'From Date' + "24h") / "24h") + " days" as 'Duration' from T1
```


author: Gireesha Jeera
body: Thanks [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) it worked for the number of days but for empty values of both from date and date displayed as "NaN days"


author: Andrey Khaneev _StiltSoft_
body: Please, use this query:

```
select *,
CASE WHEN T1.'To Date' IS NOT NULL AND T1.'From Date' IS NOT NULL THEN
ROUND(('To Date' - 'From Date' + "24h") / "24h") + " days"
ELSE "" END
as 'Duration' from T1
```


author: Gireesha Jeera
body: [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) thanks a lot mate, it worked perfectly.

thank you once again.


author: Godwin jino
body: [@Gireesha Jeera](/t5/user/viewprofilepage/user-id/4028302)   

i have used this SQL   

SELECT` `*,` `'Days'` `-` `2` `*` `(('Days'` `/` `7)`::`integer)` `-` `CASE` `WHEN` `'Days'` `%` `7` `=` `0` `THEN` `0` `WHEN` `'Start date'`::`Date->`getDay`()` `=` `0` `THEN` `1` `WHEN` `'Start date'`::`Date->`getDay`()` `+` `'Days'` `%` `7` `=` `7` `THEN` `1` `WHEN` `'Start date'`::`Date->`getDay`()` `+` `'Days'` `%` `7` `>` `7` `THEN` `2` `ELSE` `0` `END` `AS` `'Work days'` `FROM` `(SELECT` `*,` `ROUND((`T1`.'End date'` `-` T1`.'Start date')` `/` `"1d")` `+` `1` `AS` `'Days'` `FROM` T1`)

And next step i don't have clear idea can you help to what are the fields needs to be updated .

comments: author: Stiltsoft support
body: Hi [@Godwin jino](/t5/user/viewprofilepage/user-id/5443704) ,

Please check the new example from our documentation:

<https://docs.stiltsoft.com/tfac/cloud/custom-transformation-use-cases-with-advanced-sql-queries-42241587.html#CustomTransformationusecaseswithadvancedSQLqueries-Calculatingthenumberofworkdaysinaperiodoftime>

![Wed 13-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/318956i63258CE43B524D7C/image-size/large?v=v2&px=999 "Wed 13-1.png")

SELECT \*,  
'Days' -  
2 \* (('Days' / 7)::integer) -  
CASE  
WHEN 'Days' % 7 = 0 THEN 0  
WHEN 'Start date'::Date-\>getDay() = 0 THEN 1  
WHEN 'Start date'::Date-\>getDay() + 'Days' % 7 = 7 THEN 1  
WHEN 'Start date'::Date-\>getDay() + 'Days' % 7 \> 7 THEN 2  
ELSE 0  
END AS 'Work days'  
FROM  
(SELECT \*,  
ROUND((T1.'End date' - T1.'Start date') / "1d") + 1 AS 'Days'  
FROM T1)

![Wed 13-2.png](https://community.atlassian.com/t5/image/serverpage/image-id/318957iDBF0A1B0278A09C5/image-size/large?v=v2&px=999 "Wed 13-2.png")

If smth goes wrong for you, please refer to [our support](https://stiltsoft.atlassian.net/servicedesk/customer/portal/14). Attach the page storage of your page (upper right corner -\> menu ... -\> View storage format). Then we'll be able to recreate exactly your page and look into the issue.


author: Godwin jino
body: [@Stiltsoft support](/t5/user/viewprofilepage/user-id/5062270) ,

Thanks for your answer its working now.


Tags: filter

table

table-filter

table-filter-and-charts

table-filter-charts-spreadsheets

table-transformer