I am running this query in splunk which bucketizes the data on a weekly basis , based on the field "impact_start" and gives me the output. But the problem is that the start of the week in the output is Thursday rather than Monday.
Is there any way i can change the start of the week to Monday instead of Thursday?
search index=* impact=1 OR impact=2 product_line=* | eval time = round( strptime(impact_start,"%Y-%m-%d %H:%M:%S"), 0 )| where time >= 1473328728 AND time<=1476352728| bucket time span=7d | stats values(number) as incident_name by time
You're in luck - If you look at the strftime spec, Monday is considered the start of the week
http://strftime.org/
So you could try this: (untested)
index=* impact=1 OR impact=2 product_line=*
| eval time = round( strptime(impact_start,"%Y-%m-%d %H:%M:%S"), 0 )
| where time >= 1473328728 AND time<=1476352728
| eval week = strftime(impact_start,"%Y %w")
| stats values(number) as incident_name by week
Related
How can I get the number of days passed in the current quarter?
For example, if today is 1/2/2021, it will return 2.
If today is 2/5, it will return 36.
If today is 4/2, it will return 2.
Use date_trunc() to get the start of the quarter and subtract dates:
WITH cte(day) AS (
VALUES
(date '2021-01-02')
, (date '2021-02-05')
, (date '2021-04-02')
)
SELECT day
, day - date_trunc('quarter', day)::date + 1 AS days_passed_in_quarter
FROM cte;
day | days_passed_in_quarter
------------+------------------------
2021-01-02 | 2
2021-02-05 | 36
2021-04-02 | 2
+ 1 to fix off-by-one error as you clearly want to include the current day as "passed".
Always use unambiguous ISO 8601 date format (YYYY-MM-DD - 2021-02-05), which is the default in Postgres and always unambiguous, or you depend on the current datestyle setting (and may be in for surprises). Also avoids misunderstandings general communication.
Related:
PostgreSQL: between with datetime
I want to get the last Monday date for the given date. For example If my input is 190113 I want the output as 190107 which is last Monday.
if {$current_date == "Mon"} {
set startday [clock seconds]
set startday [clock format $startday -format %y%m%d]
puts $startday
} else {
puts "no monday today"
#I don't know how to get last monday date
}
This can be done fairly simply, by taking advantage of the fact that clock scan has quite a complex parser, and you can supply a timestamp that everything is relative to via the -base option. Also, both clock scan and clock format take -format options so that you can specify exactly what is going on in your input and output data.
proc getLastMonday {baseDate} {
set base [clock scan $baseDate -format "%y%m%d"]
set timestamp [clock scan "12:00 last monday" -base $base]
return [clock format $timestamp -format "%y%m%d"]
# This would work as a one-liner, provided you like long lines
}
Demonstrating:
puts [getLastMonday 190113]; # ==> 190107
puts [getLastMonday 190131]; # ==> 190128
Reference: https://www.tcl.tk/man/tcl/TclCmd/clock.htm#M22
Here's a sample code-snippet for the purpose. Added inline comments for understanding:
proc get_last_monday_date {date} {
# Get the end timestamp for the specified date
set end_timestamp [clock scan ${date}-23:59:59 -format %y%m%d-%H:%M:%S]
# Get day of the week for the current date
set day_of_week [clock format $end_timestamp -format %u]
# Sunday may report as 0 or 7. If 0, change to 7
# if {$day_of_week == 0} {
# set day_of_week 7
# }
# Monday is 1st day of the week. Monday = 1.
# Find how many days to go back in time
set delta_days [expr $day_of_week - 1]
# Multiply the delta by 24 hours and subtract from end of the day timestamp
# Get the timestamp for the result. That's last Monday's timestamp.
return [clock format [clock add $end_timestamp -[expr $delta_days * 24] hours] -format %D]
}
puts "Last Monday for 01-Jan-2019: [get_last_monday_date 190101]"
puts "Last Monday for 06-Jan-2019: [get_last_monday_date 190106]"
puts "Last Monday for 15-Jan-2019: [get_last_monday_date 190115]"
puts "Last Monday for 31-Jan-2019: [get_last_monday_date 190131]"
Execution output:
Last Monday for 01-Jan-2019: 12/31/2018
Last Monday for 06-Jan-2019: 12/31/2018
Last Monday for 15-Jan-2019: 01/14/2019
Last Monday for 31-Jan-2019: 01/28/2019
I'm using BigQuery standart SQL and i need to find the difference between 2 timestamps, in minutes.
For example:
Timestamp1 = '2016-10-10 09:40:00' |
Timestamp2 = '2016-10-10 09:50:00'
I want to return the difference:
Timestamp2-Timestamp1 = 10
I found how to do it with Legacy SQL, but it doesn't help:
https://cloud.google.com/bigquery/query-reference#datediff
Thank you !
check for TIMESTAMP_DIFF function
SELECT
TIMESTAMP "2016-10-10 09:50:00" as first_timestamp,
TIMESTAMP "2016-10-10 09:40:00" as second_timestamp,
TIMESTAMP_DIFF(TIMESTAMP "2016-10-10 09:50:00",
TIMESTAMP "2016-10-10 09:40:00", MINUTE) AS minutes;
Currently my data looks like this -
I have 3 sets of comparisons - Daily, Weekly, and Monthly. When comparing weekly cohorts against other weekly cohorts and monthly cohorts against other monthly cohorts, you often run into the case where the cohort is not fully 'baked'. This means, for example, the first cohort day in each week (i.e. install_cohort == 2/21/2016 with Install Cohort Week == Feb 21th) has more days_since_install than install_cohort == 2/27/2016 (the last day in the week of Install Cohort Week == Feb 21th).
When making comparisons between weeks this means not everyone has moved through the same days_since_install.
The goal is to filter the data such that every cohort has the same days_since_install which would get rid of the additional days_since_install that install_cohort == 2/21/2016 has over install_cohort == 2/27/2016 for example. I only want to make comparisons where each week's collection of install_cohorts has the same number of days_since_install.
Ok after a long discussion,
Create a calculated field with {fixed [install_cohort_month]: max([install_cohort])} and call it [max_date]
create a calculated field datediff('day', [max_date],today())
That gives you a table similar to this
Month | week | install_cohort | max_date | days_since_install |
28/2/2016 | 7/2/2016 | 1/2/2016 | 28/2/2016 | 50 |
28/2/2016 | 28/2/2016 | 28/2/2016 | 28/2/2016 | 50 |
You can change the first calc to use the month field instead of the week field as well.
I need to make my Access query always return the Monday of the current week. I have seen a few solutions on Google/StackOverflow but they are written in SQL and I am a beginner in creating Access queries (I am using the Design view to make them).
Goal: The week should be considered as M T W T F S S. Then, the query should always return the Monday of the current week. Therefore, if it is Sunday, it should still return the Monday before, NOT the next week's Monday. Can anyone explain how to do this using the Design View in Access 2010?
Keep in mind that in this context we are working with dates, so if we do Date() - 1, we will get 1 day prior to today.
Date() ~ Today's date
DatePart(
"w" - Weekday
Date() - Today's date
2 - vBMonday (Access assumes Sunday is the first day of the week, which is why this is necessary.)
1 - vbFirstJan1 - This gets into using the first week of the year. We could have omitted this, as 1 is the default.
)
-1 - Subtract 1 from the DatePart value.
Values
Date() = 4/27/2015 (at time of this writing)
DatePart("w",Date(),2,1) = 1
DatePart("w",Date(),2,1)-1 = 0
So we have Date()-0... Okay, what's so great about that? Well, let's look at a more useful scenario where today's date is a day other than Monday.
Let's act like today is 4/28/2015 (Tuesday)
Date() = 4/28/2015
DatePart("w",Date(),2,1) = 2
DatePart("w",Date(),2,1)-1 = 1
So, from the outside, in; give me the current weekday value. (1 = Monday, 2 = Tuesday, etc.), and subtract 1 from that -> that's how many days we need to subtract from the current date to get back to the weekday value of 1 (Monday).
Here's a function that will do this:
Public Function DatePrevWeekday( _
ByVal datDate As Date, _
Optional ByVal bytWeekday As VbDayOfWeek = vbMonday) _
As Date
' Returns the date of the previous weekday, as spelled in vbXxxxday, prior to datDate.
' 2000-09-06. Cactus Data ApS.
' No special error handling.
On Error Resume Next
DatePrevWeekday = DateAdd("d", 1 - Weekday(datDate, bytWeekday), datDate)
End Function
As vbMonday is 2 and your date is today, you can use the core expression in a query:
PreviousMonday: DateAdd("d",1-Weekday(Date(),2),Date())