Paginated Reports eBook Introduction, Resource and Sample Data Downloads
Chart reports are an effective way to visualize data to show trends and comparisons in a way that can help the user get real meaning from numbers. However, chart data can be meaningless without context. The charting components in Reporting Services and Paginated Reports are flexible enough to create relatively advanced report visualizations but even simple designs sometimes require creative problem-solving techniques. Admittedly, this technique falls into the “sleazy hacks” category to overcome the challenge that we don’t have a simple, built-in feature in the product to do this sort of thing.
This example shows you how to display a column chart with a goal line so users can easily understand whether charted values are above or below a threshold. The example in this recipe uses a SQL query to demonstrate the essential technique and an enhanced example with a parameterized threshold and dynamically colored columns to show data points above or below the goal.
When I was first challenged to design this report for a consulting client, I knew that it could be done but I just wasn’t sure how. I grew up in Washington State in the 1970s with the legend of Bigfoot. For several years during this period, there were Bigfoot sightings every week and everyone knew someone who claimed to have seen Bigfoot (or had seen someone who knew someone who had claimed to have seen Bigfoot). We were certain he was out there in the remote wilderness but we weren’t sure where. This report challenge was a little like that – the solution was out there and I just needed to find it.
The chart data region lacks a true constant line feature so in order to plot a target, you have to be creative. One option is to use the Stripline property of the vertical axis but that can only be used to shaded bands or lines along a percentage of the vertical axis scale. A second option, and the technique demonstrated in this recipe, is to add a line chart series with a constant value returned by the dataset query. A challenge with this option is that charted values are plotted from the center of each point on the X axis and not from the edges of the chart. This behavior is evident in Figure 1 where the dashed line, representing the target value begins and ends at the center of the first and last category columns.
Figure 1 – Default state of a column and line composite chart
To overcome this limitation, we need to create two additional category values, to be plotted at the beginning and end of the series, with blank titles so the line extends beyond these boundaries. After that, we can use expressions to reduce and control the margin space.
Product Versions (One of the following):
- SQL Server Reporting Services 2008 R2 or newer
- Power BI Report Server
- Paginated Reports in Power BI or Power BI Desktop
What You’ll Need:
- Moderate SQL skills
- Column, area, or line chart
- Experience with VB expressions
Difficulty Level: 200
Contributed by Paul Turley
Designing the Report
This solution is more about technique than complexity. This chart has some simple design features you’d expect to be fairly easy to define, but this is a little challenging to do right out of the box. Creating multi-series reports like this one, containing columns and a line, is standard fare in modern charting products. However, the natural behavior of all charts is that each column or line segment is plotted from the center of the data point along the axis. This means that since the lines and columns in this chart have the same range of category (X-axis) values, the line would begin in the center of the left-most column and end in the center of the right-most column instead of running from edge to edge of the chart container. In fact, no matter what the minimum and maximum values are, the chart normally provides a margin of space on each side of the axis. You’ll see this, and the resolution, as we work through the report design. The final result of this recipe is shown in Figure 2.
Figure 2 – Sales Quantity Column Chart with Target
Write the query
Let’s get started. The first bit of “hack” is in the query. In order for the line to extend beyond the range of column center points, it must begin and end with lower and higher scale values than the respective column points. This means that you need to work some magic in the query and add two extra values to the scale. You will see this in the query that follows.
Create a new report.
Add a data source for the PaginatedReportData database.
Create a dataset and enter the following SQL script:
SELECT ProductCategory as CategoryGroup , Quantity , Target , RANK() over (order by SalesOrderNumber , ProductCategory) as OrderValue FROM ( SELECT NULL as ProductCategory , NULL as Quantity , CAST(@Target as Int) as Target , 0 as SalesOrderNumber UNION SELECT Product.ProductCategory , SUM(Sales.OrderQuantity) AS Quantity , @Target as Target , 1 as SalesOrderNumber FROM Product INNER JOIN Sales ON Product.ProductKey = Sales.ProductKey GROUP BY Product.ProductCategory UNION SELECT NULL as ProductCategory , NULL as Quantity , CAST(@Target as Int) as Target , 2 as SalesOrderNumber ) as S;
Execute the query.
When prompted, enter 12000 for the Target parameter.
The results are shown in Figure 3.
Figure 3 – Query Designer
Notice that two rows have been added to the results on the first and last records, based on the OrderValue sort order. Notice also the lack of values for the CategoryGroup and Quantity columns, but the Target column contains values in these records. This provides the basis for the target line to extend beyond the range of points plotted by the column chart.
Create the chart
Build the chart and define the groups and series data fields.
Add a new chart to the report body.
Choose a standard column chart type.
Drag and drop or select the OrderValue field in the Category axis.
Drag and drop or select the CategoryGroup field as a second field in the category axis.
Click the chart in the designer to show the data field and axis field drop zones.
Add the Quantity and Target fields to the data fields drop zone to define the data series.
Right-click the Target field and change the chart type for this series to a Line chart.
Remove the axis labels.
Cosmetic changes can be applied as appropriate. These may include the line chart weight and color. The report in the designer should look similar to Figure 4.
Figure 4 – Chart in report designer
Preview the report and compare the result to Figure 5. You’ll notice the empty columns for the OrderValue field values 1 and 5. Note how the line chart naturally extends from the center of the first column position to the center of the last. Again, the requirement is for this line to extend from margin to margin.
Figure 5 – Chart report preview
Don’t worry about the axis labels for both of these groups. When you change the axis value range, the labels for the inner group will disappear. Before you do this, you need to show the CategoryGroup field values on the outer group instead of the numbers.
In the Category axis fields, edit the OrderValue group properties.
In the Category Group Properties dialog, set the Label property to use the CategoryGroup field. See Figure 5.
Note: Don’t confuse the Category Group Properties window with the CategoryGroup field. It is only a coincidence that these have similar names,
Figure 5 – Category Group properties
Click the OK button to accept the property change.
Adding Dynamic Color
You can use a dynamic fill color to bring attention to series values that are above or below the goal line. Columns under the goal will be red and columns at or above the goal will be lime green. To enable this feature, follow these steps:
Click once on the chart to show the field drop zones.
Right-click the Quantity field series and open the Series Properties dialog.
On the Fill page, set the Fill style property to Solid.
Next to the Color drop down, click the Expression button (fx) to open the Expression dialog.
Enter the following expression:
=IIF(Fields!Qty.Value < Fields!Target.Value, "Red", "Lime")
Close all dialogs and preview the report. It should look similar to Figure 6.
Figure 6 – Final chart report with dynamic file color
Charts in Reporting Services offer a lot of design flexibility and opportunities to customize the behavior and visual presentation of data. However, with these capabilities come a myriad of properties that can be a bit daunting to navigate and find in the design interface. This report design may not be quite as elusive as Bigfoot, but it does solve a specific problem and provide real business value.
When exploring new features and design patterns, I find it useful to save versions of my chart reports in different stages of development. This way I can revert to a working state if I get myself into trouble. With some practice and perhaps a few late nights of experimentation, you will develop patterns that work best for your users and the data they need to visualize.
Credits and Related References
Column Chart with Goal Threshold Line, Paul Turley, SQL Server BI Blog
SQL Server Central, March 11, 2010 (recipe excerpt from first edition recipe book)