ssrs column group subtraction from previous value - reporting-services

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.

Related

SSRS Expression to Sum value IIF YearMonth = Parameter

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.

SSRS - Average of periods with values

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))

SSRS if day = Friday use dataset2

So I have an SSSRS REPORT with 2 data sets. If the day of the week is Friday I need to show the data in dataset 2 otherwise to use dataset 1. How can I accomplish this? is there a built in expression or function to do this???
You can't set DataSetName property at runtime, but you can select the data you want to return in your dataset based on the week day.
IF DATENAME(WEEKDAY,GETDATE()) = 'Friday'
select categoryDS1 Category, salesDS1 Sales from tableDS1
ELSE
select categoryDS2 Category, salesDS2 Sales from tableDS2
This will work if both SELECT statements have the same columns name
and types.
Let me know if this helps.
You can place a copy of both reports in the report and simply set the visibility for the one you want to show. The expression for the Visibility property for DataSet1 would be:
=IIf(WeekdayName(Weekday(Today)) = "Friday", True, False)
Swap the result for DataSet2.

SUM of a SUM in SSRS 2008

I've this report
Here I make the first sum because I've grouped values from each month (months are "Gennaio", "Febbraio", "Marzo" etc etc.). These values are hidden, but anyway I get the sum and I display the sum for each month.
Then I should make the second sum that use values for each month and display the total for each category. Categories are "TOTALE LAVORI RESTAURO", "TOTALE LAVORI EDILE" etc.)
This is the final sum, where I sum values from each category.
Everything is working well, but now I have to add a "month" parameter to the report that returns sums until a selected month. This parameter changes the sum 1 with this expression:
=Sum(IIf(Fields!mese.Value <= Parameters!mese.Value, Fields!costi.Value, 0))
Now, how should I change expression in SUM2 and SUM3 to work with this parameter?
If I copy that code, ther returns #Error and as far as I know I can't use ReportItems sum.
So any suggestion?
SUM #1 could remain Sum(Fields!costi.Value) because you need to display every months.
i.e.: display GIUGNO even if Parameters!mese.Value = 4 (APRILE).
So you have only to change SUM #2 and #3 because TOTALE LAVORI RESTAURO and TOTALI must show only costi from GENNAIO to Parameters!mese.Value; i.e. if Parameters!mese.Value = 4 display only GENNAIO-APRILE even if we have details about GIUGNO.
The expression gave error because you have NULL value in Fields!costi.Value or Fields!mese.Value: convert this value to zero in your DataSet and you won't have problems.

Table of averages by date for multiple IDs

I have a table full of items that each have a unique ItemID. There is another table with information on tests that are done on these items around once a month (some may have multiple tests per month and some may have none some months) and each test entry has a full date mm/dd/yyyy. I would like to display the average of one of the fields over all the data on a monthly basis. So assuming what I'm averaging is the item weight, I want to end up with a table like this:
Date AvgOfWeight
1/09 24.55
2/09 28.64
3/09 22.39
and so on...
I know that I can do a query to do averages and I can group the averages by certain criteria, but is there an easy way to group mm/dd/yyyy dates by month?
I love this strzero function that can be used here this way:
m_monthYear = strZero(year(date()),4) & "-" & strZero(month(date()),2)
Results look like 2009-09 and can be ordered ascending, as they begin with the year. You'll just have to create this strZero function as follows:
Public function strZero(x_myLong as long, x_length as integer) as string
Dim m_strZero as string
m_strZero = trim(str(x_myLong))
do while len(m_strZero) <= x_length
m_strZero = "0" & m_strZero
loop
strZero = m_strZero
end function
This was written on the fly. Please check all parameters (if any) for str, add error management, and adapt to your own needs. You might like to pass x_myLong as a variant, in order to return a '0000' string for example. This function has multiple other uses when converting numbers to fixed length strings.