I'm trying to sum a field value in an SSRS report based on a date range field in the same dataset, but have had no luck. So far this is what I've come up with. I've scoured the forums with no luck. Any help would be greatly appreciated.
= sum( iif(
datediff("d",Fields!Calculated_DueDate.Value.value,today())>=31
AND
(datediff("d",Fields!Calculated_DueDate.Value,today())<62
AND (Fields!Calculated_InvoiceBal.Value.value>0 ),
Fields!Calculated_InvoiceBal.value,0)))
Calculated_DueDate is my date field and
Calculated_InvoiceBal is the field I want to sum
The first: you shouldn't use expression like Fields!Calculated_DueDate.Value.value - it is error.
The second: There are error in arrangement of brackets.
The third: In SSRS 2008R2 call of function today() marks as error. You can use expression DateTime.Today to get current date without errors markup.
So, your expression should look like this:
=sum(
iif(datediff("d",Fields!Calculated_DueDate.Value, DateTime.Today) >= 31
AND datediff("d",Fields!Calculated_DueDate.Value, DateTime.Today) < 62
AND (Fields!Calculated_InvoiceBal.Value > 0),
Fields!Calculated_InvoiceBal.value,
0
)
)
Try this:
=SUM(IIf(datediff("d",Fields!Calculated_DueDate.Value.value,today())>=31 AND datediff("d",Fields!Calculated_DueDate.Value,today())<62 AND Fields!Calculated_InvoiceBal.Value.value>0),
Fields!Calculated_InvoiceBal.value, 0)
Related
I currently have a QUERY function which is set up based on a start date cell and an end date cell, formula as below:
=QUERY(Haulage!$A$3:$L$29," Select * Where A >= date """&text('2022 Stats'!S1, "yyyy-mm-dd")&""" AND A <= date """&text('2022 Stats'!T1, "yyyy-mm-dd")&"""")
This is working fine but I would like to adapt it so I can also narrow the query down further with the use of a dropdown. I have the following IF formula for this:
=IF('2022 Stats'!V1="All TOCs",""," AND LOWER(K) = LOWER('"&'2022 Stats'!V1&"') " )
This seems to yield the correct results but I am struggling to get the two to work together......
Link to sheet: https://docs.google.com/spreadsheets/d/1wTWuvFwMTqJ-sjIZbXWpGOS1WKwpODj2R8KAzqlqkuw/edit?usp=sharing
Try:
=QUERY(Haulage!A3:L29,
"where A >= date '"&TEXT('2022 Stats'!S1, "yyyy-mm-dd")&"'
and A <= date '"&TEXT('2022 Stats'!T1, "yyyy-mm-dd")&"'"&
IF('2022 Stats'!V1="All TOCs",,"
and lower(K) = '"&LOWER('2022 Stats'!V1)&"'"))
This may have been asked before but I've not been able to find it having searched! In Oracle SQL there's a function called MONTHS_BETWEEN which returns a fractional value if the two dates you are comparing are not both the first day of the month for example. I need to do something similar in SSRS report builder, I've tried using DateDiff("m",Date1,Date2) however this always returns an integer and I think from what I can tell it just compares the two months from the dates, so when I compare 30/09/20 and 01/04/21 I get 7 months when actually it is much closer to 6.
Is there a function or a fix that can be used in SSRS to get that more accurate value?
Thank you!
For example I would like to get the following result:
Difference between 30/09/20 and 01/04/21 = 6.1
Difference between 01/08/20 and 30/09/20 = 1.9
It doesn't have to super accurate as I will be rounding to the nearest integer but I'm looking for something that will recognise that in the second example nearly 2 months have been covered and in the first example it's only just over 6 months.
If you only need an approximation then you could just calculate the number of days difference and divide by 30.
using the following expression...
=DATEDIFF("d", Fields!startDate.Value, Fields!endDate.Value)/30
I put a few more examples into a table and got the following results.
The following code mimics oracle definition of months_between
Public Function MonthsBetween( d1 As Date, d2 As Date) As Decimal
Dim df As Decimal
df = DateDiff("m", d1, d2)
If Day(d1) <> Date.DaysInMonth(Year(d1), Month(d1)) Or Day(d2) <> Date.DaysInMonth(Year(d2), Month(d2)) Then
df = df + Cdec((Day(d2)-Day(d1))/31)
End If
Return df
End Function
Integer result when both dates are last day of month
Negative result when date1 > date2
Decimal part based on 31 days month
For your expression use something like
=Code.MonthsBetween(Fields!date1.Value , Fields!date2.Value)
UPDATE
The following expression works in the same manner
= Cdec(
DateDiff("m", Fields!date1.Value, Fields!date2.Value)
+
Iif (
Day(Fields!date1.Value) <> Date.DaysInMonth(Year(Fields!date1.Value), Month(Fields!date1.Value)) Or
Day(Fields!date2.Value) <> Date.DaysInMonth(Year(Fields!date2.Value), Month(Fields!date2.Value)) ,
Cdec( (Day(Fields!date2.Value) - Day(Fields!date1.Value))/31),
0.0
)
)
I'm just learning Power Query and trying to figure out how modify a filter to return data within a dynamic date range. E.g. from Today - 60 days to Today
Here's the code, any help much appreciated.
= Table.SelectRows(#"Changed Type", each [Start] >= Date.AddDays(DateTime.Date(DateTime.LocalNow())-60) and [Start] <= DateTime.Date(DateTime.LocalNow()))
You can do that also in UI.
If your column if type date:
Then when you look into Advanced Editor (or formula bar) you will see that Power Query has dedicated function for that:
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each Date.IsInPreviousNDays([Column1], 60))
= Table.SelectRows(#"Changed Type", each [Start] >= Date.AddDays(Date.From(DateTime.LocalNow()),-60) and [Start] <= Date.From(DateTime.LocalNow()))
Help me Stackoverflow, I'm close to going all "HULK SMASH" on my keyboard over this issue. I have researched carefully but I'm obviously not getting something right.
I am working with a Julian dates referenced from a proprietary tool (Platinum SQL?), though I'm working in SQL 2005. I can convert their "special" version of Julian into datetime when I run a select statement. Unfortunately it will not insert into a datetime column, I get the following error when I try:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
So I can't setup datetime criteria for running a report off of the Stored Procedure.
Original Value: 733416
Equivalent Calendar Value: 01-09-2009
Below is my code... I'm so close but I can't quite see what's wrong, I need my convert statement to actually convert the Julian value (733416) into a compatible TSQL DATETIME value.
SELECT
org_id,
CASE WHEN date_applied = 0 THEN '00-00-00'
ELSE convert(char(50),dateadd(day,date_applied-729960,convert(datetime, '07-25-99')),101)
END AS date_applied,
CASE WHEN date_posted = 0 THEN '00-00-00'
ELSE convert(char(50),dateadd(day,date_posted-729960,convert(datetime, '07-25-99')),101)
END AS date_posted
from general_vw
SELECT
org_id,
CASE WHEN date_applied = 0 OR date_applied < 639906 THEN convert(datetime, '1753-01-01')
ELSE dateadd(day,date_applied-729960,convert(datetime, '07-25-99'))
END AS date_applied,
CASE WHEN date_posted = 0 OR date_applied < 639906 THEN convert(datetime, '1753-01-01')
ELSE dateadd(day,date_posted-729960,convert(datetime, '07-25-99'))
END AS date_posted
from general_vw
You're casting to char but want a datetime so that's one easy fix.
You were also using '00-00-00' as your minimum date, but the minimum TSQL date is '1753-01-01'. Alternatively you could use something like ('1900-01-01') but that would need a change to the "less than" date_applied comparer.
I've added a "less than" date_applied comparer too. I calculated this as "SELECT 729960 + datediff(day,convert(datetime, '07-25-99'), convert(datetime,'1753-01-01'))". Any number less than this would cause a date underflow.
I'm creating a Jasper report that includes the following parameters:
DATESTART (Date)
DATEEND (Date)
These parameters indicate a date range for a field called DATECREATED (Timestamp) which includes times. I would like the date range to be INCLUSIVE, that is, if I filter for "Jan 1, 2009" to "Jan 31, 2009", any DATECREATED value on Jan 31, 2009 (such as "Jan 31, 2009 15:00") will be included in the report.
When I used Crystal Reports in the past, I used the DATEADD function to create a filter expression like the following:
{DATECREATED} >= {DATESTART} and {DATECREATED} < DATEADD("d", 1, {DATEEND})
(I realize that this isn't syntactically correct, but you get the idea.)
Is there any way to do something similar in Jasper Reports?
If you understand French, there the same question is asked in this thread
(the only difference is that it is about adding a month)
The proposed solutions are following:
SQL
Do it with SQL statement directly in the query (if your data source is a SQL datasource of course).
With MySQL you can do something like
DATE_ADD($P{DATEEND},INTERVAL 1 DAY);
more information: Date and Time Functions (MySQL doc)
JAVA
The other solution is to use the Java possibly of the Date object:
I proposed something like :
$P{DATEEND}.setDay($P{DATEEND}.getDay()+1)
But I did not try it (and it is probably wrong).
Maybe you need to defined a new Date Variable DATEEND_1
with a value expression like :
new Date($P{DATEEND}.getTime() + 24*60*60*1000)
or
new java.util.Date($P{DATEEND}.getTime() + 24*60*60*1000)
And use this new variable in your query V{DATEEND_1}.
(again I am not sure of it)
Try this:
new java.util.Date($P{DATEEND}.getTime() + 24*60*60*1000)
Another option is to use the Groovy SDK that comes bundled with the latest versions.
new Date().plus(1) //Today plus one day = tomorrow.
Or to be more complete - add your two parameters DATESTART and DATEEND and set the default expression to be the above code. Then in your query add the following to the where clause:
DATECREATED >= $P{DATESTART} and DATECREATED < $P{DATEEND}
or depending on your SQL variant.
DATECREATED BETWEEN $P{DATESTART} AND $P{DATEEND}
Suppose you have a Parameter PARAM1 and you want to add 366 days to param1 then you can do it by following way
1) Declare another parameter say $P{finalDate}
2) Code like below for $P{finalDate} as the Default Value Expression
new Date($F{PARAM1}.getTime() + 366L*24*60*60*1000)
Dont forget to put L after 366 . Without putting L , it may not work properly and may not give accurate date.
It will add 366 days to PARAM1
We have built a library of static date-related functions, which can be used like this:
DateUtil.add(NOW(), 0, 0, 1)
The above would get you a date one day into the future (the arguments are year, month, day). It would be nice to have a DATE_ADD function supported directly by JasperReports. Sounds like a patch waiting to be submitted.
DAYSINMONTH($P{Date}) >= (DAY($P{Date})+ 1)
?
DATE(YEAR($P{Date}),MONTH($P{Date}),DAY($P{Date})+1)
:
((MONTH($P{Date}) + 1) > 12) ? DATE(YEAR($P{Date}) + 1,1,1) : DATE(YEAR($P{Date}),MONTH($P{Date}) +1 ,1)
I am sorry for necro'ing this post but I thought that I should share this as another alternative to the options posted above. You just need to change all the $P{Date} to be your parameter!
You can create a class to help you, which has a static method which processes your parameters (or modify class JasperUtils by adding a static method). This way you can have amethod returning a boolean which will act exactly as the filter you need.
package com.package_name.utils;
public class JasperUtils2 {
public static Boolean filterDate(Date dateStart, Date dateEnd, Date dateCreated) {
if (dateCreated.compareTo(dateStart) < 0) return false; // dateCreated is greater or
// equal to dateStart
if (dateCreated.compareTo(dateEnd) > 0) return false; // dateCreated is smaller or
// equal to dateEnd
// you can combine the two conditions into one. I wrote it like this to be more obvious
}
}
To use this method, you need to import the class created (Edit -> Report import directives -> new import ---- this may differ for different versions of iReport). Then, you can use the method in static way:
JasperUtils2.filterDate(..., ..., ...)