2

I want to build a SSRS report that has column as week numbers - 8 weeks for 8 columns starting with current. This report is run every week and current week number is set then. So both column names and their values should change .Is it possible to build something like this in SSRS?

I tried doing this with a dynamic SQL based stored proc in dataset. However for every run I don't even see the columns values updating dynamically

Here's an example :

enter image description here

Also I am trying to avoid these week numbers as row values and then using matrices

My stored proc looks something like this

declare @n tinyint = datepart(wk, getdate())

declare @n1 tinyint = (@n+1), @n2 tinyint =(@n+2), @n3 tinyint =(@n+3), @n4 tinyint =(@n+4), @n5 tinyint =(@n+5), @n6 tinyint =(@n+6)

exec ('Select b.sku, b.['+@n+'], b.['+@n1+'], b.['+@n2+'], b.['+@n3+'], b.['+@n4+'], b.['+@n5+']...

Will appreciate any help in this direction.. many thanks!

PC-Stats
  • 95
  • 2
  • 9

3 Answers3

0

When working with SSRS it's generally best to avoid dynamic SQL and pivoting the data in the SQL. Use the SQL to get the raw data you need and then let SSRS do the pivoting and aggregation. This way you take advantage of what they each do best. I know you said you want to avoid matrices, but it is the best way to make the report dynamic.

So you should either return all the data in one dataset and use filters on your matrices OR write two queries and have each one populate a matrix. BTW a matrix is just a table with a column group added, so don't be intimidated by them.

StevenWhite
  • 5,907
  • 3
  • 21
  • 46
  • Thanks for your response. The reason I am trying to avoid matrices or table group is that within the report I am using individual week values ( eg Field!Wk48.value) to calculate other column values.If I group them(having week numbers as group item) will I be able to access individual week data? – PC-Stats Nov 21 '16 at 23:40
  • @PC-Stats Yes, see the `Lookup` function. https://msdn.microsoft.com/en-us/library/ee210531.aspx – StevenWhite Nov 22 '16 at 16:03
  • Lookup function would need a field name. Here field name is grouped. More appropriate function may be first , last and previous.. However from my experience, these make report very slow and am trying to keep performance as much as possible unchanged... – PC-Stats Nov 27 '16 at 03:10
0

There are 2 ways to do this with a standard tablix.

  1. Calculate the column headers as expressions using concatenation of Wk and some date math to find the correct week number and return the same sort of thing from your query (e.g. columns are current_week, week_minus_1, week_minus_2...)

  2. Return the column headers as additional columns in your query that are the same value for every row (e.g. ColHeader0, ColHeader1...). Your data columns would still be relative weeks (e.g. ValueWeek0, ValueWeek1...). In your report the column header would have an expression like =First(Fields!ColHeader0.Value). This is a more flexible approach since it lets you pick 8 historical weeks instead of only the last 8 weeks if you add a parameter.

EDIT - Clarifications

The reason that you get the blank column Wk48 is approximately that you have created your report looking for that column that won't be there the next time. SSRS looks for exact columns. You should you use relative column names for either of the options I have specified:

exec ('Select b.sku, b.['+@n+'] as Wk0, b.['+@n1+'] as Wk1, b.['+@n2+'] as Wk2, b.['+@n3+'] as Wk3, b.['+@n4+'] as Wk4, b.['+@n5+'] as Wk5...

This will allow you to populate the aliased Wk0 column with the appropriate current week data and still make sure that it can be consistently referenced as the base week by SSRS.

To change the column headers you can:

  1. Independently calculate the week numbers in SSRS in the column header expressions: ="Wk" + CStr(<correct week calculation>).

  2. Return the column headers in the result set and access them in the column header expression:

    exec ('Select b.sku, b.['+@n+'] as Wk0, b.['+@n1+'] as Wk1, b.['+@n2+'] as Wk2, b.['+@n3+'] as Wk3, b.['+@n4+'] as Wk4, b.['+@n5+'] as Wk5..., ''Wk'''+@n+' as ColHeader0, ''Wk'''+@n1+' as ColHeader1...

and reference the returned column headers from the SSRS column header expression as =First(Fields!ColHeader0.Value).

SMM
  • 2,225
  • 1
  • 19
  • 30
  • I have added clarifications for you. – SMM Nov 22 '16 at 16:30
  • Also, you could probably do it this way (relative weeks instead of specific weeks) without using dynamic SQL. – SMM Nov 22 '16 at 16:45
  • Thank you again. (1) may work with a parameter based field name containing current week and subsequent week. A very interesting article is : [link] (https://blogs.msdn.microsoft.com/sqlforum/2011/05/03/walkthrough-control-dataset-query-dynamically-based-on-ssrs-report-parameters/?replytocom=2935#respond) But I am struggling to access individual columns produced by this expression based query. It shows No Fields when I try the usual way to create a table – PC-Stats Nov 27 '16 at 03:18
  • Notice in the article that while they are using a dynamic query, the column names stay constant. Can you post the query/proc you have come up with? – SMM Nov 27 '16 at 04:21
0

Here's a solution that worked for me:

  1. Create parameters (say CurrWk, CurrWk1) ,set as hidden and store 'Default value' and 'Available value' equals to current week number (datepart(wk, now()) and any subsequent week by doing a +1, +2, +3.. etc.

  2. Write a query expression . Click onto fx beside dataset query space and write the select query for your program embedding parameter values in the expression window. For eg ="Select SKU, [" & Parameter!CurrWk.Value & "] as Wk1, [" & Parameter!CurrWk.Value & "] as Wk1 from Sales_Table" Before passing this query as a 'command text expression' please ensure this query is working in sql ssms.

  3. Save the expression. Now find 'Fields' tab on the left hand side panel.You need to map the fields manually from the query here. If this is not done, there is a very high chance you seean empty field list and wont be able to access them at all. This may be because ssrs do not store query metadata directly from expressions.

You can avoid part of the issue by having atleast the static fields , for example here SKU listed in the 'Fields' list by first running a sql query with static field(select SKU from Sales_Table ). You can then go back to update dataset- change query to expression and embed the parameterized field names.

  1. Map field names. In this example I chose 'Query Type' fields and set Field names as SKU, CurrentWeek, NextWeek and mapped to source SKU, Wk and Wk1 respectively.

Click on 'Refresh Fields' at the bottom. Now you have a dataset with the complete field list. Use these in charts, tables . Run it every week and note the numbers changing as expected.

In case you are using this dataset in a table, make sure you set headers with Labels of Parameters (for eg here I did =Parameters!CurrWk.Label for col with current week data)

That's it!

PC-Stats
  • 95
  • 2
  • 9