How to fix the error dividing by zero in my expression in visual studio reports? - reporting-services

I created report in visual studio.The problem is with dividing by zero in total fields.
I have one column with value 00:03:15 and another column with 00:00:00
I want to sum these columns and want to show me average value from first and second column.
I used this expression ( formula ) in my case :
=Format(
TimeSerial(0,0,
Round(iif(sum(Fields!N_INBOUND.Value)=0,0,
sum(Fields!T_INBOUND.Value/Fields!N_INBOUND.Value))
+
iif(sum(Fields!N_INBOUND.Value)=0,0,
sum(Fields!T_HOLD.Value/Fields!N_INBOUND.Value)
+iif(first(Fields!HANDLING_TIME_MEASURE_TYPE.Value)=2,
sum(Fields!N_INBOUND.Value-Fields!N_TRANSFERS_TAKEN.Value),0))
+iif(sum(Fields!N_INBOUND.Value)=0,0,
sum(Fields!T_CONSULT.Value/Fields!N_INBOUND.Value)
+iif(first(Fields!HANDLING_TIME_MEASURE_TYPE.Value)=2,
sum(Fields!N_CONSULT.Value-Fields!N_TRANSFERS_TAKEN.Value),0)))/count(Fields!PRESENTATION_NAME.Value))
, "HH:mm:ss")
I used this https://sqldusty.com/2011/08/01/ssrs-expression-iif-statement-divide-by-zero-error/ and looks like :
=Format(
TimeSerial(0,0,
Round(
iif(count(Fields!PRESENTATION_NAME.Value)=0,0,
(iif(sum(Fields!N_INBOUND.Value)=0,0,
sum(Fields!T_INBOUND.Value/Fields!N_INBOUND.Value))
+
iif(sum(Fields!N_INBOUND.Value)=0,0,
sum(Fields!T_HOLD.Value/Fields!N_INBOUND.Value)
+iif(first(Fields!HANDLING_TIME_MEASURE_TYPE.Value)=2,
sum(Fields!N_INBOUND.Value-Fields!N_TRANSFERS_TAKEN.Value),0))
+iif(sum(Fields!N_INBOUND.Value)=0,0,
sum(Fields!T_CONSULT.Value/Fields!N_INBOUND.Value)
+iif(first(Fields!HANDLING_TIME_MEASURE_TYPE.Value)=2,
sum(Fields!N_CONSULT.Value-Fields!N_TRANSFERS_TAKEN.Value),0)))/iif(count(Fields!PRESENTATION_NAME.Value)=0,1,count(Fields!PRESENTATION_NAME.Value)) ) ) )
, "HH:mm:ss")
But shows me an ERROR.Anyone who know how to fix that?

Your data is something like below, Correct me if I am wrong. At the Bottom I have added Total for both columns.
Note: You will have to take care of what type your column (T_INBOUND and N_INBOUND) return values.
Now to create a Total for T_INBOUND and N_INBOUND
To add totals for a column group In the tablix data region row group
area, right-click a cell in the column group area for which you want
totals, then point to Add Total, and click Before or After.
A new column outside the current group is added to the data region,
and then a default total is added for each numeric field in the
column.
Link:
https://learn.microsoft.com/en-us/sql/reporting-services/report-design/add-a-total-to-a-group-or-tablix-data-region-report-builder-and-ssrs?view=sql-server-2017
Once you have Total you can create create a Cell Either next to your Total or below Total and add expression as below
IIF(IsNothing(Fields!Total_N_BOUND.Value) OR Fields!Total_N_BOUND.Value=0,0,
Fields!Total_T_BOUND.Value/Fields!Total_N_BOUND.Value)

Related

SSRS 2016 - How to calculate a percentage difference between two values in a group?

I'm creating a report that shows the sales of item before a date range and after a date range.
The part I ran into trouble with is the percentage difference between the total sales on Date 1 and Date 2.
Items can have no sales for a certain week.
The user can select multiple item ID's in the item ID parameter.
I can update the question to post my SQL query if needed.
What I've tried
Since I put a group on item ID I thought the First and Last functions would work.
Here's my expression on the column PCT.
=(Last(Fields!total_sales1.Value, "Date1")- First(Fields!total_sales1.Value, "Date1")) / First(Fields!total_sales1.Value, "Date1") * 100
But when I run the report I get the following results.
I need an expression on PCT column that will give me a percentage difference for each item pairs.
It looks like your scope is incorrect. Check what the rowgroup is called where you group by item id (let's say the row group is called "yourItemRowGroupName").
Then change you expression to use that scope rather than "Date1".
In fact you may not need the scope at all as it should work within the scope that expression sits (in your case, within your ItemID group.).
So try
=(Last(Fields!total_sales1.Value)- First(Fields!total_sales1.Value)) / First(Fields!total_sales1.Value) * 100
Or..
=(Last(Fields!total_sales1.Value, "yourItemRowGroupName")- First(Fields!total_sales1.Value, "yourItemRowGroupName")) / First(Fields!total_sales1.Value, "yourItemRowGroupName") * 100
You may have to handle divide by zero but try this to start with before you add any more complications.

SSRS - Sum Last 12 items

I have a Total column and am summing all the values in the row with:
=Sum(Fields!MyField.Value)
How can I sum only the last 12 items?
This is valid:
=Last(Fields!MyField.Value)
I need something like the below...
=Sum( LastX(Fields!MyField.Value, 12) )
You could achieve this result by creating a subquery in your main dataset query. You will just have to have a value that you want to sort on in descending order.
Say you have a table sorted by the price in ASC order. You want to sum the last 12. You just need to flip the sort in the subquery and sum the top 12.
SELECT OtherFields, (SELECT TOP 12 Sum(S1.MyField) FROM MyTable S1 ORDER BY S1.MyField DESC) AS BottomTwelveSum
FROM MyMainQueryTable
Yes this value will repeat in your dataset, however if you place it correctly in the Table you should be fine, just dont show the value in the details section. This wont add additional rows. Another solution would be using a subreport, however this is much quicker on render time.

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.

Line chart over Column chart in SSRS

I have a column chart with multiple series. (number of series vary from 1-14). X axis is date and Y axis is count.I would like to create a line chart which gives the sum of all series count per date. I tried adding another value of count and changing the chart type to line but the line is drawn with maximum series count per date. Also the label is repeating twice for every series.
Any suggestions on what I am missing?
Here is how I would do it.1. Rollup your Query in SQL - doing this will add a total value for each date as well as a total value for the entire query. Your query would look something like this:
select
YourDate,
YourSeries,
sum(YourCount) YourCount
from YourTable
group by
YourDate,
YourSeries
with rollup
2. Filter out the grand total - the with rollup clause produces a grand total that your chart doesn't need. You can filter this out of the dataset by adding a filter expression =IsNothing(Fields!YourDate.Value), datatype Boolean, value =False3. Set your chart values visibility - set the column values visibility to =IsNothing(Fields!YourSeries.Vale) and the line chart values visibility to =Not(IsNothing(Fields!YourSeries.Vale). Now, your chart and series legend will only show the daily columns and the total line.4. Put the total line chart on a secondary axis This is optional, but since the date totals will be much greater than each series/date value, it will keep the columns from shrinking so small.
It should look something like this:

SSRS Line Chart X-Axis group by Month

everyone. For the life of me I cannot figure out why the X-Axis is pulling 2 dates in each month when I want it to group by each month. In the Values I have:
Value Field =RunningValue(Fields!new_actualsalesfromsplit.Value, Sum, "Chart1_SeriesGroup")
Category Field: =Fields!closedate.Value
Category Groups =Month(Fields!closedate.Value)
Group On =Month(Fields!closedate.Value)
Series Group by ["salesperson']
The chart should have a line for each sales person, and each month should be a cumulative representation of the sales by that person. Thanks for any help.
Set the category field to "=Month(Fields!closedate.Value)", or something similar that will net the results you need. Right now, even though you're grouping by month, you're still telling SSRS to use the atomic data for your X axis, so that's what it's doing.
It may make your task simpler to just add a Calculated Field to your dataset - open the dataset properties window, go to the fields tab, and click "add". Set that field to your month value, use it for grouping and your X axis.