# Creating day of week filter on my dashboard

**URL:** <https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391>\
**Category:** Kibana\
**Created:** [July 14, 2023, 8:51am UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391 "2023-07-14T08:51:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 14, 2023, 8:51am UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/1 "2023-07-14T08:51:49Z")

</div>

Hello there,

I am trying to create Day of Week filter that would enable me to exclude day of the week which I do not want to see in the reports on my Dashboard.

What I did is created a new script:

doc['createdDate'].value.getDayOfWeek()

preview of the script is:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/2/025edda7b57f064d18ec5893dd2599d91804a5e4.png)

so, when I created a filter on dashboard I got this:

![image](https://us1.discourse-cdn.com/elastic/original/3X/d/a/da8dccd8bdb4ee57d499b8f81fd04fc367591da2.png)

Instead of numbers I would like to see the names of the days: Monday, Tuesday, etc. How could I do this?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 16, 2023, 1:53am UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/2 "2023-07-16T01:53:52Z")

</div>

Hi @Valerija

What version are you on?

If you are on a newer version 8.x you should use a [runtime field](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html) instead of a scripted field.

You can add them to a mapping or Data View / Index pattern

Here is my quick code...

```auto
int day = doc['@timestamp'].value.getDayOfWeek().getValue();
String dayOfWeek = "unkown";

if (day == DayOfWeek.MONDAY.value) {
  dayOfWeek = "Monday";
} else if (day == DayOfWeek.TUESDAY.value) {
  dayOfWeek = "Tuesday";
} else if (day == DayOfWeek.WEDNESDAY.value) {
  dayOfWeek = "Wednesday";
} else if (day == DayOfWeek.THURSDAY.value) {
  dayOfWeek = "Thursday";
} else if (day == DayOfWeek.FRIDAY.value) {
  dayOfWeek = "Friday";
} else if (day == DayOfWeek.SATURDAY.value) {
  dayOfWeek = "Saturday";
} else if (day == DayOfWeek.SUNDAY.value) {
  dayOfWeek = "Sunday";
} else {
  dayOfWeek = "unkown";
}

emit(dayOfWeek);

```

 ![Screenshot 2023-07-15 at 6.53.18 PM](https://us1.discourse-cdn.com/elastic/original/3X/a/4/a4204392e1f9759f2a3e78d82b9dadb280e5322b.png)

Now in my control drop down

 ![Screenshot 2023-07-15 at 6.59.16 PM](https://us1.discourse-cdn.com/elastic/original/3X/5/9/59ec28e3456b1aae4e50bfce8ba7363626b45bb7.jpeg)

if you want to have it available for all filters searches etc you need to add it to the mapping / template

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 17, 2023, 7:17am UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/3 "2023-07-17T07:17:51Z")

</div>

Hello @stephenb

Thank you very much for the explanation.

I am on 7.10 version. What you have described as a solution is available in this version as well or not?

I should add the script you provided as a new scripted field or?

I would like this filter to apply to all reports. When you say "you need to add it to the mapping / template" what do you mean?

Sorry, I am totally new to this tool so I am probably asking a lot of questions about basic things.

Thank you in advance.  
Valerija

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 17, 2023, 3:11pm UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/4 "2023-07-17T15:11:00Z")

</div>

Hi @Valerija '

7.10 is very very very old you should upgrade as a matter of urgency.

Make sure you look at the [correct docs for scripting](https://www.elastic.co/guide/en/elasticsearch/reference/7.10/modules-scripting.html) :

Runtime fields do not exist in 7.10 so you will need to continue with scripted fields which are at search time not mapping

So you will need to write a script similar to mine but with the correct syntax The script I showed should be pretty close but at the end instead of `emit` I think it will be.

`return dayOfWeek;`

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 18, 2023, 12:30pm UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/5 "2023-07-18T12:30:02Z")

</div>

Hi @stephenb

I am also surprised we are using such an old version. I will check that internally.

I wrote a script as you suggested...I am sending printscreen.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/5/d5e17465b3e17d78990fa71df78d299f381a842d.png)

Script is created but there are not results.

Regards.  
Valerija

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 18, 2023, 2:11pm UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/6 "2023-07-18T14:11:31Z")

</div>

Not sure but at the top it looks like you're trying to return a number but the script returns, text or keyword

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 19, 2023, 8:02am UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/7 "2023-07-19T08:02:40Z")

</div>

Hello @stephenb ,

I am not sure what is wrong.

I was googling some solutions and run into discussion from 2021 - [Exclude weekends(Saturdays and Sundays) from my dashboard view](https://discuss.elastic.co/t/exclude-weekends-saturdays-and-sundays-from-my-dashboard-view/272757).

This filter would solve all of my problems. I did read through the discussion but am not sure how to write the scrip in the end.

I tried like this but no success...

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/5/85198288c044b6e167cd72b8310b6669842480ad.png)

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 19, 2023, 1:26pm UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/8 "2023-07-19T13:26:09Z")

</div>

That's an invalid script You just show it about..

The script is a program it needs to be defined and then logic and then returned...

What I showed you earlier is closer but you're going to have to debug it. No other way around it.

And apologies I don't have it but old 7.10 to test it on....

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 19, 2023, 2:00pm UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/9 "2023-07-19T14:00:01Z")

</div>

I brought a 7.10 docker realy quick

This looks like it works my script above showed the exact error when I tried

I had to change  
`int day = doc['@timestamp'].value.getDayOfWeek().getValue();`  
to this  
`int day = doc['@timestamp'].value.getDayOfWeek();`

and add the `return` statement

Then it worked fine.

```auto
int day = doc['@timestamp'].value.getDayOfWeek();
String dayOfWeek = "unkown";

if (day == DayOfWeek.MONDAY.value) {
  dayOfWeek = "Monday";
} else if (day == DayOfWeek.TUESDAY.value) {
  dayOfWeek = "Tuesday";
} else if (day == DayOfWeek.WEDNESDAY.value) {
  dayOfWeek = "Wednesday";
} else if (day == DayOfWeek.THURSDAY.value) {
  dayOfWeek = "Thursday";
} else if (day == DayOfWeek.FRIDAY.value) {
  dayOfWeek = "Friday";
} else if (day == DayOfWeek.SATURDAY.value) {
  dayOfWeek = "Saturday";
} else if (day == DayOfWeek.SUNDAY.value) {
  dayOfWeek = "Sunday";
} else {
  dayOfWeek = "unkown";
}

return dayOfWeek;

```

Added it to the index pattern

 ![Screenshot 2023-07-19 at 6.52.32 AM](https://us1.discourse-cdn.com/elastic/original/3X/3/2/325ed767bdf490c67309b6433af3fa5a6689ea1e.png)

 ![Screenshot 2023-07-19 at 6.54.07 AM](https://us1.discourse-cdn.com/elastic/original/3X/f/e/feec82cb2c0881a71aca8830257a5a2b3eefc093.jpeg)

Then made a control and it worked fine, note I only have 1 day so far...

 ![Screenshot 2023-07-19 at 6.58.18 AM](https://us1.discourse-cdn.com/elastic/original/3X/a/f/af7648a9a2bced2d428014814f1a7ca3208b31c3.png)

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 20, 2023, 11:55am UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/10 "2023-07-20T11:55:15Z")

</div>

Hello @stephenb

Thank you so much for extra effort and going back to 7.10 ver.

I did as you explained but now I have another issue. I assume that this is because some actualStart fields are empty.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/7/675b1540722bb6c7d1507396fb0644661a54b135.png)

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 20, 2023, 1:26pm UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/11 "2023-07-20T13:26:07Z")

</div>

Google is your friend 🙂  
I just searched

painless script check if field exists

Found several answers

> <https://gist.github.com/jclosure/8e103dee2f7e9491845a2c0bb64c6b7a>

So you could use code like that to check if the field exists and if it does not set a default value or return a default value

You're basically writing a small program so all the normal programming things apply.

---

<div class="post-metadata">

**Author:** ![Valerija](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Valerija](https://discuss.elastic.co/u/Valerija)\
**Post date:** [July 21, 2023, 6:40am UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/12 "2023-07-21T06:40:32Z")

</div>

Hello @stephenb

The scrip works now! 🙂 Thank you so so much for all your help and your patience. I learned a lot from converstions with you about Kibana.

Thanks once again!

---

<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:** [August 18, 2023, 6:40am UTC](https://discuss.elastic.co/t/creating-day-of-week-filter-on-my-dashboard/338391/13 "2023-08-18T06:40:40Z")

</div>

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