presto - Get date for the last most recent Saturday - amazon-web-services

I want to always get the date for the most recent last Saturday since current date, and I'm trying this query:
date_trunc('week', current_date) - interval '2' day
This would work if today is Monday - Saturday, but if the day is Sunday, it would return the last Saturday rather than yesterday.
Example:
If I put 2021-07-18 as current_date, I want it to return 2021-07-17, but it actually returns 2021-07-10.
I guess this is because presto treat a week as Monday - Sunday, rather then Sunday - Saturday?
How can I solve this problem? Thanks!

Add two days, truncate, then subtract two days:
select date_trunc('week', current_date + interval '2' day) - interval '2' day

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

Power Bi DAX: Total for the month

I need to make a total for each of the months based on the working days.
So far i have working days set as 1 if the day in the month is during Monday - Friday and 0 for Saturday and Sunday. I now need to total up the 1's and make it so that it is a single value for the month.
E.g Going the weekdays is 1 and weekend is 0, January will have 22 days on each row on the table in the data mode - January 1 = 232 January 2 = 22 etc. so i can use it to divide against my target which is set to the 1st of every month.
How can i do this??
try this. i haven't tested it.
=GROUPBY( FILTER(table, table[Working_Day]=1) , table[Month], ["new col", SUMX( CURRENTGROUP(), table[Month_Order])])
filter gives working day 1's data as table,then group by performed by month, then month order has been added and returns table.

eliminate weekend dates by shifting it on work dates

I have a worksheet in which there is some work that has to be done on any specific date monthly, weekly, quarterly, yearly and daily. I have a formula that auto shows the planned date on which any work has to be done. But there is a need for change in formula that I have only taken all the dates from Monday to Saturday as Sunday is off here.
The problem arises here. please see C13, D13, E13, date of December (12/8/2019) is Sunday. (please see FT13:FW13). so I want that if Sunday falls on any planned date it should roll over to Monday automatically. for example, if 12/8/2019 is sunday this should show up the planned date automatically in cell FV13 (12/9/2019) which is Monday. it means date should be auto roll from Sunday to Monday. however, I have got a formula that can round up the Sunday for monthly planned works. (you can see E12 ,FT12:FW12) but round up of dates is still needed for yearly and quarterly planned works.
https://docs.google.com/spreadsheets/d/1CXjQDjiP_xTnb8DcpsgQKmLyaaV9XX0NdisvcruuOfY/edit#gid=1924994592
you can simply do IF statement to correct the weekend dates acordingly:
=ARRAYFORMULA(IF(A2:A<>"",
IF(TEXT(A2:A, "ddd")="Sat", A2:A+2,
IF(TEXT(A2:A, "ddd")="Sun", A2:A+1, A2:A)), ))

PowerBI - Convert Date to the WeekNumber of the Month

Date WeekNum Month Year
5/2/2018 Week 1 May 2018
6/1/2018 Week 1 June 2018
How would you get the WeekNum from the Date?
The WeekNumber needs to be week number of the particular month and not the Year.
One approach would be to take the day of the month and divide by seven:
WeekNum = ROUNDUP(DIVIDE(DAY(TableName[Date]), 7), 0)
You can use the next formula
1 + WEEKNUM(usage_users[row_date]) - WEEKNUM(STARTOFMONTH(usage_users[row_date]))
This gives you the number of the week.
basically you need to use STARTOFMONTH and WEEKNUM together:
Here is a good video explaining it also:
https://www.youtube.com/watch?v=Oq5WOmo94_Q

Power BI-Move on to the next working day

How can I use the Weekday formulae in powerBI?
The objective is if the current day is Saturday add 2 days to make it Monday(next working day) and if the current day is Sunday add 1 day to make it Monday and if its friday then add 3 days to make it the next working day.