Get entry by week number using Django ORM? - django

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 ;-)

Related

How to sort , filter and display model data using week number in django

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)

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

Power BI - filter data to current week (week number)

I have a power bi report with a line chart that has a field called 'EventDate'
I need to add a filter on to this report to say 'only get me the days where the EventDate matches this week number'
I don't want to display the last 7 days as a relative filter.
I need to do it by this week.
Can anyone help
You can use the week in the relative date filtering and it returns the dates associated with the current week number rather than the last 7 days.

How to MTD, YTD in Apache Superset?

I would like to create a dashboard with month to date (MTD) and year to date (YTD) charts. However, I do not want to update my date range each month. A fixed date range would do this. Choosing 1 months ago give the last 30days. Using last month gives me last month and everything from this month. Yet, this month is not supported. MTD doesn't work either. I am using Superset version 0.24.0
I figured it out! Use 1st for MTD and Jan 1st for YTD.
For newer versions of Superset, this quarter is supported for quarter to date (QTD) charts.
I think it can be set on the custom tab of the time range in the chart explorer. There you can use 1st and Jan 1st as strings that are parsed.
TO get MTD put 1st in the 'Start' field and put this month in the 'end' field
Select Range Type as "Advanced", then enter "1st" for Start Date and leave End Date blank. Image has today's date as End Date but leave it blank to get YTD
TIME RANGE dialog showing YTD entries

Django Query same week last year

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.