Filtering rows in NSPredicate by comparing 2 dates within the row - nspredicate

Records in my database contain 2 NSDate fields.
At creation of the record both date fields are identical.
Using the app moves one of the two date fields to current time of day.
I want to construct an NSPredicate clause that will only return rows where Date1 is younger than Date2.
All the examples I've seen so far compare dates to externally supplied values as in:
" > %#", refDate
What I'm after is the construction that equates to:
"[Date2 timeIntervalSinceDate:Date1] > 0.0". Naturally, this won't work.
Any help or pointers to existing examples will be gratefully received.

OK! I need therapy! I can use a block to execute exactly the predicate condition that I highlighted in my original question! Consider this one closed!

Related

How to compare columns in a new column

I have a recordset where rows have a date field.Rows are from current year and last year. I want to see the count of rows per year. That's easy. Now I'd like to have a third column with difference (count(currentYear) - count(lastYear)). Is there any way to achieve this?
thanks
It seems like you want to have the difference calculated between two members of the same date field.
If so, you might want to consider the customizeCell() API call to retrieve the count values of each year, use them to calculate the difference, and modify the necessary cells with the result.
Alternatively, you could try modifying your data set so that the lastYear and currentYear are two different fields – this, for example, would allow you to compare them within a calculated value.

Sorting date as 2nd criteria gives wrong order

The date is in the correct format ie YYYY-MM-DD. If I sort just on date it sorts to correct order however if i sort on another field first with date the second criteria it is sorted correctly on first criteria but the date is sorted by DD first MM second and YYYY last. Out of curiosity I tried the same file in Excel which sorted correctly. Can anyone explain the difference and how to get a correct sort in Calc? Using 2 criteria in Calc does not work. I tried sorting on date first and species second. Date order was correct but species were jumbled.
The data is a spreadsheet download of an online data base where we record squirrel and other species sightings in the forest. The entries are not in date or species order. There are many fields for each entry but I am trying to sort first to species and then each species to be in date order. It is for an animation using Time Manager.
I have re-asked the question with edit because the question was closed, for what reason I don't know.
The sort does work correctly, tried again and all sorted as it should. Tried to reproduce strange date order but can't. Have no idea what went wrong originally.

Sorting of Month Columns Beginning with wrong month

I have a problem with sorting my columns in my matrix by month order. I have read many, many posts on problems where the results are alphabetical but none where it starts at the wrong month.
I am using a calculated field called MonthSort using expression:
=FORMAT(Fields!createdon.Value,"yyyyMM")
And I have then Sorted by MonthSort in the Column Group (Group Properties) Sorting Option.
This is something that I have used frequently and in the past it has always sorted from Jan to Dec correctly. However this time it is sorting from November to October.
In my query I am pulling data with the following WHERE clause and I am wondering if this is affecting the starting month.
AND (createdon BETWEEN '2016/11/25' AND DATEADD(minute, - 1, #EndDate + 1))
Any help would be gratefully appreciated.
Out of curiosity, what happens if you change the sort expression to =FORMAT(Fields!createdon.Value,"MM")
Your sort expression
=FORMAT(Fields!createdon.Value,"yyyyMM")
gets you data in the format 201611, 201612, 201701,... so the sort works correctly.

Can I compare values in the same column in adjacent rows in PowerPivot?

I have a PowerPivot table for which I need to be able to determine how long an item was in an Error state. My data set looks something like this:
What I need to be able to do is to look at the values in the ID and State columns, and see if the value in the previous row is ERROR in the State column, and the same in the ID column. If it is, I then need to calculate the difference between the Changed Date values in those two rows.
So, for example, when I got to row 4, I would see that the value in the State column for Row 3, the previous row, is ERROR, and that the value in the ID column in the previous row is the same as the current row, so I would then calculate the difference between the Changed Date values in Row 3 and Row 4 (I don't care about the values in any of the other columns for this particular requirement).
Is there a way to do this in PowerPivot? I've done a fair amount of Internet searching, and it looks like if it can be done, it would use the EARLIER or EARLIEST DAX functions, but I can't find anything that tells me how, or even if, this can be done.
Thanks.
Chris,
I have had similar requirements many times and after a really long time of trial-and-error, I finally understood how EARLIER works. It can be very powerful, but also very slow so always check for the performance of your calculations.
To answer your question, you will need to create 4 calculated columns:
1) Item Rank - used for ranking the issues with same Item ID
=COUNTROWS(FILTER('ID', EARLIER([Item ID]) = [Item ID] && EARLIER([Date]) >= [Date]))
2) Follows Error - to easily find issue that follows EROR issue
=IF([State] = "EROR",[Item Rank]+1)
3) Time of Following Issue - simple lookup so that you can calculate the different
=IF([Follows Error]>0,
LOOKUPVALUE([Date], [User], [User], [Item Rank], [Follows Error]),
BLANK()
)
4) Time Diff - calculation of time different for the specific issue
=IF([State]="EROR",
DAY([Time of Following Issue])-DAY([Date]),
BLANK()
)
With those calculated columns, you can then easily create a powerpivot table, drag State and Item Id onto the ROWS pane and then simply add Time Diff to Values. You will get an overview of issues that contain string "EROR" issue and the time it took to resolve them.
This is what it looks like in PowerPivot window:
And the resulting Pivot table:
You can download my Excel file here (2013).
As I mentioned, be careful with the performance as the calculated columns with nested EARLIER and IF conditions might be a bit too performance-demanding. If there is a smarter way, I would be very happy to see it, but for now this works for me just fine.
Also, keep in mind that all calculated columns could be nested into 1, but I kept them separated to make it easier to understand the formulas.
Hope this helps :-)

How can I select entries for a given weekday using SQL?

I could use this query to select all orders with a date on a monday:
SELECT * from orders WHERE strftime("%w", date)="1";
But as far as I know, this can't be speed up using an index, as for every row strftime has to be calculated.
I could add an additional field with the weekday stored, but I want to avoid it. Is there a solution that makes use of an index or am I wrong and this query actually works fine? (That means it doesn't have to go through every row to calculate the result.)
If you want all Mondays ever, you'd need a field or sequential scan. What you could do, is calculate actual dates for example for all Mondays within a year. The condition WHERE date IN ('2009-03-02', '2009-02-23', ...) would use index
Or as an alternative to vartec's suggestion, construct a calendar table consisting only of a date and a day name for each day in the year (both indexed) and then perform your query by doing a JOIN against this table.

Resources