I am using a QCalendarWidget in my application, and I overloaded the updateCells method to put a red background on every dates which meet certain conditions.
My problem is I don't know how to get the first date displayed in the calendar (not the first date in the month), and the last date displayed.
Example: in february, the first date displayed is january the 25th, and the last date displayed is march the 7th.
There isn't any useful method in QCalendarWidget and I can't think of an algorithm for this.
Do you have any idea how to do this ?
As you've got access to the currently shown month and year, you can use the QDate::dayOfWeek on the first and last date of the shown contents. Taking QCalendarWidget::firstDayOfWeek into account, you aught to be able to decide how far back and forth you have to go.
QCalendarWidget class is a combination of more simple widgets, you can see Qt source code for help. Just use loop to get all dates between:
class myQCalendar(QCalendarWidget):
"""custum QCalendarWidget"""
def __init__(self, parent=None):
self.table = self.findChild(QTableView)
def return_first_last_dates(self) -> Tuple[QDate, QDate]:
# first row(0) and col(0) - headers, so we use second(1,1)
first_date = self.date_by_index(
self.table.model().index(1, 1))
last_date = self.date_by_index(
self.table.model().index(6, 7))
return first_date, last_date
# self.table.model().dateForCell(1,1) didn't work in python
# maybe it will work in c++
def date_by_index(self, index: QModelIndex) -> QDate:
""" Return QDate by index of model of QTableView """
date = self.selectedDate()
day = int(index.data())
mnth = date.month() # current month always the same as current date
if day > 15 and index.row() < 3: # qcalendar always display 6 rows( and 0 - is header)
mnth = date.month() - 1
if day < 15 and index.row() > 4:
mnth = date.month() + 1
return QDate(date.year(), mnth, day)
Related
I am very new to the PowerBI community and I am confused about how to visualize and create measures/columns for the data which requires comparing last week/month/year data with respect to the current week.
I have tried various solutions available on the internet or other forums. I would appreciate it if anyone can please outline the steps required to achieve the goal.
The data that I have is transactional data and I have also created a Date Table. I am not sure how to go ahead with the problem.
You can create measures like this (for days):
PreviousDay =
var __DayOnRow = SELECTEDVALUE(Calendar[day])
return
CALCULATE( SUM(Table[SomethingToSum]), FILTER(ALL(Calendar),Calendar[day] = __DayOnRow -1 ))
How this work:
SELECTEDVALUE gets a specific day from the current context
__DayOnRow -1 give us a previous Day (not yesterday date< except for today date>)
FILTER with ALL, remove every filter on Calendar (current row is also a filter, so without removing filter we get two excluding conditions )
How do that for WEEK?
PreviousWeek =
var __WeekOnRow = SELECTEDVALUE(Calendar[Week])
var __FirstDayOfWeek = calculate(min(Calendar[Day]), FILTER(ALL(Calendar), __WeekOnRow = Calendar[Week] ))
var __LastDayOfWeek = calculate(max(Calendar[Day]), FILTER(ALL(Calendar), __WeekOnRow = Calendar[Week] ))
return
CALCULATE(SUM(Table[SomethingToSum]), FILTER(ALL(Calendar),Calendar[day] >= __FirstDayOfWeek -7 && Calendar[day] <= __LastDayOfWeek -7 ))
I`m trying to populate values in column F, "Product Sold Date" in "TABLES" tab
Basically... the logic is as follows:
1) If Column C (Product Status) = "Paused", then return "Paused"
2) If Product start date = NULL or Product end date = NULL, then return NULL
3) If Product start date < today`s date, then return "No Data"
4) If Product start date >= today`s date, return "Upcoming"
5) If product End date <= today`s date, return "Ended"
6) If product start date <= today`s date, return "In Market"
7) If the condition does not belong to any of the above cases, then return the actual Product launch dates
Below is the link to the sample data I`m working on..
I`m pasting the link itself, becaues there are multiple tabs included
https://docs.google.com/spreadsheets/d/120rHOt8Pa_PdMKPgLkSYOKTO2Cv1hBH6PpTrg7FfJLk/edit?usp=sharing
Ultimately, I need to populate the actual "Product Launch Date" by scanning data in each tab
I tried using nested if statements with combination of Index Match.
But I`m totally lost in case of multiple tabs
Can anyone please provide suggestion on this?
Should we think about using query statements instead in this case?
Sidenote: The returned values will be mix of dates and character
[In market/ Ended/ Upcoming/ No Data/ NULL/ Paused/ Actual Date]
=ARRAYFORMULA(
IF(C2:C="Paused", C2:C,
IF((A2:A="")+(B2:B=""), ,
IF(A2:A >= TODAY(), "Upcoming",
IF(B2:B <= TODAY(), "Ended",
IF(A2:A = TODAY(), "In Market",
IF(E2:E<>"", IFERROR(VLOOKUP(D2:D&E2:E,
{'Eaton Centre'!A2:A &"Eaton Centre", 'Eaton Centre'!B2:B;
'Yorkdale Mall'!A2:A&"Yorkdale Mall", 'Yorkdale Mall'!B2:B;
'Vaughan Mills'!A2:A&"Vaughan Mills", 'Vaughan Mills'!B2:B}, 2, 0)), )))))))
Your formula would be
=IF(C2="Paused",C2,if(OR(A2="",B2=""),"",IF(A2<TODAY(),"No Data",IF(A2>=TODAY(),"Upcoming",IF(B2<=TODAY(),"Ended",IF(A2<=TODAY(),"In Market","Actual Product Launch dates"))))))
In the above formula, you should be using a Query formula in place of "Actual Product Launch dates", to extract the actual date.
But the points 3 & 6 don't make any sense. The 6th condtion should be If product start date = todays date, return "In Market"
I am trying to pass a date into Oracle 2 different ways when running Python code.
Be able to pull the first day of last month and the last day of last month automatically from datetime.date. I found this code online, but not sure that it will produce the first day of last month and the the last day of last month.
import datetime
today = datetime.date.today()
first = datetime.date(day=1, month=today.month, year=today.year)
lastMonth = first - datetime.timedelta(days=1)
In case of situations that I will need to manually enter the date for prior dates, etc. I want to be able to manually enter a beginning and end date. First off is there a way to have Python prompt me to enter start date and end date? Secondly, I can't seem to get my code to work correctly when I try to put the dates in my code.
Import dateutil.parse
start = dateutil.parser.parse('06/01/2015')
end = dateutil.parser.parse('06/30/2015')
startdate = start.strftime('%d-%b-%y')
enddate = end.strftime('%d-%b-%y')
cursor = con.cursor()
cursor.execute(SELECT * From Executives WHERE DateAdded >= %s And DateAdded <= %s), (startdate, enddate)
I have even tried DateAdded >= :s and DateAdded <= :e. but I am still getting an error.
I have a list of events but I'm struggling to work out how to show specific date ranges in the index view.
I would like to list the events by showing events today, this week, this month etc.
I'm new to rails so I've tried to use this site and I've come up with the following which works for today's events.
#events_today = Event.find(:all, :conditions => ["date between ? and ?", Date.today, Date.tomorrow])
But I'm not sure how to set the page to automatically update and show only this weeks events and this month.
Your basic query should do something like this:
Event.where(date: date_range)
Now before calling this query you can set the date range variable. If you only want this week:
date_range = Date.today.beginning_of_week..Date.today
Event.where(date: date_range)
Now there are all sorts of things you can do. You can select a start and end date or a custom period using either a form or a dropdown select. In this case date_range is set based on your params. You could also always use one predefined period.
If you want to work with several date range periods it could be nice to have a last_week, last_month, etc. scope in your model (or concern). Or you could simply define date_range constants in your initializers.
As per my understanding, you want to show all the event of a week or month on click of tab 'Week' or 'Month' from view.
When you clicked on month or week for getting events, you send simply month or week in params(assuming params[:events_in] hold 'week' or 'month')
apply check on this attributes of params
def get_events_in_week_or_month
if params[:events_in] =='week'
start_date_of_time_period = Date.today.beginning_of_week
end_date_of_time_period = Date.today.end_of_week
else
start_date_of_time_period = Date.today.beginning_of_month
end_date_of_time_period = Date.today.end_of_month
end
#events in descending order
#events = Event.where("date between ? and ? ", start_date_of_time_period, end_date_of_time_period).order("created_at DESC")
#events in ascending order
#events = Event.where("date between ? and ? ", start_date_of_time_period, end_date_of_time_period)
end
for getting more methods of date class, you should run following command on rails console :
Date.public_methods
Does anyone know how I can sort (in this instance date) my django query set against todays date ?
class Person(models.Model):
name = models.CharField(max_length=50)
date = models.DateField()
My goal is to list the name and date entries. At the top of the list will be the entry with the date that is closest to todays date (day/month).
You can use extra queryset method to select additional data from database table.
This is example that works with MySql:
Person.objects.extra(select={
'datediff': 'ABS(DATEDIFF(date, NOW()))'}).order_by('datediff')
DATEDIFF - returns difference in days bewteen two dates,
ABS - returns absolute value. For sqlite, there is different syntax, see this answer.
EDIT: use current year
Person.objects.extra(select={
'datediff': "ABS(DATEDIFF(CONCAT(YEAR(now()), '-', MONTH(date), '-', DAY(date)), NOW()))"}
).order_by('datediff')
EDIT 2: optimized *
from datetime import date
dayofyear = int(date.today().strftime("%j"))
datediff = 'LEAST(ABS(DAYOFYEAR(date) - %d), ABS((366 - %d + DAYOFYEAR(date))) MOD 366)' % (
dayofyear, dayofyear
)
Person.objects.extra(select={'datediff': datediff}).order_by('datediff')
EDIT 3: closest date after given (todays) date
from datetime import date
dayofyear = int(date.today().strftime("%j"))
datediff = '(DAYOFYEAR(date) - %d + 365) MOD 365' % (
dayofyear
)
Persion.objects.extra(select={'datediff': datediff}).order_by('datediff')
If you want to sort based on date, you can order as: .order_by('date') on a result queryset.
I'm not sure if that answers your question. In case you mean you want to select only the Persons with date of today, you can use:
import datetime
now = datetime.datetime.now()
persons_with_date_today = Person.objects.filter(date=now)