SSRS: Add multiple if statements - reporting-services

I am using SSRS to add certain values in a column together. If the Service ID is 13,15,18,or 19 I want it too add these values together. Right now I have:
=IIf(Fields!ServiceID.Value = 13,Fields!TermPrimary.Value, Nothing) + IIf(Fields!ServiceID.Value = 15,Fields!TermPrimary.Value, Nothing) + IIf(Fields!ServiceID.Value = 18,Fields!TermPrimary.Value, Nothing) + IIf(Fields!ServiceID.Value = 19,Fields!TermPrimary.Value, Nothing)
I thought This would allow me to add these values together, instead it only shows me the exact same values as the table below it showing serviceID 13. How do I add these figures together?

Your Service ID field will always be a single number and never equal to 13 AND 15 on the same line.
I think you want to Group your column by some other field and SUM the TermPrimary for that group IIF the ServiceID is one of your given values.
=SUM(IIf(Fields!ServiceID.Value = 13 OR Fields!ServiceID.Value = 15 OR Fields!ServiceID.Value = 18 OR Fields!ServiceID.Value = 19, Fields!TermPrimary.Value, 0)

Related

Mysql filter by multiply ids

I can't finish writing query to filter row by multiply ids. Here is query:
select distinct `storage_file`.*, `storage_tag`.`id` as `tid` from `storage_file`
inner join `storage_file_tag` on `storage_file`.`id` = `storage_file_tag`.`storage_file_id`
inner join `storage_tag` on `storage_tag`.`id` = `storage_file_tag`.`storage_tag_id`
where `storage_file`.`user_id` = 17 and `storage_file`.`deleted_at` is null and
`storage_tag`.`id` IN(13,17);
So the result is without group by statement is:
So.. I need result only with two records which contain tid 13 and 17
And when i replace "IN(13,17)" with storage_tag.id = 13 AND storage_tag.id = 17 - i get no records at all
How can i write subquery which will work like a + b but not a OR b ?
I'm not sure what you do exactly but it seams, that the distinct is not working as you expect, because you select "*" from storage_file, as there are different values in the columns of storage_file, the result is distincted but over all selected columnns and so more the two are selected.
You can replace
... AND id IN (11,22)
with
... AND ( id = 11 OR id = 12)
You need the parentheses because WHERE operator precedence rules are very simple.
Of course,
... AND id = 11 AND id = 12
never returns anything because the id cannot have two different values at the same time.

Using like operator in SSRS Expression

I have a tablix with following results.
SSRS Result
Since i am learning SSRS. i wonder how to Sum line total with respect to product name. Since product name has duplicate values but it has only M and Xl difference. If i use row group it won't total like i expected since it has M and Xl difference. I wonder how to write an expression for the total.
The desired result set
May 31 2011 S043659 Long-Sleeve Logo jerse M 3 $86.52
Long-Sleeve Logo jersey XL 1 $28.84
Total $115.36
mountain bike socks M 6 $34.20
i used this expression but giving me an error.
`IIF((Fields!Product.value = Previous(Fields!Product.value),Sum(Fields!linetotal.value))`
There's actually a few things wrong with your expression.
The IIF doesn't have a 3rd argument for the ELSE value. In this case, you'd want to use 0. So the expression would be IIF the fields match then LineTotal Else 0.
You want to have the SUM on the outside of the IIF, otherwise it will only SUM one row.
The matching without the size is trickier. I have it trim off the last 4 characters to exclude the size for a match - it may not work depending on your other Product names.
=SUM(IIF(LEFT(Fields!Product.value, LEN(Fields!Product.value) - 4) = LEFT(Previous(Fields!Product.value), LEN(Fields!Product.value) - 4), Fields!linetotal.value, 0))
The expression reads the SUM of (IF the Product matches the Previous Product then the Line Total else 0).
All this being said, it would actually be easy to crate a parent group and GROUP BY on the parent product. Unfortunately, your data uses a comma to separate one type (jersey) by size but a space in another (socks) so I don't see how to do it. If they all had a comma, I would create a Calculated Field on the dataset to use the product-line up to the comma.
=LEFT(Fields!Product.value, INSTR(Fields!Product.value, ",") - 1)
IF your Product line is either a comma or space to separate, you might be able to use this for your Calculated Field:
=LEFT(Fields!Product.value,
IIF(INSTR(Fields!Product.value, ",") > 0,
INSTR(Fields!Product.value, ",") - 1,
InStrRev(Fields!Product.value, " ") - 1) )

How to not COUNT a value in SSRS matrix when value is NULL

I cannot make the my expression NOT count a NULL value in my SSRS matrix.
In my SSRS matrix, I have 2 columns one for AppraisalCompany and a count under the SubmittedDate column. In my report this what is happening:
Per Derrick's suggestion here is the change I made in the ColumnGroup properties for the SubmittedDate:
Here is my expression change in the ColumnGroup properties:
Unfortunately I got this error:
I'm suspicious of your Dataset, I'm not entirely sure how you're getting a null value to return 1 in the COUNT. I have been unable to reproduce your results.
Dataset Query
SELECT 'Drive In' AS AppraisalCompany, NULL AS SubmittedDate
UNION
SELECT 'Photo App - English', 'Dec-18'
Next I created a Row Group on AppraisalCompany and a Column Group on SubmittedDate.
I filtered the column group to remove the null grouping, using the expression =IsNothing(Fields!SubmittedDate.Value), operator <>, and Value true.
In the textbox in the matrix I used [Count(SubmittedDate)].
OUTUT
Appraisal Company | Dec-18
-------------------------------
Drive In | 0
Photo App - English | 1
By a Decimal (Number) datatype Nothing and 0 are the same. You can test this.
Put a tablix into your report with year from 2017 to 2019. Then put the year in a column of the tablix as a number format, then write the following expression in the detail textbox:
=CDec(IIF(CDec(Fields!Year.Value) = 2017, 0, Nothing))
After executing your report you will notice that every value in the year column is 0.
The same goes for the check. Both of these expressions will always return Yes. I basically check for 0 and the second one for for Nothing:
=IIF(CDec(IIF(CDec(Fields!Jahr.Value) = 2017, 0, Nothing)) = 0, "Yes", "No")
=IIF(CDec(IIF(CDec(Fields!Jahr.Value) = 2017, 0, Nothing)) = Nothing, "Yes", "No")
But remember your textbox/column has the be a number format.
So if you want to return Nothing and you display it in a number format textbox, it will show you a 0.
With this in mind it will make sense that a Count() returns the value 1 for 0 AND Nothing. So basically this will do the trick:
'Cont
=Sum(IIF(Fields!YourValue.Value = Nothing, 0, 1))

SSRS 2012 using switch statement with LookupSet count in group footer

Apologies for the long post.
I have an SSRS 2012 report looking at 2 datasets, Dataset1 and Dataset2.
Dataset2 may return 0, 1 or 2 records, so I'm using LookupSet to do this. From the records returned, I need to display several fields.
The report is grouped by Main_ID. In the footer of Main_ID, I am counting the number of returned records from Dataset2.
=LookupSet(Fields!MainID.Value, Fields!MainID.Value, Fields!Record_Name.Value, "Dataset2").Length
This works fine, I'm getting 0, 1 or 2 as appropriate.
In Dataset2 the field:
HasIP
will be used to determine which record in the array I display (as I need to test a field value in Dataset2).
I'm trying to use a switch statement to use the length of the LookupSet array to return the correct fields.
If there are no records (eg length of array = 0) then return the
number 2
If there is 1 record (eg length of array = 1), then return
Record_Name
If there are 2 records (eg length of array = 2), then
check the field HasIP.
If HasIP = YesYes, then return the Record_Name of that matching entry
If HasIP = YesNo, then return the Record_Name of that matching entry.
If all else fails, return the number 2
This is what I'm up to at the moment.
=Switch(
'if no returns returned, display the numeral 2
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=0, 2,
'if one return returned, then display Record Name
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=1,
LookUp(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2"),
'if two records returned, check the HasIP field. If HasIP = YesYes, then use this combination first.
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=2 AND
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!HasIP.Value,"Dataset2")(0)="YesYes",
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2")(0),
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=2 AND
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!HasIP.Value,"Dataset2")(1)="YesYes",
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2")(1),
'if two records returned, check the HasIP field. If HasIP = YesNo, then use this combination second.
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=2 AND
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!HasIP.Value,"Dataset2")(0)="YesNo",
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2")(0),
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=2 AND
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!HasIP.Value,"Dataset2")(1)="YesNo",
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2")(1),
'If all else fails, display the number 2
True, 2)
I'm getting the correct one returned for 2 records, but getting #ERROR for zero and 1 records. If I break the formula down to their individual parts:
=SWITCH(
LookupSet(Fields!MainID.Value, Fields!MainID.Value, Fields!Record_Name.Value, "Dataset2").Length=0,
2)
This works. The number 2 is displayed in my report where the are no entries in Dataset2 (the field is blank for any other combination).
If I use:
=SWITCH(LookupSet(Fields!MainID.Value, Fields!MainID.Value, Fields!Record_Name.Value, "Dataset2").Length=1, LookUp(Fields!MainID.Value,Fields!MainID.Value,Fields!Record_Name.Value,"Dataset2"))
This works fine for when I have 1 record returned from Dataset2 (the field is blank for any other combination).
No matter what I tried with the third part (handling the 2 records), I'm getting issues. I can get the correct Record Name returned, but I'm getting #ERROR for 0 or 1 records.
I've tried nesting the third part in IIF statements, flipping it round to look at the length first, etc, but no success.
I cannot edit the underlying query of either dataset.
Any guidance much appreciated.
Solved
See:
SSRS 2012. Replicate grouping results in report in Query Designer
Essentially it was adding a numeric value to HasIP and also add in the Primary key of the second dataset.
Then sort the LookupSet array to get the value you want.
In the expression, you can use this to assign a default value, even when there are not results returned by the LookupSet:
=iif(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Main_ID.Value,"Dataset2"
).Length>0,
Mid(
Split(
Join(
Code.JoinSorted(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12b.Value,"Dataset2")
)
,";")
,";").GetValue(0)
, 'start point of mid
InStr(
Join(
Code.JoinSorted(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12b.Value,"Dataset2"
)
)
,";")
,"$")
+1,
1),
"2"
)

DLookup: Need to be able to add to most recent entry

I have a form that is being used to create entries with different sequential fields. I'm currently using DLookup in order to do this, but I am running into some issues.
Me.txtProgramID.Value = DLookup("ProgramID", "tblMain", "Program = Forms!Form2!Combo.Value") + 1
Me.txtProgramNumber.Value = DLookup("Number", "tblMain", "Program = Forms!Form2!Combo.Value") + 1
Me.txtSequence2.Value = DLookup("Sequence2", "tblMain", "Program = Forms!Form2!Combo.Value") + 1
Me.txtSequence1.Value = DLookup("Sequence1", "tblMain", "Program = Forms!Form2!Combo.Value") + 1
There are four different values: ProgramID, ProgramNumber, Sequence1, and Sequence2. Everytime a new record is added, based on the contents of Combo, the contents of the new field should be the previous field + 1.
Lets say the contents of Combo is A and that the values for ProgramID, ProgramNumber, Sequence 1, and Sequence 2 are all 1. The new record for A should have them all as 2. The problem I'm having is that instead of DLookup finding the most recent entry, it is capturing the original, meaning that instead of having 1, 2, 3, 4, etc I have 1, 2, 2, 2.
Really what I need to know is how to make DLookup grab the most recent record in regards to the respective profiles.
You might be able to get away with DMax, but once you have more than one user, all bets are off:
Me.txtProgramID.Value = DMax("ProgramID", "tblMain", _
"Program = Forms!Form2!Combo.Value") + 1
ProgramID should probably be an autonumber, so there is no need to get the next number, it is handled autmatically. However, there is no guarantee that an autonumber is the previous number +1. I am not sure why you would have a programID and a program number.
A proper sequential number is a but more complicated: Access VBA: Find max number in column and add 1