SSRS: Summing TimeSpan values in a report - reporting-services

I have a report and a datasource where one of the columns are of type TimeSpan. The TimeSpan value appears to display correctly in the report when I use Fields!TheTime.Value, no problem there.
07:02:00
05:41:00
But I would like to do a Sum on those values to get the total time of a group. In C# and such I can of course do a TimeSpan + another TimeSpan, so I know they can be added. I tried
=Sum(Fields!TheTime.Value)
But it ends up printing out as a long number of some sort. For example for the outputted times above, I would get 457800000000 as the sum. And what is that even supposed to be?
Anyways, how can I sum timespan values in a report? For the above timespans I would like to end up with 12:43:00 as the sum. Unless my head failed me at math once again... but you get the idea :p

sigh The solution annoyingly simple... Why couldn't I just have tried that in the first place? Oh well... maybe because I didn't realise I had access to TimeSpan class... maybe because I had thought myself blind... But anyways, here it is:
=TimeSpan.FromTicks(Sum(Fields!TheTime.Value))
D'oh!

#Svish - I deleted my previous post because I had a fit uncertainty about my answer but I concur with #pfunk.
I finally got SSRS back up and had a play around and it certainly looks like your big number is the number of ticks so it looks like a bit of formatting of the result will work for you.
Interestingly enough my previous convoluted answer was a workaround for summing DateTime values (using SQL Server DATETIME datatype in my query) which you cannot do in SSRS (and SQL) because you cant sum a DATETIME. I'll include it here again for future reference but I think was on a bit of a tangent earlier :)
The below code converts a DateTime field into a double, sums the result and then converts it back to DateTime and formats it for hh:mm:ss
=Date.FromOADate(Sum(Fields!TheTime.Value.ToOADate())).ToString("hh:mm:ss")

What is probably happening is that when you display Fields!TheTime.Value, SSRS is smart enough to know to display that as a DateTime type field
when you add the sum in there it thinks it is a numeric type field and displays it as such (ie, it is summing the number of "ticks" in each timespan field)
try specifically formatting the summed value as a datetime in the field properties and it will probably show correctly

Related

Ms Access - Data type mismatch error when doing sum of few columns that will contain text symbols

I'm working on a simple RDBMS project. My database contains work hours per day of each employee. I have to calculate work hours of complete month. It is not a problem. But I have a problem, here it goes, each workday field contains either number of work hours or it may contain text such as if the employee was absent (symbol "A") for a particular day or it may contain symbol ("H") for holiday etc. Now my task is to calculate the 30 day work hours and exclude any symbol. But obviously the approach I am using will get me "Data Type Mismatch Error." So any body got any solution or any suggestion please. Thanks in advance!
I tried to use sum formula with NZ function, it doesn't work. Also tried Query with the same error.
Use Val:
TrueHours: Val(Nz([YourHourTextFieldName]))
That will return 0 (zero) for non-numeric entries, and that allows you to sum the values.

How to calculate the difference between days in a table field

Ok this should be a relatively easy thing to do, yet I'm at the head desk stage trying to figure out the insanity here.
I have a table called tblPersonnel. I'm tracking two document expiration dates in date/time fields called CED and PPED. When I run a query against tblPersonnel I need it to look at PPED, determine if that document is expired and if so use CED instead. I have a few fields in the query that need to use this concept to determine what the output value is, but I am hitting a wall here trying to get the query to spit out the correct value. Here's what I'm using for one of the fields - Document Expiration Date: IIf([PPED]-Now()<0,[CED],[PPED]). What's happening is that the expression is constantly popping as false, so PPED is getting used regardless if it's an expired date or not. Does anyone have any ideas as to what I'm doing wrong here?
I've also tried to set this up as its own field in tblPersonnel, but that's even more aggravating. If I try to set the field to just a text field - IIf([PPED]-Now()<0,"Yes","No"), the formula will accept the use of Now(), but it doesn't like the reference to the other fields in the table. If I set it as a calcuated column, I can reference the other fields but it doesn't like Now(). I'm at a loss here.
If PPED is less than Date(), it is expired. Don't need to subtract. Assuming CED and PPED are just date parts, no time, consider:
IIf([PPED] < Date(), [CED], [PPED])
If PPED could be null:
IIf(Nz([PPED],0) < Date(), [CED], [PPED])
Ok finally fixed it here. I had another issue in that I wasn't accounting for how Access would handle a Null or blank value in PPED. The functioning formula is Document Expiration Date: IIf(Len([PPED])>0,IIf([PPED]<Date(),[CED],[PPED]),[CED]) Thanks to June7 for helping me simplify the expression, as I was using DateDiff('d',[PPED],Date())<0 but their answer is just so much cleaner and quicker to type.

How to stop rounding in ssrs report

I have an SSRS report that is rounding currency and I need the report to show the actual value. When I run the query in query designer all the values are shown correctly. If I output this to Excel and SUM the values I get the total I expect (e.g £56724.30)
When I run the report I get the value £56840.00 so it looks as if the data being used is getting rounded before output.
I have a Calculated field in the report called Total_Rent_Due_UC_Claims:
=iif(Fields!UC_Rent.Value = Fields!LastCharge.Value, Fields!UC_Rent.Value, 0) or
iif(Fields!UC_Rent_Date.Value = Fields!LastCharge.Value and
Fields!extra10a_d003.Value < Parameters!AsAtDate.Value, 0, Fields!UC_Rent_Date.Value)
I then use this to get a total:
=Sum(Fields!Total_Rent_Due_UC_Claims.Value)
I have formatted this field to currency to two decimal places.
Can someone assist with this so that the value in the report is the same as the expected value?
I don't think this difference can be attributed to rounding -- at least not any rounding I've ever seen before. That's a difference of £115.70. I think the calculated field isn't quite doing what you want it to and needs a little modification. I'm not exactly sure what it's doing right now, but basically both of those IIF statements will evaluate because OR doesn't really work the way you have it. I would try the following expression.
=IIF(Fields!UC_Rent.Value = Fields!LastCharge.Value,
Fields!UC_Rent.Value, IIF(Fields!UC_Rent_Date.Value = Fields!LastCharge.Value
AND Fields!extra10a_d003.Value < Parameters!AsAtDate.Value, 0, Fields!UC_Rent_Date.Value))
Of course, this could still be wrong as I can't tell if all of the fields are date datatypes or if there's some kind of mismatch going on here. Let me know if this doesn't work and I'll see if I can adjust it.

SSRS datetimepicker and textboxes behave differently when using Weekday function

In my SSRS report i have a date parameter and i want to set to it a default value with a complicated logic. I arrived at this strange behaviour:
Today is May 6th, wednesday. If i use the following expression:
DateAdd("d",Weekday(Today(),DayOfWeek.Sunday),Today())
for the default time picker I get May 9th.
If I use exactly the same expression in a textbox in the same report
DateAdd("d",Weekday(Today(),DayOfWeek.Sunday),Today()).ToLongDateString()
I get May 10th!
The only thing that changes is the toString.
Why are the two values different?
I tried with different expressions and the difference arises when i start using Weekday(Today(), somevalue)
After a while and some fundamental help i realized that I had to use "FirstDayOfWeek" instead of "DayOfWeek" in both expressions for both of them to be the same regardless of current culture.

Use a summary field in an expression in SSRS reports

I have the details of my report being summed up in a summary expression, all works fine. The fields are decimal values of hours worked. Thus, the summary value is also a decimal value. I'd like to access the summary value and convert it to hours / minutes. I've labeled the express as "WorkTimeSum", but can't seem to get a handle to it, however. Fields! obviously won't work since it is a summary expression. I was thinking ReportItems! should work, but to no avail. How can I use this expression field (in a summary row) in an expression in the same summary row?
If I've understood correctly, you're asking how to reference the textbox containing the total work hours value so that you can convert it to hours and minutes using an expression in a different textbox?
You can use either ReportItems! e.g.
=ReportItems!Textbox20.Value)
or ReportItems("") e.g.
=ReportItems("Textbox20").Value
to reference the value of another textbox. Be careful with the names as they are case sensitive.
You can use aggregate functions in any expression. For example, in any header rows you can use the following expression to determine the total hours value:
=Floor(Sum(Fields!hours.Value))
Sum(Fields!hours.Value) is just the total hours in whatever context, e.g. the group total if it's a group header row; you can use this expression as an input in any other expression you require.
It sounds like your issue wasn't the conversion itself, so hopefully this points you in the right direction. If you need further information please specify.