Splunk Search

Week number and Month/Day of the begining of that week

wpreston
Motivator

I report on a count of events by week number, it displays like this:

Week Number         Count
-----------         -----
01                  15
02                  35
03                  35
...

I use strftime(_time, "%v") to get the week number. How can I also display the calendar month and day of the first day of each week in the report? Ideally, the report would look like this:

Week Number         Week Begin         Count
-----------         ----               -----
01                  12/31              15
02                  01/07              35
03                  01/14              35
...
Tags (1)
1 Solution

lguinn2
Legend

try this

| eval Day1ofWeek = strftime(relative_time(_time,"@w0"),"%m/%d")

will be the Sunday. If you want the Monday, use this

| eval Day1ofWeek = strftime(relative_time(_time,"@w1"),"%m/%d")

View solution in original post

lguinn2
Legend

try this

| eval Day1ofWeek = strftime(relative_time(_time,"@w0"),"%m/%d")

will be the Sunday. If you want the Monday, use this

| eval Day1ofWeek = strftime(relative_time(_time,"@w1"),"%m/%d")

wpreston
Motivator

Exactly what I was looking for, thanks!

0 Karma

blhuynh
Explorer

Does this work in Splunk 6.1.3? I cannot get the additional column to display when I append the piped statement to my search strings.

0 Karma

somesoni2
Revered Legend

It does work on 6.1.3. Above statement requires field _time to be available before this command. What is your current query?

0 Karma

blhuynh
Explorer

I have a set of data that I am averaging weekly using eval week=strftime(_time,"%Y-%U"). I then display this using chart values(foo) by week, bar.

However, I'd like to replace the week column with the date of the Monday of that week when it is displayed in the table. I would still like the data to be sorted by week still, but I do not want the week number to be displayed, only for that Monday of that week to be displayed. Thanks.

I wanted to generated a the Day1ofWeek column and appendcols to my search query but just generating a Day1ofWeek column with my query doesn't work.

What do you mean by "requires field _time to be available before this command"? Does that mean that I cannot use _time in my eval week statement?

0 Karma

somesoni2
Revered Legend

The answer by Lisa uses field _time (epoch format) to generate the Day1ofWeek, which is not available after your chart command, you can't use it directly. What I would suggest is to use something like this

your base search |  eval week=relative_time(_time,"@w1")| chart  values(foo) by week,bar| eval week=strftime(week,"%Y-%m-%d  %a") 

It will be sort ascending order of the date/week.

0 Karma

blhuynh
Explorer

That worked! Thanks so much especially with the prompt responses.

0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...