I need to compare sums of events in the same week (isocalendar), year over year.
class MyModel(models.Model):
date = models.DateTime(...
hits = models.IntegerField(...
I've got this sorted for last year, last month, same month last year, same day last year using variations on:
same_month_last_year = MyModel.objects.filter(date__month = (datetime.datetime.now().month), date__year = (datetime.datetime.now() - relativedelta(years = 1)).year).aggregate(total=Sum('hits')['total']
I don't see an equivalent 'week' function. I can use .isocalendar()[1] on the right side of the equation but that's of no help on the left. Any ideas? Thanks.
Django 1.11 added a week filter. Can you use that?
If not, you might be able to look at the source code and cherry pick it.
As a last resort, you could use Python to calculate the date range for the same week last year, then pass those dates as filters to the Django query.
Related
Basis on below data I want to add a calculated column with Week Description
I've done it in excel by typing it manually.
Also my week is starting from Thursday and ends on Wednesday hence I've Used this function to get the weekday WEEKDAY('Calendar'[date],14)
Requesting you to help me with a dax code which can be used to create a new calculated column with week information as shown below in third column.
The logic would be : If the date is current week then the value will be "This Week" else if the
date is in last week then "This Week -1" else if the date is in last to last week then "This Week - 2" and so on.
The weekday can be calculated as following:
Weekday = WEEKDAY([Date] + 3)
We do a shift of 3 days to make Thursday the start of the week
Next, we get the WeekDesc in two steps, frist we calculate the difference between now and the date in weeks and as second step we use an if statement to create the correct sting (and logic).
WeerDesc =
var weeksPast = DATEDIFF(now(), [Date] + 3,WEEK)
return if ( weeksPast = 0, "This Week", "This Week" & weeksPast)
As you can see you can use variables in your DAX, I would recommend using them to keep the overview.
Enjoy
We can do this also in this way (as a measure if you need only label to display on rows):
DayOfWeek = WEEKDAY(SELECTEDVALUE('CAL'[Date]),14)
DiffToToday = DATEDIFF(SELECTEDVALUE('CAL'[Date]),TODAY(),DAY)
Label = CONCATENATE("This Week", (DIVIDE( CAL[DiffToToday] + CAL[DayOfWeek],7) -1) *-1 )
Of course, we can do all steps in one measure.
I would like to sort my model's data on a week number for example when user logins user will see current week number and associated data and user should be able to select previous week number and get all past data as well also week starts on Sunday and ends on Saturday (considering 1 week)
Select Week :[2021 - 30 ] <- week number
You can filter it such way:
from datetime import datetime, timedelta
itemsList = Model_Name.objects.filter(user=request.user, created_date__gte=datetime.now()-timedelta(days=7))
I have got similar problem in my task and I solved it in simple steps:
#django #django-models #django-views #problem-solving
def get_weeks(request):
week = now().isocalendar()
if Event.objects.filter(start_date__week=week.week).exists():
instances=Event.objects.filter(start_date__week=week.week)
How would I go about setting up a rolling 12 month period in Django without any year.
I am trying to plot the academic year but the problem I am finding mainly due to my lack of knowledge is that I have to set the year in datetime
I would like to try and create a start and end date for the academic year which runs from the beginning of April to the end of March every year however I have to include the year in all the objects such as datetime.
Is there a way for me to set a start and end date regardless of the year, the only way I can think of so far is to take the end of March as a start date and then count 12 months from then.
As always, thank you for the help.
timedelta might be help full. check out documentation
import datetime
start_date = datetime.datetime.now()
end_date = start_date + datetime.timedelta(days=365)
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
I tried something like:
MyModel.objects.filter(year__week=1)
It doesn't work. For now, I calculate the first day and the last day of the week and then use gte and lte, but it's less than efficient given that SQL comes with a Week function.
It seems that django DateField doesn't have week attribute, as weel as date object from Python. They have both year and month and also day, but not week attribute. I guess you need to translate week to day range and file a ticket on django website ;-)