# I have a field "time\_range" which holds hours data at which row was created (like 0:00 - 0:59, 13:00 - 13:59 etc.) and I want to visualize the average of count of every time range. How can I do that? Also I want to order the time range by hour

**URL:** <https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941>\
**Category:** Q&A\
**Tags:** calculations, quick-sight\
**Created:** [April 6, 2023, 1:15pm UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941 "2023-04-06T13:15:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raghu](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@Raghu](https://community.amazonquick.com/u/Raghu)\
**Post date:** [April 6, 2023, 1:15pm UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941/1 "2023-04-06T13:15:03Z")

</div>

![image](https://d1wl4jav5dp96l.cloudfront.net/original/2X/a/a253de16b02be8e6e393bdb4e85e9e2b474dec3a.png)

---

<div class="post-metadata">

**Author:** ![Max](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/max/32/2288_2.png) [@Max](https://community.amazonquick.com/u/Max)\
**Post date:** [April 6, 2023, 8:41pm UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941/2 "2023-04-06T20:41:03Z")

</div>

Can you explain more of what you mean by average count of every time range? The average count of these three colors per time range, or the average count over all time ranges?

In regards to each you would do a countOver but partition either by the group or nothing.

`countOver({metric},[{color}])` or `countOver({metric},[])`

For the second part you will need to make these groups integers and then sort by that calculated field.

`parseInt(substring({group_field}, 1,locate({group_field} , ':' )-1))`

This will get the integer of the group and then you will sort by this field.

---

<div class="post-metadata">

**Author:** ![Raghu](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@Raghu](https://community.amazonquick.com/u/Raghu)\
**Post date:** [April 7, 2023, 7:06am UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941/4 "2023-04-07T07:06:43Z")

</div>

In my dataset, I have:  
i) a field named “createdon” which stores date on which the referral was created.  
ii) a field named “hours\_series” which contains time range at which referral was created. For example, if user created referral at 2:34 PM (i.e. between 2PM and 3PM), then value of “hours\_series” for that row will be “14:00 - 14:59” in string format.

In my visual, I want to show Average number of referrals per hour.  
For that we need to divide the count of referrals of that particular hour by total number of days.  
(Total number of days = most recent date in “hours\_series” - first date in “hours\_series”)

Right now, I am fetching this data from database using postgreSQL using the following query:

select count(\*),cast(DATE\_PART(‘day’, ‘2023-04-07’::timestamp - ‘2023-03-01’::timestamp)as decimal),  
cast(DATE\_PART(‘hour’,P.CreatedOn) as integer) AS OnHour from PatientInfo P, ReferrerDetail R, PatientDiagnosis L   
where P.Id=R.PatientId and P.Id=L.PatientId and R.IsDeleted =false and P.IsDeleted=false   
and P.CreatedOn\>=‘2023-03-01’ and P.createdon \<=‘2023-04-07’  
and Urgency is not null and Urgency\<\>‘’ group by DATE\_PART(‘hour’,P.CreatedOn);

But now I am swithcing my stats to quicksight. How can I do this on quicksight.

I am attaching output of that query and a sample of my dataset.

Kindly help and thanks for the solution of second part.

 ![image](https://d1wl4jav5dp96l.cloudfront.net/original/2X/6/62feef7fb43159df31e44a8b1515f213d3f4f754.png)

![image](https://d1wl4jav5dp96l.cloudfront.net/original/2X/3/348c632adec3e404647e9719baae48c7efca939f.png)

---

<div class="post-metadata">

**Author:** ![Max](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/max/32/2288_2.png) [@Max](https://community.amazonquick.com/u/Max)\
**Post date:** [April 7, 2023, 1:45pm UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941/5 "2023-04-07T13:45:53Z")

</div>

Have you tried my calculations?

Do you just need to add a division of total number of days?

---

<div class="post-metadata">

**Author:** ![Raghu](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@Raghu](https://community.amazonquick.com/u/Raghu)\
**Post date:** [April 10, 2023, 1:43pm UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941/6 "2023-04-10T13:43:11Z")

</div>

After trying calculations, we got close to goal but still we have an issue.

I am using count({hours\_series}) / dateDiff(min(createdon), max(createdon)) to calculate the required average but dateDiff() is returning different value for each row. As we want to divide count(hours\_series) by the number of days, value of dateDiff() should be same for all the rows to calculate accurate average.

After observing the data, I am perceiving that min() and max() functions are not returning the first and latest date of whole dataset but they are returning first and latest date corresponding to each value of hours\_series.

Is there any solution of this?

I am attaching the screenshot of data these calculated fields are returning.

1st column : hours\_series  
2nd column : count(hours\_series)  
3rd column : dateDiff(min(createdon), max(createdon))  
4th column : count(hours\_series) / dateDiff(min(createdon), max(createdon))

 ![image](https://d1wl4jav5dp96l.cloudfront.net/original/2X/9/9e30dc9baeb950e0089adb82c11349bfdb9373b4.png)

---

<div class="post-metadata">

**Author:** ![Max](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/max/32/2288_2.png) [@Max](https://community.amazonquick.com/u/Max)\
**Post date:** [April 10, 2023, 1:47pm UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941/7 "2023-04-10T13:47:44Z")

</div>

you can say` minOver({createdon}),[],PRE_AGG)`

---

<div class="post-metadata">

**Author:** ![Raghu](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@Raghu](https://community.amazonquick.com/u/Raghu)\
**Post date:** [April 11, 2023, 7:00am UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941/9 "2023-04-11T07:00:56Z")

</div>

Great, we got correct dateDiff() after using minOver() and maxOver(). Now the last issue I am facing is while dividing the count({createdon}) by dateDiff(minOver(({createdon}), , PRE\_AGG), maxOver(({createdon}), , PRE\_AGG)), it is showing mismatched aggregation error.

Is there any way to get over this?

![image](https://d1wl4jav5dp96l.cloudfront.net/original/2X/f/f1a9eb0329a4b94f61f07e35c88906ef3ca11065.png)  
 ![image](https://d1wl4jav5dp96l.cloudfront.net/original/2X/8/8ce88e43920d79049303e0a6aaa8838f5dc3ab9d.png)

---

<div class="post-metadata">

**Author:** ![Max](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/max/32/2288_2.png) [@Max](https://community.amazonquick.com/u/Max)\
**Post date:** [April 11, 2023, 1:25pm UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941/10 "2023-04-11T13:25:21Z")

</div>

Use `countOver({hours_series},[],PRE_AGG)`

---

<div class="post-metadata">

**Author:** ![Raghu](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@Raghu](https://community.amazonquick.com/u/Raghu)\
**Post date:** [April 12, 2023, 6:42am UTC](https://community.amazonquick.com/t/i-have-a-field-time-range-which-holds-hours-data-at-which-row-was-created-like-0-00-0-59-13-00-13-59-etc-and-i-want-to-visualize-the-average-of-count-of-every-time-range-how-can-i-do-that-also-i-want-to-order-the-time-range-by-hour/11941/11 "2023-04-12T06:42:25Z")

</div>

Thanks @Max for help. Calculations are generating the expected output.
