Can I modify the following query to make it more concise way using Django ORM?
queryset.filter((Q(from_date__lte=from_date) & \
Q(to_date__gt=from_date)) | \
(Q(from_date__lte=to_date) &
Q(to_date__gt=to_date)))
Here are some examples:
"from_date": "2014-05-11 08:00:00",
"to_date": "2014-05-11 10:00:00",
"from_date": "2014-05-12 12:00:00",
"to_date": "2014-05-12 15:00:00",
These are the dates in the database
If you submit
fromDate=2014-05-11 08:00:00
toDate=2014-05-11 13:00:00
should give me only the first date
"from_date": "2014-05-11 08:00:00",
"to_date": "2014-05-11 10:00:00",
Here again, should give me only the first result
fromDate=2014-05-11 09:00:00
toDate=2014-05-11 13:00:00
If I submit the following date
fromDate=2014-05-11 07:00:00
toDate=2014-05-11 09:00:00
Again, only the first result should be taken
If you do so you can get both results
fromDate=2014-05-11 09:00:00
toDate=2014-05-12 13:00:00
I hope you understand what's the idea.
The query should remain the same logic!
queryset.filter(to_date__gt=from_date, from_date__lt=to_date)
That is, to be returned the event must end after the new event starts and must start before the new event ends. Anything that ends before the new from_date is not returned, neither is anything that doesn't start until after it ends.
That does not precisely match your original query, but it does match the behavior described in the comments for when there's a record in the database that is entirely within the range set by the new dates.
Related
All, I am trying to isolate the time portion only of a date/time stamp and then create a case statement to populate another column with data. This is how the date/time stamp looks: 2022-03-25 04:00:00
Here is the code I originally wrote but when I just use the hours portion I get an error in PowerBi saying invalid date/timestamp...
case when create_tmstp between '04:00:00' and '07:30:00' then 'N' else 'D' end as Day_Night,
From the looks of it, you're using SQL, not DAX. I say that, because you've tagged this with dax and not SQL.
Problem is that you're not showing us what create_tmsp is, or what version of what kind of sql server you're using. So, just to make it easier, this is an working example to use for instance inside SSMS. This uses getdate() to get the current datetime, and then converts it into just showing what hour it is (0-24), then there is a case when that checks which intervall it falls under. You can use this, and simply replace getdate() with the column you wish to use if you're using a server that accepts this syntax.
select
DATEPART(HOUR,getdate()) AS 'HOUR',
getdate() AS 'Datetime',
CASE WHEN DATEPART(HOUR,getdate()) BETWEEN 9 AND 16
THEN 'Day'
WHEN DATEPART(HOUR,getdate()) BETWEEN 17 AND 24
THEN 'Evening'
WHEN DATEPART(HOUR,getdate()) BETWEEN 0 AND 4
THEN 'Night'
WHEN DATEPART(HOUR,getdate()) BETWEEN 5 AND 8
THEN 'Morning'
END AS 'Label'
Example result:
Hour
Datetime
Label
0
2022-04-04T00:18:13.203Z
Night
Fiddle here:
http://sqlfiddle.com/#!18/56f78/3829
I have only one table named 'Data' in my PowerBI Desktop model, which contains [Datetime], [Type], [Name], and several other columns.
I choose a specific Type="B" by clicking on a graph, and a period of time with a slicer on Datetime, and then use the measures below to calculate how many different names there are in Type "A"(as expected, there should be no record with A and B at the same time).
That's when I'm totally confused by the result:
(My PowerBI version is the latest release in April,2019.)
Wrong =
CALCULATE (
DISTINCTCOUNT ( Data[Name] ),
FILTER ( ALL ( Data[Type] ), Data[Type] = "A" )
)
While this measure turns out to be the right one:
Correct =
CALCULATE (
DISTINCTCOUNT ( Data[Name] ),
FILTER ( Data, Data[Type] = "A" )
)
What I think is happening (probably missing something important):
Since the two measures share the same explicit filter context outside CALCULATE, and what they finally calculate are the same, the only problem would be "what's the final context provided by FILTER"?
With the Correct version, FILTER simply takes the (Type="B" + Datetime) subset, try to find rows with Type "A" inside the subset(Type="B" + Type="A" + Datetime), and it just fails. So FILTER gives nothing to final calculation, which turns out to be blank as expected(there should be no A Type record when I choose Type B).
With the Wrong version, FILTER (with only one column) ignores all filter context on Type(originally "B"), then it applies a new one (Type="A") to replace the original one. And since each column is filtered separatedly, the filter on Datetime does not change at all. So the final context taken by CALCULATE should be a subset which contains Type "A" and the selected period of time at the same time(Type="A" + Datetime), which makes the final result "the number of distinct name of Type A during the time", having nothing to do with what Type I chose at first.
But the thing is, according to the strange result it gives, [Wrong] does absolutely not what I'm thinking of, and I have no clue on it. I've tried many ways I think reasonable to test how it works, but they just fail...
Thank you for any advice!
I made a tiny pbix file for test(with the same structure and problem):
https://pan.baidu.com/s/1gNZDNlICFLkMdPpPArb8cQ
If needed, use yf7f to download it.
The FILTER function takes a table as its first argument. This table is evaluated within the filter context you are operating in.
In the Correct version, that means when you pass in Data, that table is filtered on Type = "B" and Datetime according to your slicer selections. You then add the condition Data[Type] = "A" which is not true for anything in the filtered Data set since you already picked "B" for the type. Therefore, it returns blank since the table is empty.
Edit: Scratch what I said before and look at an example. Start with this as a full table:
Name Type Datetime
Alex A 1/3/2019
Alex A 1/4/2019
Bob A 1/5/2019
Bob B 1/5/2019
Bob A 1/7/2019
Carla B 1/3/2019
Carla B 1/4/2019
Dan A 1/6/2019
If I slice on type B and dates 1/3/2019 - 1/5/2019, here are the remaining rows:
Name Type Datetime
Bob B 1/5/2019
Carla B 1/3/2019
Carla B 1/4/2019
When we calculate ALL( Data[Type] ) within this context we get the following table, which is the same as if you remove the Type slicer but keep the date slicer:
Name Type Datetime
Alex A 1/3/2019
Alex A 1/4/2019
Bob A 1/5/2019
Bob B 1/5/2019
Carla B 1/3/2019
Carla B 1/4/2019
Now when you add the Data[Type] = "A" condition you get this table, which is the same as if you had originally filtered on A instead of B (and keep the date slicer), you get the following:
Name Type Datetime
Alex A 1/3/2019
Alex A 1/4/2019
Bob A 1/5/2019
This clearly has two distinct names instead of none. In the Correct version, the difference is that you filter for type A on the 2nd table above instead of the 3rd.
Basically, the ALL undoes the selection of type that you chose with the slicer.
Note: What I said before about indirectly affecting things isn't what's happening here. That's a concern when you are doing a context transition from row context to filter context, but doesn't apply here. Sorry for the confusion.
Finally, I've found the reason why it is happening!
It is called the overwrite of arbitrarily shaped filters.
The complete explanation can be found on "Definitive Guide to DAX", chapter 10, page 439-443.
Just be careful to use more than one filter on two or more columns of the same table, which is the easiest way that may cause the problem!
We have users replying on an e-mail and we need to retrieve the date mentioned in the table.
For the moment I made the following code to do that for us:
$temp = ($Mail.Body.Text -creplace '(?m)^\s*\r?\n' -split "User name`r`n`tLogon name`r`n`tEnd date`r`n`tNew end date")[1]
(($temp -split "`r`n`t")[3] -split "`r`n")[0]
As you can see, it's quite long and very much relying on -split. Is there an easier way to retrieve the date? With Select-String or a Regex or something?
The table format in the e-mail is always the same, as the users needs to fill in the date in the HTML table. So we need to be able to read that table. In the example below the result would be 01/08/2016.
Example of such an e-mail:
Date added below
From: GBR Service Desk
Sent: Wendesday 29 juni 2016 7:00
To: Bond, James (London) GBR
Subject: REMINDER Expiring user:
Importance: High
Dear James
This is a reminder e-mail to inform you that the following Windows user account will expire soon:
User name
Logon name
End date
New end date
Smith, Jobn (Manchester) GBR
jent
01/07/2016
01/08/2016
Because you are registered as the manager of this user, we would like to ask you to verify if this account is still nee
ded after its end date. More text...
Yours sincerely
Thank you for your help.
You could use Select-String to find date-like patterns and select the last one:
$EndDate = ($Mail.Body.Text |Select-String '\d{2}/\d{2}/\d{4}' -AllMatches).Matches[-1].Value
I have a time field in my database. I want to have a time select field to select the time as three drop downs, for a 12-hour clock. One drop down is to select the hours from 1 to 12, the second drop down is to select the minutes from 1 to 59, and the third drop down is to select the AM or PM, for a single time field column in database. I have searched for this kind of time helper, but I did't find one. Is there any way to customize the time select helper to select the time format in this way in Rails?
There is not a built-in Rails helper that will add a separate select field for choosing between AM/PM. However, the time_select helper will allow you to select time in hours, minutes, AM/PM as you asked. This would most likely also be the suggested "Rails way" of accomplishing this. You can use the :ampm option to display hours as "12 PM", "01 AM", etc.
time_select 'game', 'game_time', {:ampm => true}
See the time_select documentation page for more info.
I am using the graph-api to fetch data for a specific query.
I am using the value of "next" field for pagination.
Suppose
The link is http://graph.facebook.com/search?q=blade&type=post&limit=300&until=1322857230
This page has 2 links
"previous" and "next"
Now when i hit the previous link, i am getting results having latest time stamp.
How is this possible? Previous link should have data with older time stamps.
That previous link has the updated data bcoz you are seeking the "blade" keyword until 2nd December so first page is getting the data from 2nd December and the previous one is getting the updated data , so on . But if you use http://graph.facebook.com/search?q=blade&type=post&limit=300&until=1323167000 then its searching until 6th December so there is no updated date for 6th December , thats why its previous page is blank .
Example :
Suppose there are 2000 post for "Blade" keyword from 2nd dec , So your query is returning the first 300 results starting from 2nd dec , lets say there are 500 results from 2nd December so in first page you are getting 300 and in previous page you will get rest of the 200 and another 100 result from 3rd December .
Make senses ?