I am trying to create a query on a table that filters out dates. I need to display all records previous to a specific date, but need this to be dynamically updated annually.
Specifically, I need to find dates prior to October 1 of the previous year. (i.e. anything prior to Oct 1, 2012.)
I know I can use '<#10/1/2012#' but would like to make this dynamic to update every year, as this same function will be utilized for multiple queries.
I have tried several iterations of the Date() function to get this to work, to no avail.
Things that don't work are...
Year([TrainingDate])
[TrainingDate] < DateSerial(Year(Date())-1,10,1)
Date() is the current date, so Year(Date()) is the current year.
Related
I´m creating a static reports for every month for every year from our ERP database, contract table. I've managed to get everything right but now I have to alter the monthly queries every year. Currently I use this to separate the months:
WHERE [Ending Date] >= '04.01.2021' and [Starting Date] <= '04.30.2021'
As said the output is right but what I want is the query to use system year, so only dates are input in the query and therefore when the year changes, it would change in the query automatically.
The question isn't too clear, but what I vaguely understood is you want to get the year dynamically (not sure if you want just the year or the whole date) but you can make use of YEAR() function (something like YEAR(CURDATE()) could get you the year of the current date). You can also take a look here for more useful functions.
I couldn't form a query or tell you what functions to exactly use because I doubt I'm fully understanding. Some functions you may make use of are PERIOD_ADD() and TIMESTAMPADD().
I am working on an SSRS report that gets its data from an OLAP cube. In the OLAP cube I have a field named WeekOfYear which gives me the week number of the year based on the date. For example, week 1 for January 1st (if January 1st falls on a Monday) and week 2 for January 8th. My data is grouped by this field but now I want to be able to compare the data from this week of the year to the previous year's week of the year. Like comparing Week 1 of 2015 to Week 1 of 2014. Is there anyway that I can accomplish this? I appreciate any help. Thanks.
There is a LookUp function that should be able to do what you need.
You mention you have a WeekOfYear field. This also assumes you have a Year field (or can calculate it with **YEAR(Fields!YouDateField.Value) )
=LookUp(Fields!YourYearField.Value - 1 & "|" & Fields!WeekOfYear.Value, Fields!YourYearField.Value & "|" & Fields!WeekOfYear.Value, Fields!YourValueField.Value, "YourDataSet")
If you are actually summing multiple rows of data from your cube, you would need to use LookUpSet to get all the values and sum them with a custom function (since Microsoft couldn't possibly envision users wanting to SUM multiple records). Luckily users have already created a function - SumLookup. See How to combine aggregates within a group with aggregates across groups within SSRS if needed.
If you have access to the OLAP cube and can edit this, you could define a new Calculated Measure on the cube. How exactly this would work depends on the set up of your date hierarchies. You can also access this and define calculated measures through the Query Designer while constructing your dataset in Report Builder/Visual Studio.
Right-click in the cube browser and choose "New Calculated Member".
Reporting on weeks across years can be difficult due to the fact that the number 7 doesn't fit neatly into 365 or 366, so you always end up with a little over 52 weeks. Since the 1st of January could be a Sunday one year, and a Tuesday on the next (2012/2013), it's not always a good idea to directly compare these. So people may work around this by defining the 7-day weeks for the year against their date dimension. One year you may have 52 weeks, another you'd have 53. This is a little off topic, so I'll link to an explanation of this here, but it is important to be aware of this in order to implement my suggestion below.
Assuming you have a nice hierarchy on your date dimension that can aggregate up Weeks to Year level, you can create a new measure in your cube using the ParallelPeriod function.
[Measures].[SalesSPLY] AS
(
ParallelPeriod
(
[Dim Date].[ReportingCalendar].[ReportingYear],
1,
[Dim Date].[ReportingCalendar].CurrentMember
),
[Measures].[Sales]
)
My example MDX assumes that you already have a hierarchy called ReportingCalendar created on your date dimension. Yours may be named differently.
Now if you browse your cube and select WeekOfYear, Sales, and SalesSPLY, you will see your value for this year's week 1, alongside last year's.
OLAP cubes are very good at this type of time-based intelligence, as they can very quickly provide aggregated and offset data in a way that would be slower to run in an RDBMS or within SSRS itself.
I've looked on a couple different forums and I'm unable to find what I need they all have it listed in SQL View, and that is NOT what I want. I would like to do this in the Query Design as it is much easier for me.
I simply would like to make a query to display certain parameters of the database I maintain. And those parameters would equal last month's data.
I previously was able to successfully make a query displaying all information for the past year but can't figure out how to display just this past months.
The past year Criteria:
>DateAdd("yyyy",-1,Date())
Since that worked I tried doing this but it would not work for me:
>DateAdd("mmmm",-1,Date())
I'm sure it's something simple that I'm just not seeing here. Any help or recommendations are welcome.
Referencing the link provided by Fionnuala I have come up with
>DateAdd("m',-30,Date())
The problem being is that it queries all results for the past 30 days from Today's date. I wish to display only data from October ! While this can be done easily. I don't wish to manually go in this query every month and change certain parameters. I would like it to be automatic so or next month December I click on the query and it displays all 30 days of November's data. And there is no manual process of going back in and changing any of the criteria.
In the Query Designer you can use a Criteria: like this
>=DateSerial(Year(Date()),Month(Date())-1,1) And <DateSerial(Year(Date()),Month(Date()),1)
The corresponding SQL statement is
SELECT Donations.*
FROM Donations
WHERE (((Donations.DonationDate)>=DateSerial(Year(Date()),Month(Date())-1,1)
And (Donations.DonationDate)<DateSerial(Year(Date()),Month(Date()),1)));
If it was run on November 18, 2014 then it would effectively be
SELECT Donations.*
FROM Donations
WHERE (((Donations.DonationDate)>=DateSerial(2014,10,1)
And (Donations.DonationDate)<DateSerial(2014,11,1)));
Notes:
This query should be sargable and take advantage of an index on the date field (if one exists).
In case anyone is concerned about "month wrap-around", the DateSerial() function takes care of that for us. If the query was run in January 2015 then the first WHERE condition would indeed be ... >=DateSerial(2015,0,1) but that's okay because it returns 2014-12-01.
I have built an Access database to keep track of Quality Assurance Monitors for our team. Our team has several leads that oversee the reps on the team.
I want to build a query the will return all of the QA's for a specified lead for the current month. I have criteria for specifying a lead and have got that to work, but every time I try to set the criteria for the current month, it ends up returning no results.
Searching Google has repeatedly suggested using the Month(Now()), but that doesn't work either.
How can I write this query?
Answer inspired by #Justin-rentmeester but refined to always return the desired results:
Add the following to the WHERE clause of your query:
MyDateField BETWEEN
DateSerial(Year(Date()),Month(Date()),1)
AND
DateAdd("s", -1, DateAdd("m", 1, DateSerial(Year(Date()),Month(Date()),1))), DateSerial(Year(Date()),Month(Date()),1)
I reccommend that you use SQL view to add this criterium.
This approach also works with dates that include times, and with months with less than 31 days.
Put this code on criteria field- Between Date()-Day(Now()-1) And Date()
I edit my answer!
Assuming workdate is your date field.
A shorter where clause can be
where Year(workDate) = Year(Date()) and Month(workDate) = Month(Date())
This will filter all records for the current month.
If you want to further filter for records up to today add
and workdate <= Date()
This may not be necessary if you are not storing future dated records in your table.
Good luck.
To do this in the Design view in Access, select all of the columns you want in the report and on the date column you want to restrict to the current month, for the Criteria put: Between
DateSerial(Year(Date()),Month(Date()),1)
And
DateSerial(Year(Date()),Month(Date()),31)
I am using an MS Access db to track some tasks during the year. Each task has a due Month. I do not want to use exact dates as the convention in my team is to refer to the month. I don't want to store the dates in a date format as team members will be entering the due month by hand.
Is it possible to sort my fields in date order if the date is stored as a text string month? (eg. January, February rather than 31/01/2009, 28/02/2009).
If so, what would such a query look like?
Thanks in advance.
If you are storing only the month name, your will first need to convert to a date to get a month number, use a lookup table (MonthNo, MonthName) or use the Switch function. Here is an example of converting to a date:
SELECT Month(CDate(Year(Date()) & "/" & [MonthNameField] & "/1")) AS MonthNo
FROM Table
However, there is probably a good argument for storing a date based on the month name entered, this would prevent any confusion about years.
This should work
SELECT *
FROM TableName
OrderBy Month(date_field)
I would store the month as an integer 1-12 then you can easily sort them.
I would make a date field.
I would store 1/1/2009 for January 2009, 2/1/2009 for February 2009, and so forth. For display purposes, I'd format it so that it displayed only the month (or Month + Year -- can't imagine how you wouldn't want the year).
This makes it possible to take advantage of date operations on the field without messy conversions of text to date formats.
Thank you all for your responses. Sorry for the delay in responding - I'm working on this issue again now.
For clarity, the DB is to be used to track a schedule of events within a 12 month period. The year does not need to be stored as everything in the DB is referring to the same year. A new copy of the DB will be made at the beginning of 2010.
I'm really keen to actually store the month as a word rather than any kind of value or date field as when bulk adding tasks I will likely edit the table directly rather than use a form.
I realise this is dead but google brought me here while i was looking so thought I would add to it:
I had this problem myself (Access 2010) and found a decent answer here: http://www.vbforums.com/showthread.php?503841-How-to-Convert-MonthName-to-Value(Microsoft-access-2003)
So what I did was have a Query which pulled out the DISTINCT months from my table. Then in the design view i added another column with MonthNo: Month(CDate("1 " & [Month]))and sorted the query on this column
hope this helps someone if not the OP.