SSRS how to stop repeating year on a bar chart - reporting-services

I was wondering if a dataset has a year and month for a whole year dataset. How can you have it so the year part isnt showing up for every month on the chart. so say if the report is ran for 01/04/2015 to 31/03/2016, the year part appears twice but not repeating 12 times for each month.
Just another quick question, in my chart I have formated the date i am using which the values looks like the following '2015-09-02 00:00:00.000' so in the chart category I have the date formated to show as (format(date,"MM/yy"))
this is used in the group by as i need the data for monthly.
However by doing this it takes ages for the chart to load, is there any better way to do this for monthly, the result set is about 90,000 rows

If you have a dataset query like
SELECT RIGHT(CAST(MONTH(soh.OrderDate) + 100 AS char(3)), 2) + '/' + RIGHT(CAST(YEAR(soh.OrderDate) AS char(4)), 2) AS MMYY, YEAR(soh.OrderDate) AS YYYY, MONTH(soh.OrderDate) AS MM, SUM(sd.OrderQty)
AS Qty
FROM Sales.SalesOrderHeader AS soh INNER JOIN
Sales.SalesOrderDetail AS sd ON sd.SalesOrderID = soh.SalesOrderID
WHERE (soh.OrderDate BETWEEN #StartDate AND #EndDate)
GROUP BY YEAR(soh.OrderDate), MONTH(soh.OrderDate)
For the first question set pick yyyy ,mm as category group
For the second question pick mmyy as category group then right click on category group properies, click sorting and add yyyy and mm in the change sorting options dialog.
SalesOrderDetils in Adventurworks2012 contains 121,000 records and the charts are returned preety quickly on my low spec pc.

Related

Getting average value based on grouped data

I'm trying to find the average of net total for a given month, based on previous years to help show things like seasonal trends in sales.
I have a table called "Invoice" which looks similar to the below (slimmed down for the purpose of this post):
ID - int
IssueDate - DATE
NetTotal - Decimal
Status - Enum
The data I'm trying to get, for example would be similar to this:
(sum of invoices in June 2018 + sum of invoices in June 2019 + sum of invoices in June 2020) divided by number of years covered (3) = Overall average for June
But, doing this for the full 12 months of the year based on all the data (not just 2018 through to 2020).
I'm a bit stumped on how to pull this data. I've tried subqueries and even tried using a SUM within an AVG select, but the query either fails or returns incorrect data.
An example of what I've tried:
SELECT MONTHNAME(`Invoice`.`IssueDate`) AS `CalendarMonth`, AVG(`subtotal`)
FROM (SELECT SUM(`Invoice`.`NetTotal`) AS `subtotal`
FROM `Invoice`
GROUP BY EXTRACT(YEAR_MONTH FROM `Invoice`.`IssueDate`)) AS `sub`, `Invoice`
GROUP BY MONTH(`Invoice`.`IssueDate`)
which returns:
I see two parts to this query, but unsure how to structure it:
A sum and count of all data based on the month
An average based on the number of years
I'm not sure where to go from here and would appreciate any pointers.
Ideally, I'd want to get the totals from rows where "Status" = "Paid", but trying to crack the first part first. Walk before running as they say!
Any guidance greatly appreciated!
Basically you want two levels of aggregation:
SELECT mm, AVG(month_total)
FROM (SELECT YEAR(i.IssueDate) as yyyy, MONTH(i.issueDate) as mm,
SUM(i.`NetTotal`) as month_total
FROM Invoice i
GROUP BY yyyy, mm
) ym
GROUP BY mm;
Just for the Average Amount Part You Could use a query like
Select Date From Your_Table Where Date Like '20__-06-%'
You can arrange it into asc desc order.

SSRS - How do I format SQL data to generate line chart of time series?

I have a table that is set up like
SELECT [EntryDate] --Date
,[StoreId] --Nvarchar
,[PassFailElement] --Int, 1 or 0
And the SSRS report is set up for the user to input #StartDate and #EndDate to bookend the [EntryDate]s they want to see.
Is there a way to create a line graph that shows the values for [PassFailElement] from #StartDate to #EndDate as the first series, DateAdd(DateInterval.Year,-1,#StartDate) to DateAdd(DateInterval.Year,-1,#EndDate) for the second series, and then two years back for the third series?
I'm sure there are a million more elegant ways to do this but here is how I might approach it...
The following is based on the Microsoft supplied NorthWind database so you can recreate it if you really want to...
I've set the actual start and end date values in here but you can comment out the first few lines and then your SSRS parameters will be applied.
So to start with a took the orders table as it had some dates in and joined to some basic order data just so I could see the results looked OK, most of the columns are not used.
My dataset looks like this...
SET DATEFORMAT YMD
DECLARE #startDate date = '1998/04/01'
DECLARE #endDate date = '1998/07/30'
SELECT
e.EmployeeID, e.FirstName, e.LastName, e.Title
, o.OrderID, o.OrderDate
, c.CustomerID, c.CompanyName
, CASE
WHEN (OrderDate between #startDate and #endDate ) THEN 'This Year'
WHEN (OrderDate between dateadd(YYYY,-1,#startDate) and dateadd(YYYY,-1,#endDate )) THEN 'Last Year'
WHEN (OrderDate between dateadd(YYYY,-2,#startDate) and dateadd(YYYY,-2,#endDate )) THEN '2 Years ago'
END as YearGroup
, MONTH(OrderDate) AS OrderMonth
, Day(OrderDate) AS OrderDay
FROM Employees e
join Orders o on e.EmployeeID = o.EmployeeID
join Customers c on o.CustomerID = c.CustomerID
WHERE
(OrderDate between #startDate and #endDate ) OR
(OrderDate between dateadd(YYYY,-1,#startDate) and dateadd(YYYY,-1,#endDate )) OR
(OrderDate between dateadd(YYYY,-2,#startDate) and dateadd(YYYY,-2,#endDate ))
The Case statement in the SELECT clause checks to see if the dates fall into one of three groups, either
between the start and end dates supplied
between the same date range but minus a year
between the same date range but minus two years
The computed OrderMonth and OrderDay are there as I assume you will want to 'stack' the lines so say, 1 June, across all three groups is in the same horizontal position on the chart. ** See notes later for changing this.
The WHERE clause does similar tests to make sure we only return data from the ranges we need.
All I did them was simply add a line chart and set
the Series Groups to the [YearGroup] field
the [OrderMonth] and [OrderDay] to the CategoryGroup
and (for no other reason than I didn't have much else to display) I used the sum of OrderID as the values.
** if you want to represent the time range as one continuous time, then remove the OrderMonth and OrderDay from the category groups and replace with OrderDate
The resulting chart looked awful but that was just down to the data.
I would create a couple of Calculated Fields in your dataset to break up the data and add them to the chart.
The first would be a field that determines which year the data is in - Current, Previous or the Prior. Maybe just 0, 1 or 2? This field would be used as your chart series. Call it ENTRY_YEAR.
The second would be a date field with the years all set to the current year. You could just add the integer from the first field. This will normalize all your data to a single year timeline. You won't actually use the year - that's just to separate the data at the year break.
=DATEADD("y", ENTRY_YEAR, Fields!EntryDate.Value)

SSRS - match dataset values with hard-coded values

I am trying to create an RDL file and I need a tablix to appear in the following format.
This is how I want the results to look
The values that are in bold are hard coded values. This is how the output from the SELECT statement in the datasets looks
SQL Output
I don't know how to make the values that output from the database match with the hard coded values in the RDL file. The 'Day' field represents a day in the month and the 'Num' field represents the number of sales that were on the day. The above example shows that on the first day of the month, there were 100 sales made. I need the tablix to output in that specific format.
If the day isn't in the SQL output (no sales made that day), I want it to output blank and/or 0.
Any idea how this could be accomplished?
Use a CTE to create rows for each day you need and then join your results on. A starting point for you CTE could be:
;WITH nums AS
(SELECT 1 AS value
UNION ALL
SELECT value + 1 AS value
FROM nums
WHERE nums.value <= 30)
SELECT *
FROM nums
You'll probably then want to modify the total days based on the month you are viewing.
You can do this using lookups, but you would need to hard code a lookup in each cell. e.g. for day 1
=lookup(cint(1),Fields!Day.Value,Fields!Num.Value,"Dataset1")
A faster way would be to create a tablix on the dataset filtered on the first ten days:
=Switch(
Fields!DAY.Value <= 10 and Fields!DAY.Value >=1,"Include",
True,"Exclude"
)
Create a row group on days, then create a column with day and num, and columns with Fields!DAY.Value+10 and Fields!DAY.Value+20 with the following lookups:
=lookup(Fields!DAY.Value+10,Fields!DAY.Value,Fields!NUM.Value,"DataSet1")
=lookup(Fields!DAY.Value+20,Fields!DAY.Value,Fields!NUM.Value,"DataSet1")

Group By or Case Logic Issue (Similar to SUMIFS in Excel)

I have a temp table and I'm trying to sum data but can't seem to get the logic right for it. The table contains customer level data and now I'm trying to aggregate it by fiscal year, quarter, and product description. I'm trying to sum by going back 1 year and using the same quarter to sum the # of units sold.
I can do this in excel, but the table is too large for that. This is what the formula in Excel looks like:
=SUMIFS(Units,FiscalYearQuarter >= Concat(FiscalYear -1 & FiscalQuarter, FiscalYearQuarter <= Concat(FiscalYear, FiscalQuarter)
Here's an example of the table:
Here's what the results should looks like (This does not include productdescription, but I will want to add that in):
Every time I try to group by or do a Sum(Case When...) I keep getting the results only by the fiscal year/quarter instead of the sum of historical for 1 year.
A simple GROUP BY will work (although I don't quite understand your Excel logic with concatenation):
SELECT t1.FiscalYear, t1.FiscalQuater, sum(t2.UnitsPurchased)
FROM `table` t1
LEFT JOIN `table` t2
ON ( t1.FiscalYear = t2.FiscalYear + 1
AND t1.FiscalQuater < t2.FiscalQuater)
OR ( t1.FiscalYear = t2.FiscalYear
AND t1.FiscalQuater >= t2.FiscalQuater)
GROUP BY t1.FiscalYear, t1.FiscalQuater
EDIT 1
modified query based on author's feedback

Finding difference at group level

I have 3 months of AR data. From SQL I am populating details records for 3 months. In SSRS I am grouping the totals by month and showing only group summary. I want to calculate the variance by difference between current month and previous month. Please see the attached output file.
Could anyone help me on finding the variance at group level. How do I calculate the difference by using previous function?
Is there any way to find individual group sum of the particular field like this one
Sum(Fields!Current.Value, "DataSet1")
Something like this perhaps?
=Sum(iif(month(Fields!myDate.Value) = month(Max(Fields!myDate.Value)), Fields!myVal.Value, 0))
-Sum(iif(month(Fields!myDate.Value) = month(dateadd("M", -1, max(Fields!myDate.Value))), Fields!myVal.Value, 0))
Where myDate is the date used to group the months on, and myVal is the specific data for the column
UPDATE
Using this dataset
EOM myValue
09/30/2015 2
10/31/2015 6
11/30/2015 19
Gives this basic report
You can then use this formula (based on the original one in the original answer above) to determine the difference between the last and the penultimate row
=Sum(
iif(month(CDate(Fields!EOM.Value)) = month(Max(Fields!EOM.Value)),
Fields!myValue.Value,
0)
)
-Sum(iif(month(CDate(Fields!EOM.Value)) = month(dateadd("M", -1, max(CDate(Fields!EOM.Value)))),
Fields!myValue.Value,
0)
)
Then place this in the footer of the tablix as shown
This shows the difference between the two rows
Is this the behaviour you require? If not please clarify further in the original question as an update/edit.