SSRS : How to aggregate on certain values of a column? - reporting-services

I have a column (Say PriceA) in SSRS which gets populated based of Expression.
PriceA.value
IIF((Fields!MOP.Value = "PRIV" OR Fields!MOP.Value = "FACI" OR Fields!MOP.Value = "GUAR" OR Fields!MOP.Value = "CASH"
OR Fields!MOP.Value = "CHRG" OR Fields!MOP.Value = "INV"),
IIF(Fields!UDRx.Value,"U/D",Fields!TtlPrice.Value),
IIF(Fields!elecadj.Value="0" AND Fields!elecadj2.Value="0", IIF(Fields!UDRx.Value,"U/D",IIF(Fields!MOP2.Value="0",Fields!Copay.Value,"")),
IIF(Fields!UDRx.Value,"U/D",Fields!PatientPayAmt.Value)
)
)
The Value in PriceA can be "U/D" or Zero or some value as "150.00". Now I want to display at the end of the report, an aggregate of the rows in that column, discarding the values $0.0 and "U/D".
How do I do that?
I also have another column where it shows values based off off 2 different expression, that I need to aggregate. That one is not working either.
2ndPriceA_expression
IIF((Fields!MOP.Value = "PRIV" OR Fields!MOP.Value = "FACI" OR Fields!MOP.Value = "GUAR" OR Fields!MOP.Value = "CASH"
OR Fields!MOP.Value = "CHRG" OR Fields!MOP.Value = "INV"), " ",
IIF(Fields!elecadj.Value="0" AND Fields!elecadj2.Value="0",
IIF(Fields!UDRx.Value="0",Format(Fields!TtlPrice.Value,"C"),"U/D") ,
IIF(Fields!UDRx.Value="0",Format(Fields!Payor1PaidAmt.Value,"C"),"U/D")
)
)
2ndPriceB_expression
IIF(Fields!MOP2.Value<>"",
IIF(Fields!elecadj.Value="0" and Fields!elecadj2.Value="0"
,IIF(Fields!UDRx.Value="1","U/D",Fields!Copay.Value)
,IIF(Fields!UDRx.Value="1"
,"U/D"
,IIF(Fields!elecadj2.Value="0"
,Fields!Copay.Value
,Fields!Payor2PaidAmt.Value
)
)
)
," ")
I need to show aggregate of 2ndPriceA & 2ndPriceB in a 3rd cell. Sorry I'm 2 weeks new to SSRS.
Thanks in advance. any help is appreciated.

Just do a SUM() of an IIF() expression that will filter out the values you don't want to SUM.

Related

How can I get both values from a multi-dimensional array in a multi-select ssrs report parameter?

I have the following SQL query in an SSRS dataset:
SELECT c.Id
, c.LastName + ', ' + c.FirstName AS CustomerName
, r.PurchaseDate
FROM tblCustomer c
JOIN Receipt r ON r.CustomerId = c.Id
WHERE StoreId = #storeId
I also have three parameters for the report: start date, end date, and customer. The workflow is: select a start date, select an end date, then the above dataset is filtered to only show the customer names in the multi-select dropdown parameter that have a receipt date within the start and end dates. The problem is, when customer has multiple receipts within the date range, the customer shows up more than once in the dropdown parameter. I copied the VB code that filters out the duplicates:
Public Shared Function RemoveDuplicates(parameter As Parameter) As String()
Dim items As Object() = parameter.Value
System.Array.Sort(items)
Dim k As Integer = 0
For i As Integer = 0 To items.Length - 1
If i > 0 AndAlso items(i).Equals(items(i - 1)) Then
Continue For
End If
items(k) = items(i)
k += 1
Next
Dim unique As [String]() = New [String](k - 1) {}
System.Array.Copy(items, 0, unique, 0, k)
Return unique
End Function
which works great, except that it only shows the Customer ID in the dropdown.
How do I get the multi-select dropdown to have the CustomerName as the label and the Customer Id as the value?
You can get around this by doing a cascading parameters.
I don't see where the start_date and end_date parameters are being used..
You can create another dataset, lets call it customers.
Your customer dateset query will be:
SELECT DISTINCT
c.Id
, c.LastName + ', ' + c.FirstName AS CustomerName
FROM tblCustomer c
JOIN Receipt r ON r.CustomerId = c.Id
WHERE StoreId = #storeId
-- and Receipt.somedate between #start_date and #end_date
Set your customer parameter to source it's data from this query. You will only ever have customers from the above select..
Go to the customer parameter.. available value -> customer data set
Set the values in there - value field will be ID and Label field will be name
Of course, your main dataset need to have #customerID filter along with #start_date and #end_date

SSRS calling Monthname inside switch

I have a report that can be grouped monthly, quarterly, or annually. With my data, I have two columns, ActivationYear and ActivationMonthOrQuarter. I get ActivationMonthOrQuarter when I run the report, so I can have values ranging from 1-12 (1-12 if monthly, 1-4 if quarterly). If the grouping option is annually the ActivationMonthOrQuarter field will be null.
The issue I am running into is that it seems to still try and call the MONTHNAME function even when I use GroupingOption = "Annual" and throws an error since it cannot call it on a NULL value.
=SWITCH(
Parameters!GroupingOption.Value = "Annual", "",
Parameters!GroupingOption.Value = "Quarterly", "Q" & Fields!ActivationMonthOrQuarter.Value,
Parameters!GroupingOption.Value = "Monthly", MONTHNAME(Fields!ActivationMonthOrQuarter.Value)
)
Try:
=SWITCH(
Parameters!GroupingOption.Value = "Annual", "",
Parameters!GroupingOption.Value = "Quarterly", "Q" & Fields!ActivationMonthOrQuarter.Value,
Parameters!GroupingOption.Value = "Monthly", MONTHNAME(IIF(IsNothing(Fields!ActivationMonthOrQuarter.Value),1,Fields!ActivationMonthOrQuarter.Value))
)
Let me know if this helps.

Not equal expression

I'm looking at using 4 expressions 3 being equals and 1 being not equals but is not counting the not equals
im using .
=sum(iif(Fields!NEW_SUPPLIER.Value = "Eon", 1,0)
And (Fields!WeekDay.Value = "Monday")
And (Fields!UTILITY.Value = "Elec")
And (Fields!OLD_SUPPLIER.Value <> "Eon"))
Please can some one point me in the right direction.
You need to make sure all the checks are in the first argument of the IIf expression:
=sum
(
iif
(
Fields!NEW_SUPPLIER.Value = "Eon"
And Fields!WeekDay.Value = "Monday"
And Fields!UTILITY.Value = "Elec"
And Fields!OLD_SUPPLIER.Value <> "Eon"
,1
,0
)
)

complicated nested if statement

I am hoping that someone can modify my existing query and provide me the SQL for me to copy and paste based on my 2 conditions listed below. Thank you very much in advance, Nathaniel
condition#1. compare the values of the following 3 fields and output the lowest value of the 3 fields (exlcuding null values):
"lowest list price"
"lowest price sold"
"lining_price_exception"
example: if "lowest list price" = 3 and "lowest price sold" = 7 and "lining_price_exception" =null, then the output should be 3.
conditon#2. if the value in the field "overide_price" is not null, then the field "overide_price" should replace the output from from condition #1. if the value in "overide_price" is null, then output the value from condition#1.
SELECT q.Part_ID, q.[lowest list price], q.[lowest price sold],
q.lining_price_exception, q.overide_price
FROM qry_to_determine_wmx_price_01 AS q;
This should be complicated enough for you. Here is tested SQL no need to define any VBA methods which meets both criteria:
Select q.Part_ID,
q.[lowest list price],
q.[lowest price sold],
q.lining_price_exception,
q.overide_price,
NZ(q.overide_price,
IIf(q.[lowest list price]<q.[lowest price sold] OR ISNULL(q.[lowest price sold]),
IIf(q.[lowest list price]<q.lining_price_exception or ISNULL(q.lining_price_exception),
NZ(q.[lowest list price],"No valid Price listed"),
q.lining_price_exception
),
IIf(q.[lowest price sold]<q.lining_price_exception or ISNULL(q.lining_price_exception),
NZ(q.[lowest price sold],"No valid Price listed"),
q.lining_price_exception
)
)
) as Actual_Low_Price
from qry_to_determine_wmx_price_01 as q;
If you will be running your query from within an Access session, you can use a custom VBA function to make this easier.
Testing the MinValue() function in the Immediate window ...
? MinValue(4, Null, 2)
2
? MinValue(Null, Null, 2)
2
? MinValue(Null, Null, Null)
Null
So you could tackle your condition#1 in a query like this ...
SELECT
MinValue(
q.[lowest list price],
q.[lowest price sold],
q.lining_price_exception
) AS lowest_value
qry_to_determine_wmx_price_01 AS q;
As for your conditon#2, I think the Nz() function would be useful.
Nz(q.overide_price, MinValue(q.[lowest list price], q.[lowest price sold],
q.lining_price_exception))
Basically that Nz() expression says "return overide_price if it's not Null, otherwise return the value from the MinValue() expression".
This is the function I mentioned.
Public Function MinValue(ParamArray pValues() As Variant) As Variant
Dim i As Long
Dim iUbound As Long
Dim varMin As Variant
iUbound = UBound(pValues)
varMin = Null
For i = 0 To iUbound
If Not IsNull(pValues(i)) Then
If Not IsNull(varMin) Then
If pValues(i) < varMin Then
varMin = pValues(i)
End If
Else
varMin = pValues(i)
End If
End If
Next
MinValue = varMin
End Function

Linq to Sql - Use sum in the where clause

I'm trying to select orders that have either over or under 2000 products ordered in them, depending on other values. I need to select the information from the Orders table, but check this value in the OrdersProducts table, specifically the sum of OrdersProducts.ProductQty. I also need to do this using predicate builder, because of other requirements. So far, I have this, but it isn't returning the results correctly. Its using nested Lambda expressions, which I didn't know I could do but I tried it and it works, but its not returning correct results.
Dim getOrders = From d In db.Orders _
Where d.Status = OrderStatus.Approved _
Select d
' Then a for loop adding parameters via Predicatebuilder...
If over2000 = True Then
' over 2000
predicate1 = predicate1.And(Function(d) (d.OrderProducts.Sum(Function(c) c.ProductQty > 2000)))
Else
' under 2000
predicate1 = predicate1.And(Function(d) (d.OrderProducts.Sum(Function(c) c.ProductQty < 2000)))
End If
basePredicate = basePredicate.Or(predicate1)
' End For loop
getOrders = getOrders.Where(basePredicate)
I removed some code for brevity but I think that gets the point across. How can I do this?? Thanks!
Try changing this:
(d.OrderProducts.Sum(Function(c) c.ProductQty > 2000))
to this:
(d.OrderProducts.Sum(Function(c) c.ProductQty) > 2000)
I haven't built this to test it, but it appears that it was currently trying to sum the results of a boolean comparison instead of summing the quantities and then comparing.