Duration calculation between two dates in confluence?
Asked by Barnett Williams:

We have a team "home page" that includes the start date of each team member. For example: April 03, 2018.

Below the start date, I am trying to calculate the running year/month tenure of each team member. Something simple like: **=(TODAY DATE - 2019-04-03) / 12** . Ideally, this would then be calculated to a year, month format similar to LinkedIn's **1yr 4mos**.

Is this possible in confluence?


Answers:
author: Andrey Khaneev _StiltSoft_
body: Hi Bernett,

Is this data stored in a table format? If yes, then the [Table Transformer](https://docs.stiltsoft.com/display/public/TableFilter/How+to+use+Table+Transformer+macro) can do the trick.

comments: author: Barnett Williams
body: Currently looks like this:

![Image 2019-06-12 at 9.35.10 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/57365iBB6F4A505DBAE66F/image-size/large?v=v2&px=999 "Image 2019-06-12 at 9.35.10 AM.png")


author: Andrey Khaneev _StiltSoft_
body: Here is what I have managed to do:

I created a vertical table instead of horizontal

![2019-06-12_17h24_36.png](https://community.atlassian.com/t5/image/serverpage/image-id/57373i180C5AF77176ACB6/image-size/large?v=v2&px=999 "2019-06-12_17h24_36.png")That's because I'm not it is possible to do the trick with horizontally oriented table.

Then I added Table Transformer macro:

![2019-06-12_17h26_15.png](https://community.atlassian.com/t5/image/serverpage/image-id/57374iD9A39CD1BA7ADC10/image-size/large?v=v2&px=999 "2019-06-12_17h26_15.png")

![2019-06-12_17h26_58.png](https://community.atlassian.com/t5/image/serverpage/image-id/57376i572B68EBEF683AEC/image-size/large?v=v2&px=999 "2019-06-12_17h26_58.png")![2019-06-12_17h27_11.png](https://community.atlassian.com/t5/image/serverpage/image-id/57375i5170B7AE927F3415/image-size/large?v=v2&px=999 "2019-06-12_17h27_11.png")

<br />

SQL code:

```
SELECT 'Teammate', 'Position', 'Location', 'Anniversary Date',
CASE
WHEN 'Years' > 0 THEN 'Years' + " year(s) "
ELSE " "
END +
CASE
WHEN 'Months' > 0 THEN 'Months' + " month(s)"
ELSE " "
END AS 'Standing'
FROM
(SELECT *, 
YEAR("today" - 'Anniversary Date') - 1970 AS 'Years',
MONTH("today" - 'Anniversary Date') - 1 AS 'Months'
FROM T*)
```

And got this result:

![2019-06-12_17h22_47.png](https://community.atlassian.com/t5/image/serverpage/image-id/57377iA1556B3A9DEFD001/image-size/large?v=v2&px=999 "2019-06-12_17h22_47.png")The SQL query can be changed to show years or year when it is needed, show number of days and so on.


author: Barnett Williams
body: Thank you for this in-depth tutorial! For whatever reason the {table transformer} macro is not showing for me?


author: Andrey Khaneev _StiltSoft_
body: Yes, sorry. This macro comes from the [Table Filter and Charts](https://marketplace.atlassian.com/apps/27447/table-filter-and-charts-for-confluence?hosting=cloud&tab=overview) app.

And one more thing, I see that dates format differs in your example: Jun**e** 18**th** , 2018 / Dec 4**th**, 2017 / April 3, 2018. You'll need to use the same date format in all cells, or some dates won't be processed correctly.


author: erzn
body: Hi Andrey,

thank you for this great documentation. is it possible to extend your sql-code by days? best regards


author: Andrey Khaneev _StiltSoft_
body: Hi Ersin,

Yes, it is possible:

```
SELECT 'Teammate', 'Position', 'Location', 'Anniversary Date',
CASE
WHEN 'Years' > 0 THEN 'Years' + " year(s) "
ELSE " "
END +
CASE
WHEN 'Months' > 0 THEN 'Months' + " month(s) "
ELSE " "
END +
CASE
WHEN 'Days' > 0 THEN 'Days' + " day(s) "
ELSE " "
END AS 'Standing'
FROM
(SELECT *, 
YEAR("today" - 'Anniversary Date') - 1970 AS 'Years',
MONTH("today" - 'Anniversary Date') - 1 AS 'Months',
DAY("today" - 'Anniversary Date') AS 'Days'
FROM T*)
```


author: erzn
body: Thank you for your quick response. it works great as it should do :)


author: Kevin Hunter
body: I tripped on this today and it works wonderfully, but crashing and burning on attempting to do \*two\* of these on each row... I have a list of team members and I want to calculate both the time since they started with our company, and the time in their current role. Is it possible to do both calculations on the same row?


author: Andrey Khaneev _StiltSoft_
body: Hi Kevin,

Here is an example with two dates:

![2020-05-29_15h37_14.png](https://community.atlassian.com/t5/image/serverpage/image-id/97000iC2154C0F72150B09/image-size/large?v=v2&px=999 "2020-05-29_15h37_14.png")![2020-05-29_15h37_32.png](https://community.atlassian.com/t5/image/serverpage/image-id/97001i1E9C9F511E3A198A/image-size/large?v=v2&px=999 "2020-05-29_15h37_32.png")

```
SELECT 'Anniversary Date', 'Other date',
CASE
WHEN 'Years' > 0 THEN 'Years' + " year(s) "
ELSE " "
END +
CASE
WHEN 'Months' > 0 THEN 'Months' + " month(s)"
ELSE " "
END AS 'Standing',
CASE
WHEN 'Years od' > 0 THEN 'Years od' + " year(s) "
ELSE " "
END +
CASE
WHEN 'Months od' > 0 THEN 'Months od' + " month(s)"
ELSE " "
END AS 'Standing Other'
FROM
(SELECT *, 
YEAR("today" - 'Anniversary Date') - 1970 AS 'Years',
MONTH("today" - 'Anniversary Date') - 1 AS 'Months', 
YEAR("today" - 'Other date') - 1970 AS 'Years od',
MONTH("today" - 'Other date') - 1 AS 'Months od'
FROM T1)
```

The additional query is a copy paste of the original. Also it can be much simplified if it is fine to see 0 years and 0 months.


author: jason sheridan
body: Hi [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) - your solution is awesome. What has hamstrung me with confluence many times, is the ability to dynamically calculate date.  

If you are still looking at this, I'd be interested in a couple of variations:

1. The calcs split out the years/months/days. Is it possible to just show one time type - ie. I am only interested in seeing the result in total number of days rather than the years and months as well (ie 150 days)

2. Have you ever managed to calculate a date in one row - against a date in another row ?


author: Andrey Khaneev _StiltSoft_
body: Hi [@jason sheridan](/t5/user/viewprofilepage/user-id/4080609),

Glad to know you found this solution useful.

1. It is even easier than the original solution:

```
SELECT *,
(T1.'End Date' - T1.'Start Date') / "24h" AS 'Days',
("today" - T1.'Start Date') / "24h" AS 'Days till today'
FROM T*
```

![2020-09-10_10h13_24.png](https://community.atlassian.com/t5/image/serverpage/image-id/109794i8334272711918019/image-size/large?v=v2&px=999 "2020-09-10_10h13_24.png")

2. Most likely it is possible, Could attach a screenshot of the table and describe what should be calculated?

Also, you can find some useful solutions [here](https://docs.stiltsoft.com/display/TFAC/Custom+Transformation+-+use+cases+with+advanced+SQL+queries).


Tags: atlassian-marketplace

developer

marketplace

script