I'm having a problem trying to calculate the average of period totals for periods that have non-zero totals. The data that's being returned looks like this (simplified)
Data Being Returned
and I'm displaying it like this:
Format in SSRS
What I need is to calculate the sum of all the values (27,377) divided by the number of months with non-zero totals (5). So it would be 27,377/5 = 5,475.40.
I've tried CountDistinct combined with iif without success.
Thanks
In custom code you have to create a function that will calculate month count
Public Dim months As Integer
Public Function MonthCount (month As String, val As Decimal) As Decimal
If val <> 0 Then
months = months + 1
End If
Return month
End Function
The function takes as parameters the month sum and the month name.
If the month sum if is not equal to zero it increments the month counter.
Finally it return the month value in order to display it in the matrix
In the cell of the month name value, you call the custom function.
For your average, the expression should be like the one below
Sum(Fields!val.Value, "dataset") / Code.months
Use Sum with IIF expression something like
=Sum(IIF(Fields!TotalCost.Value > 0,1, 0))
Related
I am using Microsoft report builder.
I have columns in a column group that are grouped by weeks. One of the field of this scope should be subtraction of one column value from current week minus column value from another week. Is this possible in ssrs?
rough dataset:
Create table #Test
(
JobNum [nvarchar](20)
,YearNumber int
,WeekNumber int
,Column1 int
)
insert into #Test
VALUES
('job1',2022,1,10),
('job2',2022,1,50),
('job1',2022,2,15),
('job2',2022,2,60),
('job1',2022,3,20),
('job2',2022,3,70)
select * from #Test
drop table #Test
and groups in builder
Onestly I don't know if this is possible (I don't think is possbile to access the scope of the "previous group") but maybe you can use this workaroud.
0. Start dataset
I've started from this dataset, somehow similar to yours
And this is the tablix object
1. Create a calculated field on your dataset
Create a new simple filed in your dataset to have for each week number the previous week number
=Fields!Week.Value - 1
2. Add a custom function to sum the result of a LookupSet
Follow this guide to add this custom function to the report enabling us to sum the result of a LookupSet function (many thanks to the author!). We will use this in the next point.
Function SumLookup(ByVal items As Object()) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
Dim ct as Integer = New Integer()
suma = 0
ct = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
ct += 1
Next
If (ct = 0) Then return 0 else return suma
End Function
3. Add a costum expression for the column 2
=
Sum(Fields!Value.Value) -
Code.SumLookup(LookupSet(Fields!PreviousWeekNumber.Value,Fields!Week.Value,Fields!Value.Value, "DataSet1"))
The LookupSet function retrive the set of values in the selected table/scope (Dataset1 in the example) wich have the week number equal to the previuos week number (in our the default scope, the column group scope). The custom function "SumLookup" enable us to sum the VariantArray (or Nothing if there is no match) returned by the LookupSet function.
4. Results
This is the result:
if you need a different result for the first week just add a condition to the custom expression for the column2
EDIT
If you have also a row group like this:
You can modify the previuos expression to this:
=
Sum(Fields!Value.Value) -
Code.SumLookup(LookupSet(Fields!JobNum.Value & Fields!PreviousWeekNumber.Value,Fields!JobNum.Value & Fields!Week.Value,Fields!Value.Value, "DataSet1"))
Achivieng a LookupSet based on multiple conditions.
I hope someone would be able to assist\help.
I have a Tablix that I'm trying to populate with three separate (summed) values (Current Month, Current Year and Previous Year) from one field based on a parameter. My parameter is set as yyyymm. My expression logic is as follows for each summed value:
Sum Current Month values
=SUM(Fields!Quantity.Value),IIF(Parameters!YearMonth.Value) = CDATE(Now()), "yyyyMM")
Sum Current Year values
=SUM(Fields!Quantity.Value),IIF(Parameters!YearMonth.Value) = CDATE(Now()), "yyyy")
Sum Previous Year values
=SUM(Fields!Quantity.Value),IIF(Parameters!YearMonth.Value) = CDATE(Now()), "yyyy")-1
I'm getting the following error when attempting the above expressions:
The Value expression for the textrun Quantity5.Paragraphs[0].TextRuns[0] contains an error: [BC30205] End of statement expected
As Harry pointed out, the IIF statement syntax is
IIF([Expression to evalute], [Expression to return if true], [Expression to return if false])
Also, I think you need to format the date you get back from now() so it matches the format of your parameter.
So, taking your first expression it should be something like
=SUM(
IIF(Parameters!YearMonth.Value = FORMAT(Now(), "yyyyMM"), Fields!Quantity.Value, Nothing)
)
So starting at the inner expression, we compare the parameter to today's date formatted as yyyyMM. If the match the return the value of the Quantity field, if not return nothing.
The outer SUM then just sums all these results.
I need to calculate a YTD value each day based on a budget table. My table looks like this:
Month Rev-50100 Rev-50101
1/31/19 75000.00 364.27
2/28/19 76000.00 360.57
3/31/19 82000.00 391.58
I'm able to handle the MTD pretty easily with a "PerDay" column that takes the monthly value and divides by the number of days in that month, then multiply the PerDay value based on the day of the month.
For YTD, I would need to add each previous month, then a MTD value for the current month. So on March 2nd my expected outcome is (75000+76000+ ((82000/31)*2)) = 156,290.32
I have been trying to use a combination of RunningValue and my MTD calculation, but RunningValue is adding in the entire current month, and not letting me get YTD value.
If I'm understanding correctly, I may have a solution for you. You need each month from the Rev-50100 column, except the last value and add the MTD value instead.
To solve this, you can use the Last function to remove the last value from the RunningValue and then add MTD.
=RunningValue(Fields!Rev-50100.Value, Sum, Nothing)
- Last(Fields!Rev-50100.Value, Nothing)
+ ReportItems!MtdTextbox.Value
I'm trying to sum a net balance based on the earliest date in an SSRS report. In this case there are only 2 dates, but there can be more dates not more than 7 days.
Here's a sample of my data:
Here's what I'm trying to get with the earliest date of 10/26/15:
I've tried the following code, but not able to get this to work:
=Sum(IIf(DateDiff("d",Fields!SettleFullDate.Value,today())>=7
and DateDiff("d", Fields!SettleFullDate.Value, today())<7
and Fields!SETTLEBALANCE.Value>0), Fields!SETTLEBALANCE.Value, 0)
Update: I tried the code below and keep getting an error on the report. Could it be that I need to change the date field to an integer?
Thanks in advance for your help!
To compare the sum of values of two dates, the maximum and minimum in a set you can use the following equation
=Sum(iif(Fields!myDate.Value = Max(Fields!myDate.Value), Fields!myVal.Value, 0))
-Sum(iif(Fields!myDate.Value = MIN(Fields!myDate.Value), Fields!myVal.Value, 0))
This Sums all the values that match the maximum date in the dataset together, and sums all the values that match the minimum date in the dataset together, and takes one from the other.
It is irrespective of which dates you ask to be received, the above approach will work only against the records that you return to SSRS. So if you have a filter (WHERE clause) to return records between Date1 and Date2 this will still apply (Note - don't actually use 'Between' in the query)
Rather than using the maximum and minimum dates as listed here, you could also calculate a date similar to your original approach using
dateadd("d", -7, Fields!MySpecificDate.Value)
And insert that to the expression above.
Hopefully this is what you require - if not please let me know.
I've a column with values for each month of the year.
Then in parameters of my report i would be able to set the month.
SSRS should return this column with values for each month of the year (like if there's no parameter) but the sum at the bottom of this column should return the sum of the value from the beginning of the year to the selected month.
Is it possible?
Yes. If I'm understanding correctly, you want something like the image below:
Assuming your MonthYear column is a DateTime and your month parameter is an Integer, you can use an expression like the following to conditionally sum the total based on the parameter value:
=Sum(IIf(Month(Fields!MonthYear.Value) <= Parameters!Month.Value, Fields!Value.Value, 0))