SSRS: Expression to filter by datepart - reporting-services

I have a dataset which contains phone data for a given period, i need to filter this data based on the hour of the day so i can use this in a chart which shows the peak periods.
So far, i have this expression:
=Count(IIF(DatePart("h", Fields!CallStart.Value = 7), Fields!ID.Value, 0))
So, what i had hoped this expression would do is replicate this SQL Query:
select * from PhoneData
where MONTH(callstart) = 7 and YEAR(callstart) = 2012 and DATEPART(HH, callstart) = 7
and Direction ='i' and Part1Device not like 'v%' and Continuation = '0'
The month and year are set in the dataset query.
Suffice to say, the expression doesn't work and i can't quite figure out why.. any help would be greatly appreciated.

Looks like your closing bracket for the DatePart function is in the wrong place. Try this.
=Sum(IIF(DatePart("h", Fields!CallStart.Value) = 7, 1, 0))

Related

SSRS Expression count records with date passed

In SSRS I am trying to create an expression that counts all records that have been scheduled for the end of today. How can is this possible?
=Count(IIF(Fields!scheduledendValue.Value <= Today, 1, 0))
Many thanks for help
Your logic is good, but your formula isn't. The "Today" function requires open and closing parentheses. Try this:
=Sum(IIF(Fields!scheduledendValue.Value <= Today(), 1, 0))

Create a date field in SQL query for Access based on criteria

I am a novice user and have only recently started using VBA in Access. I have been tasked to change a complex database to reflect a new date period for a month. Instead of using the beginning of the month as 1, my customer wants the month to be from the 23rd to the 22nd. This will give them a week to prepare for processing.
I have managed to get most of it working, with the following exception. I need to assign DT according to the new criteria.
While the way it was developed is far from the method I would use. I am stuck with making it work.
Here is the old code in SQL view:
SELECT G.Part,
G.Process,
Sum(G.QPass2) AS QtyPass,
Sum(G.QFail2) AS QtyFail,
Sum(G.QNull2) AS QtyNull,
IIf(Sum(SYtd)=0,NULL,Sum(SYtd)) AS Sprayed_Yesterday,
Sum(G.SpMTD) AS SprayedMTD,
G.AftMkt,
G.DT
FROM
(SELECT Sprayed.Part,
Sprayed.Process,
Sum(Sprayed.QPass) AS QPass2,
Sum(Sprayed.QFail) AS QFail2,
Sum(Sprayed.QNull) AS QNull2,
Sum(IIF(Sprayed.Date_Stamp = Date()-1,Sprayed.QPass + Sprayed.QFail + Sprayed.QNull,0)) AS SYtd,
Sum(Sprayed.Qpass + Sprayed.QFail + Sprayed.QNull) AS SpMTD,
Sprayed.AftMkt,
Dateserial(Year(Date_Stamp), Month(Date_Stamp), 1) AS DT
FROM Sprayed
GROUP BY Part,
Process,
AftMkt,
Dateserial(Year(Date_Stamp), Month(Date_Stamp), 1)) AS G
GROUP BY G.Part,
G.Process,
G.AftMkt,
G.DT
HAVING (((Sum([G].[QPass2])+Sum([G].[QFail2])+Sum([SYtd]))>0));
Replace the formula for DT with this: IIf(Day(Date_Stamp) > 22, DateSerial(Year(Date_Stamp), Month(Date_Stamp)+1, 1), DateSerial(Year(Date_Stamp), Month(Date_Stamp), 1))
You can simply offset the Date_Stamp by one week:
Dateserial(Year(DateAdd("ww", 1, Date_Stamp)), Month(DateAdd("ww", 1, Date_Stamp)), 1) AS DT

Finding difference at group level

I have 3 months of AR data. From SQL I am populating details records for 3 months. In SSRS I am grouping the totals by month and showing only group summary. I want to calculate the variance by difference between current month and previous month. Please see the attached output file.
Could anyone help me on finding the variance at group level. How do I calculate the difference by using previous function?
Is there any way to find individual group sum of the particular field like this one
Sum(Fields!Current.Value, "DataSet1")
Something like this perhaps?
=Sum(iif(month(Fields!myDate.Value) = month(Max(Fields!myDate.Value)), Fields!myVal.Value, 0))
-Sum(iif(month(Fields!myDate.Value) = month(dateadd("M", -1, max(Fields!myDate.Value))), Fields!myVal.Value, 0))
Where myDate is the date used to group the months on, and myVal is the specific data for the column
UPDATE
Using this dataset
EOM myValue
09/30/2015 2
10/31/2015 6
11/30/2015 19
Gives this basic report
You can then use this formula (based on the original one in the original answer above) to determine the difference between the last and the penultimate row
=Sum(
iif(month(CDate(Fields!EOM.Value)) = month(Max(Fields!EOM.Value)),
Fields!myValue.Value,
0)
)
-Sum(iif(month(CDate(Fields!EOM.Value)) = month(dateadd("M", -1, max(CDate(Fields!EOM.Value)))),
Fields!myValue.Value,
0)
)
Then place this in the footer of the tablix as shown
This shows the difference between the two rows
Is this the behaviour you require? If not please clarify further in the original question as an update/edit.

Using the NOW Function In Access

I am setting up a code to pull all employees hired within the last 2 years who got a certain rating. I have been looking into the YEAR(NOW()) function but I am having a difficult time setting it up. I need to use the NOW function because I need it to pull the data from the time the user access the query. The ratings are completed every following feburary (i.e 2013 ratings will be completed in February of 2014) so it needs to read something like
YEAR(NOW()-12) but it
This way if I were to run it today it would go back and pull the ratings for 2012 and 2011 since 2013 have not yet been completed.
My whole code looks like:
SELECT dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score
FROM O867IA_VJOBHST INNER JOIN dbo_v_TMS_QPR_01_Score ON O867IA_VJOBHST.SYS_EMP_ID_NR = dbo_v_TMS_QPR_01_Score.GEMSID
WHERE (((dbo_v_TMS_QPR_01_Score.Final_QPR_Score)>="1.25") AND ((O867IA_VJOBHST.EMP_ACN_TYP_CD)="HIR") AND ((O867IA_VJOBHST.REC_EFF_STT_DT)=Year(Now()-12)))
GROUP BY dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score;
But I keep getting the error: INCONSISTENT DATATYPES: EXPECTED DATE GOT NUMBER (#932)
What you have does not work. It subtracts 12 days off the current date/time and then converts it to the year. Thus, it returns 2013.
Use the dataadd() function. The following is a blank query in the query designer.
I am asking for today's date minus 12 months. See output below.
I would THINK you want something like:
If Month(Now()) > 3 then 'If it's after Feb, we want the last 2 years
LastDayOfPrevYear = DateAdd("d", -1, DateSerial(Year(Now()), 1, 1))
Else 'If it's before March, we want the 2 years prior to last year
LastDayOfPrevYear = DateAdd("d", -1, DateSerial(Year(Now())-1, 1, 1))
End If
SELECT dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score
FROM O867IA_VJOBHST INNER JOIN dbo_v_TMS_QPR_01_Score ON O867IA_VJOBHST.SYS_EMP_ID_NR = dbo_v_TMS_QPR_01_Score.GEMSID
WHERE (((dbo_v_TMS_QPR_01_Score.Final_QPR_Score)>="1.25") AND ((O867IA_VJOBHST.EMP_ACN_TYP_CD)="HIR") AND ((O867IA_VJOBHST.REC_EFF_STT_DT)>=DateAdd("m", -24, LastDayOfPrevYear)))
GROUP BY dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score;
This will give you a "rolling" 24 month timespan from the last date of the previous year.
This may need a little tweaking, but it should be, at the very least, extremely close.

how to do a sum iff expression for datepart m in last month

I am trying to write an expression to count the number of requests within a specific month from a year's worth of data.
I have tried:
=sum(iif((datepart("M",Fields!RequestDate.Value)) = (datepart("m",Now(-1))),1,0))
and many more different versions. Can someone point me in the right direction?
It seems in your example you're counting events from last month? I'm not sure what Now(-1) is trying to calculate in your example.
Anyway, this example worked for me in identifying counts from last month:
=Sum(IIf(DatePart("m", Fields!RequestDate.Value) = DatePart("m", DateAdd("m", -1, Now()))
, 1
, 0))
You can update the DateAdd("m", -1, Now()) part of the expression to set the month you want to update against.