# Average per day of week with time\_zone

**URL:** <https://discuss.elastic.co/t/average-per-day-of-week-with-time-zone/169268>\
**Category:** Elasticsearch\
**Created:** [February 20, 2019, 5:16pm UTC](https://discuss.elastic.co/t/average-per-day-of-week-with-time-zone/169268 "2019-02-20T17:16:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![srizzle](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@srizzle](https://discuss.elastic.co/u/srizzle)\
**Post date:** [February 20, 2019, 5:16pm UTC](https://discuss.elastic.co/t/average-per-day-of-week-with-time-zone/169268/1 "2019-02-20T17:16:02Z")

</div>

Hello,

I have a message index with a 'date' and 'channelId' fields:  
POST message/\_doc  
{  
"channelId": "channel\_twitter",  
"date": "2018-03-08T14:01:23.385Z"  
}

I need to create an aggregation that calculates the average number of messages per day of the week per channel. For example - for twitter - an average of 5 messages on Mondays, 10 messages on Tuesdays etc.  
So far I have managed to get this working but now need to make it accept a timezone offset ( "time\_zone":" [(${timezoneOffset})]") parameter which will return the correct amount of messages for each day depending on time\_zone.

{  
"size": 0,  
"aggs": {  
"messages\_per\_channel": {  
"terms": {  
"field": "channelId"  
},  
"aggs": {  
"total\_messages\_per\_day\_of\_week": {  
"terms": {  
"script": {  
"lang": "painless",  
"source": "doc['date'].value.dayOfWeek"  
}  
},  
"aggs": {  
"number\_of\_weeks": {  
"date\_histogram": {  
"field": "date",  
"interval": "week",  
"extended\_bounds" : {  
"max" : "now+1m/m"  
}  
}  
},  
"average\_messages\_per\_day\_of\_week": {  
"bucket\_script": {  
"buckets\_path": {  
"doc\_count": "\_count",  
"number\_of\_weeks": "number\_of\_weeks.\_bucket\_count"  
},  
"script": "params.doc\_count / params.number\_of\_weeks"  
}  
}  
}  
}  
}  
}  
}  
}

All my attempts have ended in errors so any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 20, 2019, 5:16pm UTC](https://discuss.elastic.co/t/average-per-day-of-week-with-time-zone/169268/2 "2019-03-20T17:16:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
