I am trying to create an Analytics report and I want to set the date range from Monday to Sunday.
I am setting the Start date to: =today()-7 and the End day to =today()-1, but this shows the last 7 days everytime I run the report (say I run it on wednesday, it will show data from wednesday to tuesday).
How can I set the date range so it shows it from last Monday to Sunday?
Short answer
Use WEEKDAY
Explanation
WEEKDAY returns the day of the week.
To get the Monday's date, use this value to subtract it from today's date.
To get the Sunday's date, add 6 to the monday date.
Example
Today =today() 11/14/2016
Weekday =WEEKDAY(B1,3) 0
Start =B1-B2 11/14/2016
End =B3+6 11/20/2016
Related
I have the following azure function that is supposed to retrieve the date of the previous Monday. It works fine except for if the current date is a monday. I need the function to still retrieve the previous monday date if it is Monday or Tuesday. This is due to the time not being updated until middle of day tuesday.
#{formatDateTime( subtractFromTime( utcNow(), sub(dayOfWeek(utcNow()),1), 'Day' ), 'yyyy-MM-dd 00:00:00' )}
I am still learning Azure synapse so I am not sure if I can write an IF statement that accomplishes this or if there is a better way to write it.
I have reproduced the above and able to get the desired result by using the below dynamic content.
#if(greater(dayOfWeek(utcnow()),1),formatDateTime(addDays(subtractFromTime(utcnow(),dayOfWeek(utcnow()),'Day'),1),'yyyy/MM/dd'),formatDateTime(addDays(subtractFromTime(utcnow(),dayOfWeek(utcnow()),'Day'),-6),'yyyy/MM/dd'))
The day of week for a monday from last sunday is 1, so I am checking the current date's day of week is greater than monday or not.
If it is greater (Today is not a Monday), then I am giving last Monday by adding 1 day to the last Sunday.
If it is not, then I am subtracting -6 from the last Sunday which is the previous Monday.
This will work for all days, but we need to change the condition and the number which we are adding and subtracting as per the day we required.
Result:
If you want the result to be Monday even though current date is Monday or Tuesday, then give the expression for Tuesday also in the above condition using or.
I want to get two fields: Begin date and End date of last month. For example, 14-04-2020 should give me the Begin date as 01-03-2020 and End_date as 31-03-2020. I have read the Nifi Expression language docs but all it can do with a date format is add or subtract in milliseconds. This is not helpful for my use case as the number of days in a month is not fixed and conversion to milliseconds won't help.
Is there a way to achieve my use case somehow using Nifi Expression language?
#AdarshKumar
NiFI Expression Language for this Use Case would be very clunky and unreliable for different timezones, months with <> 30 days, and leap years.
Please reference this post below which goes into detail for how to get "last month":
How to insert previous month of data into database Nifi?
In the Case of NiFi you kind of have to play with the dates to get the outcome
if you have the dates you easily convert a date and then just hard set a day to get the 1st day of the month with a hard coded day
${now():toNumber():format('yyyy-MM-01')}
to get the last day of the month you can either use the script or play with the calc using epoch time.
so to get the last day of the previous month you can just use the date and convert the day into epoch time and subtract it from the epoch date to get last day of previous month
example
${now():toNumber():format('yyyy-MM-dd'):toDate('yyyy-MM-dd', 'GMT'):toNumber():minus(${now():toNumber():format('dd'):toNumber():multiply(86400):multiply(1000)}):format('yyyy-MM-dd')}
in this example above we convert the date to epoch format it to convert again, conversion happens to remove default timestamp and then we use the same formula to get just the day as a number to multiply it with 86400 seconds in a day and multiply that by 1000 for the epoch number to subtract from the date which is then formatted back into a date.
Raw Date: Thursday, August 26, 2021 11:20:31 AM
formatted: Thursday, August 26, 2021 12:00:00 AM
epoch of formatted date: 1629936000000
Subtract Epoch: 2246400000 (86400 seconds * 26 days * 1000)
result: 2021-07-31
alteratively you could first add a month and the work back to get the current day of the given month
this example is just to give you an idea of ways you can use built in date functions with epoch time to calculate the correct date, removing the issues with months that end on specific numbers.
I try UpdateAttribute to minus month
test${now():toNumber():format('yyyyMM'):minus(1)}01
I have a query where I need, as date criteria, the week ending two months prior.
So for example if I ran the query on Monday (as of right now, the last Monday was 2/1/2016), it would look at 11/29/2015 through 12/5/2015 inclusive (Sunday through Saturday).
And then next week if I ran it, it would focus on 12/6/2015 through 12/12/2015 (Sunday through Saturday).
However I need it to return this exact same date range no matter which weekday of the week I run it. So for example the date range 11/29/2015 through 12/5/2015 would be selected if I ran it on 2/1/2016 through 2/5/2016 (Mon-Fri).
I'm not sure what the best way is to go about this. I've considered somehow trying to find the next Saturday and then clocking that back a few weeks, but there doesn't seem to be a week option in dateadd().
To get the first weekday of a week from Sunday to Saturday:
FirstWeekDate = DateAdd("d", 1 - Weekday(Date()), Date())
To go back, say 8 weeks:
EightWeeksBack = DateAdd("ww", -8, FirstWeekDate)
Then you can just add/subtrack days to get your intervals, for example.
EightWeeksBackLast = DateAdd("d", 6, DateAdd("ww", -8, FirstWeekDate))
BO Version: 12.1.0 on Infoview,
O/S: W7
I am creating a report in infoview, which is a cross-tab. I have departments on the row side and for the column I want to have all Saturday dates dynamically displayed, and this is dependent on the date prompt values I put in to the report when I run it.
So if I put in for the prompts Dec the 08th 2013 to Jan the 04th 2014 I should see 4 Saturday dates (14th/21st/28th/04th) along the column headers.
I started off using a variable and using the function relativedate, which gave me all the dates I wanted:
=RelativeDate(LastDayOfWeek([Query 1].[Episode End Date]);-1)
but because I used -1 to get the Saturday date it was giving me the Saturday before the earliest prompt date, so I was getting these dates instead:
(07th/14th/21st/28th/04th)
Is there a way I can get these dates but ignore the previous day (the 7th) before the start prompt date?
I want to have this dynamic so that if I put a date range in it shows me all the saturday dates within that range along the top of the report regardless of the date range period.
Andrew
The reason you're having trouble is that WebI (being ISO compliant) considers a week to run from Monday to Sunday, but your reporting week ends on Saturday.
So, I would approach it this way:
=RelativeDate(
LastDayOfWeek(
RelativeDate([Query 1].[Episode End Date];1)
)
;-1)
If we evaluate some dates with this logic, we'll see the desired result:
Testing 12/8 (Sunday):
Add one day = 12/9
Get Last Day Of Week = 12/15
Subtract one day = 12/14
Testing 12/12 (Thursday)
Add one day = 12/13
Get Last Day Of Week = 12/15
Subtract one day = 12/14
Testing 12/14 (Saturday)
Add one day = 12/15
Get Last Day Of Week = 12/15
Subtract one day = 12/14
I'm at home and don't have access to WebI right now, so I can't test this myself, but the logic should be sound.
is there a quick way to nest an if (IIF?) in an UPDATE string to change all dates to the appropriate monday day of that week? So, if a date read TUESDAY 30 JULY, i would want it to be automatically updated to MONDAY 29 JULY. I figured an UPDATE TABLE, but unsure where to go from there. Thanks!
You need something similar to this:
UPDATE tablename SET fieldname = fieldname-Weekday(fieldname,2)+1
Weekday() returns a number 1..7 indicating the day of the week. By default, 1 is Sunday. The argument 2 says to starting counting from Monday, so Monday is 1 and Sunday is 7.
So, for example, if the field's date is a Monday it subtracts 1 then adds 1, remaining Monday; if it's Tuesday it subtracts 2 (0) then adds 1 = 1, Monday.