How create rule in AWS with CRON expression - amazon-web-services

I want to create a scheduled expression, which will start js script every week at 5:30PM PT using CloudWatch Events. I tried to insert:
- 0 30 3 * * THU *
- 0 30 3 THU
But got
Error
There was an error while saving rule getEvery1min.
Details: Parameter ScheduleExpression is not valid..
Can you help me pls?

According the docs - Schedule Expressions for Rules
Only this is work - 30 17 ? * THU *
You can't specify the Day-of-month and Day-of-week fields in the same cron expression. If you specify a value (or a *) in one of the fields, you must use a ? (question mark) in the other.

The cron expression should be in the below format
"Minutes" "Hours" "Day of month" "Month" "Day of week" "Year"
Based on the above cron it appears you're trying to run the cron expression only on Thursdays on the third day of the month. However if you want it to run once a week at 5:30pm you'd want something like the below.
30 17 * * THU *
Also take care to remember this expression is in UTC, so you would need to adjust it for your timezone.
For more information take a look at the Scheduled Events documentation.

Related

Get Number of "Business Seconds" Between Two Unix Timestamps

Given these two timestamps/converted to datetimes:
Start - 1668105400.814 - Thursday, November 10, 2022 12:36 PM
End - 1668444427.195 - Monday, November 14, 2022 10:47 AM
...how can I get the number of seconds, adjusted for "business" days? With this example, you'll see that the start date was a Thursday (Friday was a holiday in Canada), and the end date was a Monday. Taking holidays into account isn't super important here, would be nice, but for simplicity of the solution, it would be cool to subtract 172800 seconds (2 days' worth) from the difference between the End and the Start if it stretches over a weekend.
My dataset looks like this:
type created_at emitted_at
assign 1668105400.814 1668105400.814
archive 1668105400.814 1668444427.195
How I'm currently calculating the number of seconds is in an added custom column with the formula:
_Created_To_Archive_Handle_Time = IF(Front_Email_Stats[type]="archive", ROUND((Front_Email_Stats[emitted_at] - Front_Email_Stats[conversation.created_at]), 1), BLANK())
...where it filters for type of archive only, and then subtracts the archived emitted_at timestamp from its created_at timestamp.
A naive approach would look something like this:
_Created_To_Archive_Handle_Time = IF(Front_Email_Stats[type]="archive", IF(AND(WEEKDAY(Front_Email_Stats[_created_at_datetime_custom_column]) < 7, WEEKDAY(Front_Email_Stats[_emitted_at_datetime_custom_column]) > 1), ROUND((Front_Email_Stats[emitted_at] - Front_Email_Stats[conversation.created_at]), 1) - 172800, ROUND((Front_Email_Stats[emitted_at] - Front_Email_Stats[conversation.created_at]), 1)), BLANK())
...where we filter for archive types only, and then do a simple check if the Start timestamp's day of week is less than Saturday (7 I think?) and the End timestamp's day of week is greater than Sunday (1?), but wondering if there's a more elegant solution.
Use NETWORKDAYS() and then subtract the seconds manually.
https://learn.microsoft.com/en-us/dax/networkdays-dax

How to replicate week number that was generated using Redshift's to_char on Snowflake?

I'm trying to migrate a Redshift SQL script to Snowflake and trying to emulate week number on Snowflake. Below is the sample Redshift code:
select cast(to_char('2020-01-06'::date, 'WW') as int) as week,
cast(to_char('2020-01-06'::date, 'IW') as int) as iso_week,
'2020-01-06'::date;
The closest functions I found on Snowflake was like this:
select cast(WEEKOFYEAR('2020-01-06'::date) as int) as week,
cast(WEEKISO('2020-01-06'::date) as int) as iso_week,
'2020-01-06'::date;
iso_week fields are matching, however week doesn't [Redshift shows 1, Snowflake shows 2]. Is there any function that emulates Redshift's behavior?
This is the definition of WW on Redshift
WW: Week number of year (1–53; the first week starts on the first day of the year.)
Then the equivalent is to get the "day of year", and divide by 7 to count the number of weeks. We will also need some +/- ones to account for base 0:
select 1+floor((dayofyear('2020-01-06'::date)-1)/7)
-- 1
In UDF form:
create or replace function weeknumber_ww(x date)
returns int
as $$
1+floor((dayofyear(x)-1)/7)
$$
;
select weeknumber_ww('2020-01-06');
It is depenedent on parameter WEEK_OF_YEAR_POLICY
Specifies how the weeks in a given year are computed.
0: The semantics used are equivalent to the ISO semantics, in which a week belongs to a given year if at least 4 days of that week are in that year.
1: January 1 is included in the first week of the year and December 31 is included in the last week of the year.

AWS redshift schedule for specific days using CRON

I'm trying to set up Redshift scheduling using CRON for running it 5 days a week (MON-FRI) between 7-14 ETC. The format is described as such:
Cron format: minute hour day-of-month month day-of-week year
Use , (comma) to include additional values, - (hyphen) to specify a range, * (asterisk) to include all values, and / (forward slash) to specify increments.
So I use these values:
Pause - 0 16 * * 1-5 *
Start - 0 9 * * 1-5 *
AWS shows me an error Invalid cron schedule expression: cron(0 9 * * 1-5 *). If I substitute 1-5 by ?, it works, but that's not what I need.
Is the range indeed supported by this scheduler and I'm doing something wrong?
The third asterisk has to be a question mark when specifying the day of the week like this: 0 16 ? * 1-5 *
The doc says:
The * (asterisk) wildcard includes all values in the field. In the
Hours field, * would include every hour. You cannot use * in both the
Day-of-month and Day-of-week fields. If you use it in one, you must
use ? in the other.

Start week on monday in AWS Quicksight

I know I can change the date field granularity to week in AWS Quicksight, and I can also display the date by week number. But as far as I understand, Quicksight defines the start of a week on Sunday, and I need it to be Monday.
Is there any way to start the week on Monday in AWS Quicksight?
Here is a formula for a calculated field you can add that will group your dates by weeks starting on Mondays. You should be able just replace {date_date} with your field name and copy/paste this into the formula box (including the newlines) and it will do the trick.
addDateTime(
1,
'DD',
truncDate(
'WK',
ifelse(
extract(
'WD',
{date_date}
) = 1,
addDateTime(
-1,
'DD',
{date_date}
),
{date_date}
)
)
)
This field will equal the Monday that starts the week your date falls into.
You can put this all in one line but I added the line breaks for readability.
It essentials checks if the date is on a Sunday, and moves it to the previous week and then just adds a day to the normal trunc function so that the weeks begin on Monday.
Acoording to the documentation
https://docs.aws.amazon.com/quicksight/latest/user/truncDate-function.html
WK: This returns the week portion of the date. The week starts on Sunday in Amazon QuickSight.
you can use filters to start like here
This is still an issue when using filters and rolling dates as weeks in analytics.
There is an open thread in the AWS community forum.
https://community.amazonquicksight.com/t/starting-weeks-on-monday-show-gaps-on-custom-weekly-groupings/4222
As a I workaround for this, I would recommend defining a column (either in the dataset or as a calculated field) that represents the year_week_iso (ie. 2023-W01 or 202301 if you prefer to have an integer value).
This column can then be used to group data in order to show trends over time when the week begins on a Monday.
The integer version of this column can also be leveraged to show the most current week of data by setting up a Top 1 filter that is based off the max(year_week_iso_integer) →
example Amazon Quicksight filter to get current week

SAS - INTNX Defining a Week

I have been using a SAS code I have written to group a historical dataset (that gets updated weekly) by grouping them into weeks (Sunday - Saturday = 1 group). I have been using INTNX.
Our definition of a week has now changed and is Tuesday through Monday. How can I update the code below to make that adjustment. I have been messing with the 'Week' function and trying to add days to the end to make this change but to no success.
Currently, I am using:
WEEKOF = INTNX('Week', SasDate,0);
Where "SasDate" is the date of each record in my dataset.
Any help would be greatly appreciated.
Thanks,
Michael
Simply change it to
weekof = intnx('week.3',sasdate,0,'beginning') ;
Sun = 1, Mon = 2, Tue = 3, etc.