SSRS Textbox expression filter from Dataset - reporting-services

Trying to create a TextBox expression:
="Validity: " & IIF(Fields!ID.Value = 2, Fields!Value.Value, "") & " from date above."
from a dataset:
ID; NAME; VALUE;
1; Delivery; x Factory;
2; Validity; 30 days;
3; Pricing Structure; Subject to...;
so that the text box would read "Validity: 30 days from date above" but returns "Validity: from date above"
The problem is the report only allows me to use aggregate First, max, etc from the dataset producing an incorrect result.
"Validity: " & IIF(First(Fields!ID.Value, "DataSet") = 1, First(Fields!Value.Value, ), "") & " from date above."
"Validity: x Factory from date above"

Your dataset is showing "30 days", do you require the text box to show this or do you require it to be "60 days"?
Meanwhile if you restrict you dataset to one row of data, ie insert a where/having clause such as : HAVING (ID = 2), then you could use the aggregate sum function in your expression:
="Validity: " & IIF(Sum(Fields!ID.Value, "DataSet1") = 2, Fields!Value.Value, "") & " from date above."

Related

SSRS - how to return blank as display if your query result is Null using expression

I am creating a report right now in ssrs that involves two columns from db. The two columns are needed to be combined in the displayed report.
Columns are:
[Column 1] Price_Low
[Column 2] Price_High
and sample values in both columns is: 0.0000
1st question how can i combine the 2 column having a dollar sign same this output using SSRS expression:
[1]: https://i.stack.imgur.com/dim6H.png
2dn question: what if query returns NULL how can i display just blank and not, $ - $
Here is my sample Exp:
'''=" $" & Fields!Price_Low.Value & " - $" & Fields!Price_High.Value'''
You can try the following solution:
=IIF(
(Format(Fields!Price_Low.Value, "C4") + " - " + Format(Fields!Price_High.Value,"C4")) = " - ", "",(Format(Fields!Price_Low.Value, "C4") + " - " + Format(Fields!Price_High.Value, "C4"))
)

SSRS change minute into day, hour and min

I have a Time column in which is in minute , in ssrs i need to get average and output in such a way that its in Day, Hour and min.
For example Column name is Time (Min).How to write an expression in such a way that we can get result in day, hour and min
Assuming that your time column is just an integer datatype containing a number of minutes then something like this will work.
The following calculates based on a report parameter to make it easier to test so oyu will need to swap this out for the correct column name and aggregations. e.g. if you time column is called MyTime and you want to calculate based on the average then swap out Parameters!MyMins.Value with AVG(Fields!MyTime.Value)
= INT(Parameters!MyMins.Value / 1440) & " days " &
INT((Parameters!MyMins.Value MOD 1440) / 60) & " hours " &
(Parameters!MyMins.Value MOD 60) & " mins"
adjust the output to suit you formatting requirements....
The above turns 4455 minutes into the string "3 days 2 hours 15 mins"
Enter the following expression:
=Format(TimeSerial(0,Parameters!MyMins.Value,0),"dd") & " Days "
& Format(TimeSerial(0,Parameters!MyMins.Value,0),"HH") & " Hours "
& Format(TimeSerial(0,Parameters!MyMins.Value,0),"mm") & " Minutes "
Alternatively, create another parameter e.g. MyTime
=TimeSerial(0,Parameters!MyMins.Value,0)
(Ensure that this parameter is evaluated after MyMins by moving it down the list)
Then put that into the code above
=Format(Parameters!MyTime.Value,"dd") & " Days "
& Format(Parameters!MyTime.Value,"HH") & " Hours "
& Format(Parameters!MyTime.Value,"mm") & " Minutes "

Dsum with Null Value

I am having abit of a situation and hope you can point me at the right direction.
1st DSUM (Text2):
=DSum("[quantity_ya7]","Stock","[part_number]= '" & [part_number] & "'")
2nd DSUM (Text2): (This can be Null at times as there are no withdrawals or records)
=DSum("[amt_ya7]","Withdrawal","[part_number]= '" & [part_number] & "' ")
After setting the 2 DSUM above, I will carry the subtract out.
=Text1-Text2
If there are values for to calculate for 2nd DSUM, the result will display in order. Else, it will be empty. (No values displayed)
How do I calculate as -0 (deduct 0) so I can get the right value displayed?
Thank you, much appreciated.
You can use the Nz function to return zero, a zero-length string ("
"), or another specified value when a Variant is Null. For example,
you can use this function to convert a Null value to another value and
prevent it from propagating through an expression.
https://support.office.com/en-gb/article/Nz-Function-8ef85549-cc9c-438b-860a-7fd9f4c69b6c
In your case you want the DSUM to return 0 rather than Null when there's no value so you can use it in a calculation.
=NZ(DSum("[amt_ya7]","Withdrawal","[part_number]= '" & [part_number] & "' "),0)

Formatting Datetime in SSRS Expression

Part of my query is like so:
SELECT * FROM TableA
WHERE ColumnA >= DATEADD(DAY, - 30, GETDATE())
With the expression at the where clause above, you can pull a rolling 30 days data without having to supply values. Now users of the report want to see it represented like:
2nd April – 1st May
when the report is ran. Knowing that I have no parameters as the requirement is to not use parameters, how do I reference ">= DATEADD(DAY, - 30, GETDATE())" to reflect the start date and the end date in the report?
SSRS doesn't have built-in support for ordinal numbers (i.e. "1st" or "2nd" instead of "1" or "2"). This page contains custom code to add this functionality to your SSRS report; however it is slightly wrong. Here is a corrected version:
Public Function FormatOrdinal(ByVal day As Integer) as String
' Starts a select case based on the odd/even of num
if(day = 11 or day = 12 or day = 13)
' If the nymber is 11,12 or 13 .. we want to add a "th" NOT a "st", "nd" or "rd"
return day.ToString() + "th"
else
' Start a new select case for the rest of the numbers
Select Case day Mod 10
Case 1
' The number is either 1 or 21 .. add a "st"
Return day.ToString() + "st"
Case 2
' The number is either a 2 or 22 .. add a "nd"
Return day.ToString() + "nd"
Case 3
' The number is either a 3 or 33 .. add a "rd"
Return day.ToString() + "rd"
Case Else
' Otherwise for everything else add a "Th"
Return day.ToString() + "th"
End Select
end if
End Function
If you add this code to the code section of your report under report properties, your textbox expression would be:
Code.FormatOrdinal(Day(Globals!ExecutionTime)) & " " & MonthName(Month(Globals!ExecutionTime), False) & " - " & Code.FormatOrdinal(Day(DateAdd("d", -30,Globals!ExecutionTime))) & " " & MonthName(Month(DateAdd("d", -30,Globals!ExecutionTime)), False)

Right Click on the Textbox, Go To Textbox Properties then, Click on Number tab, click on custom format option then click on fx button in black.
Write just one line of code will do your work in simpler way:
A form will open, copy the below text and paste there to need to change following text with your database date field.
Fields!FieldName.Value, "Dataset"
Replace FieldName with your Date Field
Replace Dataset with your Dateset Name
="d" + switch(int(Day((Fields!FieldName.Value, "Dataset"))) mod 10=1,"'st'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 = 2,"'nd'",int(Day((Fields!FieldName.Value, "Dataset"))) mod 10 = 3,"'rd'",true,"'th'") + " MMMM, yyyy"

SQL query to retrieve data between two dates

I've written following code:
Dim date1 As Date
Dim date2 As Date
date1 = Convert.ToDateTime(DatePickerFromDate.Text)
date2 = Convert.ToDateTime(DatePickerToDate.Text)
Dim cnd As New OleDbCommand("SELECT * FROM Sales WHERE Invoice_Date BETWEEN " + date1 + " AND " + date2 + "", om)
om.Open()
Dim da As OleDbDataReader = cnd.ExecuteReader
While da.Read()
ComboBox1.Items.Add(da(0))
End While
da.Close()
om.Close()
I want to retrieve data between two dates that are been taken from two datepickers.
I tried BETWEEN, also i tried >= =< but result was empty though database contains data. Please help where I'm getting wrong
Your code is probably generating an error. When doing this type of querying, you should store the query string after substitution and print it out. You seem to be missing delimiters around the dates. So this may work in your specific case.
New OleDbCommand("SELECT * FROM Sales WHERE Invoice_Date BETWEEN '" + date1 + "' AND '" + date2 + "'", om)
However, you then need to be careful about the format of the dates. The application layer and the database might use different formats. If you are substituting directly into the query string, then use the format YYYY-MM-DD -- it is the ISO standard date format and generally understood.
Even better is to learn how to parameterize queries so you can actually pass in the date values as date parameters.
If you're using MS Access, this should be the syntax...
SELECT * FROM Sales WHERE Invoice_Date>=#" + date1 + "# and Invoice_Date<=#" + date2 + "#"
If you're using MS SQL Server or MySQL, then do something like this...
SELECT * FROM Sales WHERE Invoice_Date>='" + date1 + "' and Invoice_Date<='" + date2 + "'"