Dynamic SQL SSRS and Dynamic Row Grouping - sql-server

I have a dynamic SQL query that have dynamic column names. ( The resultset is then pushed into a tablix very similar to this setup(https://www.youtube.com/watch?v=BB9wvvkDY7Y ).It works fine. However, I need to establish a grouping on the row in SSRS. Due to the dynamic nature, the fieldname is not available before run time and therefore I cannot set a group on the row. I have tried several workarounds, using parameters like :
=JOIN(Parameters!DynamicGroup.Value, ", ")
(it is a multivalued field), but I cannot seem to get this working. At best, I get 1 row returned.
I may not be clear on this, but what I need is to be able to use the grouping when exporting to excel to have each group create a new sheet in the workbook.
Any help would be appreciated.

Thanks for the replies. I figured out what why I am only getting back a single row. It is because of the way the dynamic query is pivoting the resultset and then grouping it to display all columns(marked with number) as a row. So all fields with the same number becomes a row. When I add another grouping it is taking this qrouping, hence there will only be one row because it is unique(pivoted).
This leads me to think that I cannot get this done this way. Is there a way that I can have dynamic column names (based on Dates)? There will always be the same number of columns returned, but the content will different. I googled this, but I have not found anything that really solves this issue.

Related

Writing array to filtered and grouped table fails

I am having trouble writing an array defined in VBA to a range in a table that has been grouped and filtered.
Writing an array to a table range is done using:
Sheets(1).ListObjects(1).ListRows(1).Range = MyArray
This works fine on a filtered table (i.e. filtering the rows using the filter buttons in the row headers). It also works fine in a grouped table (i.e. with some columns grouped using Data --> Outline --> Group.
However, when combining this, the array is not written to the cells in a correct manner. An array [1,2,3,4,5,6,7,8] is written as [1,2,3,1,2,3,4,5], if I group columns 2 and 3 in the table. This is NOT the case, if I haven't also filtered the table.
Do anyone have an idea what's going on here? An example workbook can be found here. It contains a simple table and one sub that tries to write an array to the table.
Any help would be greatly appreciated!
BR
Torben
OK, so I have figured out a workaround/verification that this is a bug in Excel. If you select a cell outside the table range before inserting the array, it works fine - even when both filtering and grouping is active. Why this is the case, I have no idea. It's a clumsy solution (I'm not fond of selecting cells through VBA), but it works.
(Sorry, I originaly posted it as a comment, and not an answer).
If anyone has a more elegant solution, I would still be very interested in seeing it.

SSRS Error - "Report item expressions can only refer to fields within the current dataset scope or, if inside an aggregate

I'm new to SSRS and I'm not sure if it will do what I would like to do.
I'm getting the following error in SSRS:
"The Value expression for the text box 'Textbox17' refers to the field 'DayCnt'. Report item expressions can only refer to fields withing the current dataset scope or, if inside an aggregate, the specified dataset scope. Letters in the names of fields must use the correct case."
I have a list that's 3 rows and 3 columns. I need to use multiple data sets across the rows and columns. I would like everything on the report to be grouped on the school ID, which each dataset does have.
I will be using multiple datasets per cell in some cases and I'm using a textbox and then dragging the dataset field into it. I'm not sure if grouping is the problem. I'm not sure how to group the entire list at once, or if it is row based, or how the grouping works with a list with multiple columns.
How can I get everything in the list to be based off of the school ID?
You can reference more than one dataset in the same data region (table, etc.) but only if it makes sense to use aggregate functions on all the datasets except the primary one that your grouping is based on. I'm not sure if this makes sense for your use case or not.
An aggregate function is something like First. If you don't specify the dataset, it defaults to the "current dataset." The current dataset is an invisible default that you can't see or set anywhere in the UI, as far as I can tell.
=First(Fields!MyField.Value)
vs.
=First(Fields!MyField.Value, "MyDataset")
Now, there are a couple of tricky things to know about the Report Builder UI.
When you drag a field into your report, the expression it creates does not specify the dataset.
When you drag a field into your report, it changes the current dataset!
This leads to exasperating behavior such as the following:
Create a data region.
Set its grouping to a field from Dataset1.
Drag in a field from Dataset1.
Run the report. It works!
Drag in a field from Dataset2. RB will automatically use an aggregate function, as you would expect.
Run the report again. Now you get an error, not on either of your fields, but on the grouping of your data region.
The problem is that dragging in the field from Dataset2 changed the current dataset to Dataset2 and broke everything that uses Dataset1 without explicitly specifying it. The solution is hacky:
Manually change the expressions of all your fields from Dataset2 so they explicitly reference the dataset, or
Reset the current dataset by dragging in a field from Dataset1 and deleting it.
You cannot use different datasets on only one SSRS table. One table should only refer to one dataset.
And the solution to your question is: recreate your dataset (query), try to get one dataset by using distributed query if they are on different server instance, or specify database name when they are on the same server.
You might have done some modification on the Dataset and you did not refreshed the fields.

Query SQL database from Excel

I'm attempting to create a MS Query to return data from a SQL database based on a value from a cell in Excel. I have actually successful accomplished this, but only for 1 row. I cant figure out how to get it to copy-down to other rows.
I've created a connection as follows:
Notice that the SQL statement includes a parameter. The parameter is set to point to a specific cell:
I guess this makes sense as I'm only looking to return 1 value per row:The problem is that I have multiple lines to return values for. How do I return a value per row for multiple rows?
I've tried changing the cell reference in the Parameters dialog box, but this does not work as the Excel Table is designed to grow dynamically.
Excel data connections works in a way that every connection has only one SQL Query. So in order to do what you'r looking for, you will need to have many connections, and that's not the "best practice".
However, there are two ways you can solve this situation:
1. Make a single connection with all of the data and create a pivot table based on it. Then use VLOOKUP/INDEX to gather the data to your requested cells.
2. If the data is too big, you can use VBA code to create a smaller Query based on the cells you mentioned and then continue as described on the first option.
Good luck.

How to build a column using data from another table with contraints

I have a very-badly done Excel Database that I have to repair. To do that, I need to get specific data from a table and put it in another table, if certain cells in each rows equals cells from rows in the first table.
Here is a screenshot to explain what I need.
I tried with advanced filters but I can't get it to work.
I got an answer.
The trick here is to use a vertical search (=VLOOKUP(cell; tableToSearch; cellToSearchLocation) and IF conditionning.
Sadly, my example isn't valid because the cell we search (in "hey" column) is a in a column before the "ho" and "hi" columns.
If anyone has any questions about this solution, feel free to ask.

Hide row of data in matrix by field value

I need to know how to code this. I am working with program numbers and there is one that I do not want to be displayed. I am trying:
=Fields!NoProg.Value="08007"
But then I do not know how to tell it:
(pseudocode)
if NoProg = 08007, then do not display.
As I see it, you have two options:
Filter out the row you don't want in your resulting dataset at the database side. This has the advantage of using indexes if you have them set up. This involves putting a WHERE clause into your query. If you cannot change the query or need the row to be in the resulting dataset but simply hidden, choose this option.
SELECT ...
FROM ...
WHERE NoProg <> '08007'
Hide the row in the report, as you are already doing. This has the advantage of keeping the data in the resulting dataset but hiding it from that section of the report. If you are performing aggregation and need to include the row elsewhere but just hide the detail from view, choose this option. Keep in mind, this will not use any database indexes, so it requires one comparison for every row in the resulting dataset.
<Hidden>=Fields!NoProg.Value="08007"</Hidden>

Resources