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

Don Ford

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?

Alan Schofield

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

  1. between the start and end dates supplied
  2. between the same date range but minus a year
  3. 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SSRS Chart Group Series - How do I get the secondary series group to start at the first data point?

sql how do i convert a time series data to a hoc

How do I prevent Power BI line chart drawing a line through time series points when there is no value for some intervals?

How do I format date and time on ssrs report?

How do I convert HH:MM time format to decimals in SSRS?

How do I update the legend labels in a time series chart?

How do I convert time series data from wide to long format using python (pandas package)?

how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?

How do I make a chart appear in SSRS?

How to show first data in a series in a line chart?

SQL: How can I generate a time series from timestamp data and calculate cumulative sums across different event types?

How to chart aggregated time series data with matplotlib

How do I sort Labels on SSRS Line chart's value (Y) axis?

how to generate time series data in R

How do I change data structure for use with google line chart

Chart with time series using Rickshaw. format of input data

Non Scaled SSRS Line Chart with mulitple series

How Do I generate Line Graph in PowerBI for the following Data

How do I plot time series data by week and year?

How do I create a Linear regression model for a time series data?

How do I get this JSON time series data into a pandas dataframe?

How do I remove weekends and holidays from time series data

getting unusual line chart on weekly time series data in matplotlib

Time series line chart is not displayed

R Time series line chart

How to create an time series in R for the .data format?

How do I change format vAxis for specific targetAxisIndex series in React Google Chart

How do I disable the secondary line that appears after clicking on a series in the legend of a Google chart?

How do I set the max and min of the x-axis of Vega-Lite time series chart?

TOP Ranking

HotTag

Archive