Adding Multiple Datasets and total - reporting-services

Phased Budget
Pretty new to all this.
I'm building a report in SSRS with multiple datasets.
The report has 12 trading periods and an Annual Total.
I've added a total line which sums the budget for 2 datasets using an expression.
The total works fine for the Annual Total but not for any of the periods. It just shows the annual total in each month.
What am I doing wrong?

You can use custom code to store DataSet1 values to an array and then use the stored values to calculate net margin.
Add the following custom code to your report
Dim i1 As Integer
Dim i2 As Integer
Dim sumarray(12) As Decimal
Public Function setSum(ByVal v As Decimal) As Decimal
sumarray(i1) = v
i1 = i1 + 1
Return v
End Function
Public Function getSum() As Decimal
i2 = i2 + 1
Return sumarray(i2-1)
End Function
For your gross margin monthly(dataset1) total use the expression:
= Code.setSum( SUM(Fields!Bud.Value) )
For your net margin monthly total (dataset2) use the expression:
= SUM(Fields!Bud.Value) + Code.getSum()

Related

MS Access: Calculate First Day of Next Quarter based on Date field

On a MS Access Table, I would like to have a column that has the first day of the following quarter based on a date in another column.
For example, I have a Start_Date column with a value of 2018-02-04, I would like the record on that column to show that the following quarter would start on 2018-04-01.
I have this working fine in Excel using the following formula:
=DATE(YEAR(M2),((INT((MONTH(M2)-1)/3)+1)*3)+1,1)
In Access, I added the column choosing Calculated Field > Date/Time, but could not figure out a way to get it to work like an Excel.
Has anyone tried this before or know of a possible solution? Thanks
You can use this function:
Public Function DateNextQuarterFirst( _
Optional ByVal datDateThisQuarter As Date) As Date
Const cintQuarterMonthCount As Integer = 3
Dim intThisMonth As Integer
If datDateThisQuarter = 0 Then
datDateThisQuarter = Date
End If
intThisMonth = DatePart("q", datDateThisQuarter) * cintQuarterMonthCount
DateNextQuarterFirst = DateSerial(Year(datDateThisQuarter), intThisMonth + 1, 1)
End Function
To use it in a query with SQL similar to this:
Select *, DateNextQuarterFirst([Start_Date]) As NextQuarterFirst
From YourTable

Average depending of parameter given

I have a month parameter called DimTiempoMes So I when I run preview of report I select that month in combo box it can be from 1 to 12.
If I select 1 it return value of Fields!InventarioDirecto_Monto.Value of month 1
If I select 3 it return value of Fields!InventarioDirecto_Monto.Value of month 1,2 and 3 etc...
Now I want to get Average depending of selected Fields!InventarioDirecto_Monto.Value
For Example: If I selectDimTiempoMes with value10, formula will be for example:
InventarioDirecto_Monto Value / DimTiempoMes.Value
Execution: Sum(1,2,3,4,5,6,7,8,9,10) / 10
So I try next expression:
=SUM(Fields!InventarioDirecto_Monto.Value) / CINT(replace(left(Split(Parameters!DimTiempoMes.Value,"[").GetValue(3),2),"]",""))
Problem is SUM(Fields!InventarioDirecto_Monto.Value)always come with sum of 12 months values, so it always sum values of 12 months
Question is, How can I get only values that Fields!InventarioDirecto_Monto.Value return depending of selected parameter DimTiempoMes?
Some Data:
In this image I have 3,719 in first column because I use DimTiempoMes as 1: so my colum Acum who have expression should be 3,719 too because SUM(Fields!InventarioDirecto_Monto.Value) equals to 3719 and divisor equals to 1 because I search 1. So 3,719/1 = 3791 and I getting 40,593
Other Example: if I use DimTiempoMes as 4
There I should get 3719 + 3639 + 3378 + 2999 / 4 = 3433.73 and I receive 10148
Update:
I also try adding custom code like:
Function Promedio(ByVal items As Object(),ByVal meses As Object(), ByVal Valor1 As integer) 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
For Each mes As Object In meses
IF CINT(mes) <= Valor1
Then ct = 1
For Each item As Object In items
If (ct = CINT(mes))
Then suma += Convert.ToDecimal(item)
End If
ct += 1
Next
End If
Next
suma = suma/valor1
If (Valor1 = 0) Then return 0 else return suma
End Function
And Expression something like:
=Code.Promedio(LookupSet(Fields!Dim_ConceptosInventario_.Value, Fields!Dim_ConceptosInventario_.Value, Fields!InventarioDirecto_Monto.Value, "DS_SubInventarioDirecto"),LookupSet(Fields!Dim_ConceptosInventario_.Value, Fields!Dim_ConceptosInventario_.Value, Fields!nPeriodo.Value, "DS_SubInventarioDirecto"),CINT(replace(left(Split(Parameters!DimTiempoMes.Value,"[").GetValue(3),2),"]","")) )
But I donĀ“t get desire results

SSRS Display Totals by Group

I am trying to display the total for records that are grouped together.
I am using the code below under Report Properties -> Code
Dim public totalBalance as Integer
Dim public Cnt as Integer
Public Function AddTotal(ByVal balance AS Integer ) AS Integer
totalBalance = totalBalance + balance
Cnt=Cnt+1
return balance
End Function
Public Function GetTotal()
return totalBalance
End Function
[Expression A is =Code.AddTotal(sum(DateDiff(dateinterval.Second, Lookup(Fields!dg_InteractionId.Value, Fields!Id.Value, Fields!StartDate.Value, "DataSet1"), Lookup(Fields!dg_InteractionId.Value, Fields!Id.Value, Fields!EndDate.Value, "DataSet1")))/CountRows())
While Expression B is =Code.GetTotal()
AS you can see the total 1651 for the top group is correct but the total for the second group 2597 is incorrect as it includes the total of the first group together. The correct result desired is 946 for the second group total.
Kindly advise.
You can change your GetTotal() to reset the totalBalance variable when is called to display the group total
Public Function GetTotal()
Dim bal AS integer
bal = totalBalance
totalBalance = 0
return bal
End Function
Because of this change, you should also create a separate variable for the grand total which will be updated in the AddTotal() function, and create a GetGrandTotal() function which will return its value.
Also since you have declared your variables as public you can display them directly without functions like =Code.totalBalance

SSRS running Total On Multiple Columns?

I am creating one SSRS report in which I have one Tablix the data of that Tablix shown as below,
One Parameter --> Balance = 100
firstamt SecondAmt ThirdAmt RunningTotal
10 15 20 145
02 05 01 153
-30 -20 -03 100
So basically my RunningTotal fields value should be
RunningTotal = Balance + firstAmt+SecondAmt+thirdAmt and then update
Balance = Balance + firstAmt+SecondAmt+thirdAmt (Or RunningTotal)
and for then next row should use Balance= RunningTotal then calculate the next row runningtotal and so on. I tried using custom code, RunningValue but still got no luck.
Any help would be great, Thanks. And also my reporting server is 2008.
(In case of confusion feel free to comment.)
I tried following Custom code but it doesn't work
public dim finalRunningTotal as decimal = 0
public function CalculateRunningTotal (totalAmount as decimal, CheckAmount as decimal,pstAmount as decimal) as decimal
dim valueToReturn as decimal = finalRunningTotal + totalAmount + CheckAmount +pstAmount
finalRunningTotal = valueToReturn
return valueToReturn
end function
Here's one of doing it - use an expression like:
=RunningValue(Fields!FirstAmt.Value + Fields!SecondAmt.Value + Fields!ThirdAmt.Value
, Sum
, Nothing)
+ Parameters!Balance.Value
You may have to change Nothing to a different Scope depending on how your table is set up.
Works for me in a simple example:
Here the example is as above.
To simplify this you could add a Calculated Field to the Dataset like:
=Fields!FirstAmt.Value + Fields!SecondAmt.Value + Fields!ThirdAmt.Value
Which makes the RunningValue expression simpler:
=RunningValue(Fields!MyCalculatedField.Value
, Sum
, Nothing)
+ Parameters!Balance.Value

disctinctcount from another dataset having multiple grouping

i have report that generates number of policies from 2 datasets.
the table is linked to dataset1.
i used the formula
CountDistinct(Fields!Policy_ID.Value)
my problem is how to get distinct count for the field Policy_ID from the second dataset "AccountingV10Dataset", specially that i have grouping Contract_Start_Month, Contract_Cover_Type, and Primary_LOB.
below a screen shot to the report design to help you to understand my request
the report should be generated as below:
I would derive the Distinct Count back in the Dataset layer e.g. in SQL. The SSRS Aggregate Functions only go so far.
I managed to solved this way:
i created the below code to calculate disting count from another dataset:
Dim suma As Decimal = New Decimal()
Public Function SumLookup(ByVal items As Object(),ByVal CountSum As String) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim ct as Integer = New Integer()
Dim PolId as Integer = New Integer()
suma = 0
ct = 0
PolId = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
If (CountSum = "SUM") then
ct += 1
Else If (PolID <> Convert.ToInt32(item)) then
ct += 1
END If
PolId = Convert.ToInt32(item)
Next
If (ct = 0) Then return 0 else If(CountSum = "SUM") then return suma else return ct
End Function
Public Function GetMyVal() as Decimal
GetMyVal = suma
End Function
and i call this function by:
=code.SumLookup(Lookupset(Fields!Contract_Start_Month.Value & "-" & Fields!Contract_Cover_Type.Value & "-" & Fields!Primary_LOB.Value,Fields!Contract_Year_Start_Month.Value & "-" & Fields!Cover_Type.Value & "-" & Fields!LOB.Value,Fields!Layer_ID.Value, "AccountingV10Dataset"),"COUNT")
Hope my solution will helpfull for other users as it did for me.
Regards,