Link filters to queries on Superset - jinja2

I have created a visualisation in Apache Superset based on a Saved Query. How can I update the query based on the values filtered within a Filter Box?
I have experimented with Jinja and managed to pass hardcoded variables to my query through the template parameters. Now I just need to connect Jinja to the Filter Box such that the values are obtained through the filter rather than hard coded.

I discovered that this is possible using the filter_values function that is added to the Jinja context via this file: https://github.com/apache/superset/blob/master/superset/jinja_context.py
The example in that file shows how one can build a templated query that pulls values from a filter box:
SELECT action, count(*) as times
FROM logs
WHERE action in ( {{ "'" + "','".join(filter_values('action_type')) + "'" }} )
GROUP BY action
So if you have a filter box for selecting values for action_type, those values will be returned by filter_values.

The column name used in the filter should be the same in another table also. Have you tried it? If column names are different then create a materialized view with the changed column name or rename the column in the table itself

Related

SQL Select Statement Column Selection Based on Checkbox TickMark Access

I would like to select specific columns based on the user checkbox selection.
I can able to achieve it using VBA but is it possible to arrive the fields in SQL itself based on forms checkbox tick status?
Two ideas:
1) create a text box that collects the values of the checkboxes:
= if(checkbox1,"Col1, ","") & if(checkbox2,"Col2, ","") & ...
Create a second one that removes the tailing comma.
Use the content of this second text box to build your SQL string.
2) Solution 1 requires you to hard code the columns in one formula. A more generic way would be to populate a list with the column names of your data source (the table). The changed event of the list would then generate the list of column names for your SQL string.
This solution involves VBA, yes but it's interactive. Guess that's what you're after.

SSRS Comma-delimited list as Parameter value

I have a report in SSRS. My parameter allows multiple values. My query has in the WHERE statement:
WHERE AllDiag IN (#Diag)
My user should be allowed to enter something like Z34.83,R30.0,0000.
These are 3 different codes to search for, so technically it is looking for:
WHERE AllDiag IN ('Z34.83','R30.0','0000')
I've tried all kinds of things like making the parameter properties in the query properties an expression using =join(Parameters!Diag.Value,"','"), and even entering the list of codes with the quotes already, but nothing seems to allow this to work.
I even tried some split function to see if it searched for each separately but I'm not sure I even use it right since there seems there might be some function that should run before.
I'm out of ideas. Any help is greatly appreciated!
I'm assuming your dataset uses the WHERE clause as you stated
WHERE AllDiag IN (#Diag)
I'm also assuming that you cannot easily produce a list of available parameter values to choose from.
So to create a parameter that allows the user to manually enter a list of values simply set 'Allow multiple values' on #Diag parameter. The user then simply types each value and presses enter after each one.
Note there is no need for comma's just type them one by one pressing enter after each.
When SSRS sees a multi-value parameter being passed to a SQL statement using an IN clause, it converts this to dynamic SQL automatically including adding the comma's. If you trace the report using the SQL Profiler, you can see the SQL that is generated.
I thought I would share this with you all in case you may have had issues (like I did) with passing multiple values in a single parameter from your web page to a SSRS report.
NOTE: This is different from passing multiple parameters, each with its own value into a report. The later, there are plenty of examples on the web.
This is very usefull when you need to basically pass into your report's SQL command a list of values for your report's SQL command to use using a "special" function, and where you do not know the number of times the values may be required, as the user can choose anything from one value to 'n' values (but we will hit a limit, as I'll explain later). It's also useful for generating Excel row-by-row extracts from your website - say for Pivot table handling or charting later on.
Unfortunately using IN() on its own tricks a lot of people and they cannot figure out why it does not work. That's because if you define your report in SSRS to expect a parameter straight into the IN() function, the system literally places the value as a parameter in the function and tries to compare what is essentialy a parameter "data type" with your column's data type and you will get errors.
If your report has SQL similar to this ...
SELECT t.Col1, t.Col2, etc
FROM myTable t
WHERE t.myColumn IN (#myListOfValues)
where #myListOfValues is something like "'value1','value2','value3',..." it "may" work but I found passing such a string from ASP.net into SSRS did not work and there are technical issues with string handling from the ASP.net side plus a limit depending your system and browser.
To get around possible issues, create a function in your SQL Server database to receive a string of values delimited by a comma and allow the function to turn your list into a table. That way the table can easily be linked using SQL and passed as a sort of "parameter feeder" into your report's SQL or dataset.
So without babbling on too much lets start with code and an example:
Firstly lets create a special utility function that converts a list of values into a table, and by the way this function can be used within your projects to do exactly that - split strings delimited by something into a table for anything else.
Open SQL Server and create a new function using your normal right-click NEW function command. Something like this ...
CREATE FUNCTION [dbo].[fnMakeTableFromList](#List VARCHAR(MAX),#Delimiter VARCHAR(255))
RETURNS TABLE
AS
RETURN (SELECT Item = CONVERT(VARCHAR, Item)
FROM (SELECT Item = x.i.value('(./text())[1]', 'varchar(max)') FROM (SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(#List, #Delimiter,'</i><i>')+ '</i>').query('.')) AS a
CROSS APPLY [XML].nodes('i') AS x(i)) AS y
WHERE Item IS NOT NULL);
NOTE: the delimiter does not have to be a single character! Again useful for delimiting using keywords, etc.
Note the XML logic and conversion in the function? That is because ASP.Net is going to literally pass some HTML into SQL Server and we're going to use it to strip off the data we need into a table.
Run the function with some values to test:
SELECT * FROM dbo.fnMakeTableFromList ('a,b,c,d', ',');
You should see 4 rows of data returned ...
a
b
c
d
That is the results in a table.
Now use this function in your SQL Reporting Services report:
Here is my report as an example:
SELECT DISTINCT s.StudentID
FROM tblStudents s
LEFT OUTER JOIN dbo.fnMakeTableFromList(#StudentList,',') AS list ON list.Item = s.StudentID
WHERE (#StudentList IS NULL
OR #StudentList='')
AND (l.Item IS NULL
OR l.Item = s.StudentID)
Note my example also caters for reporting every student ID if there was no value passed at all. So report every student found in tblStudents or report those based on the list of student IDs given, delimited by a comma. When you run this directly in SSRS, you'll be asked for a parameter #StudentList. In there type what ever values you need separated by a comma, and you should only get those student IDs. If not, make sure the report works "stand alone" first before going over to the ASP side.
Once you are happy your report works, and the function in SQL Server works, then we are ready to code the ASP.net side of things:
In your ASPX code behind page (C#) we need to control what the list is and how to pass it over to SSRS. Because we are dealing with a LIST<> here, I am only going to illustrate the way to do using a LIST<> to mimic an array. As you know C# does not have array terminology like you have with VB.
So in your ASP.net page paste this code in your PageLoad event ...
protected void Page_Load(object sender, EventArgs e)
{
//get parameters from the URL list
string strStudentList = Request.QueryString["StudentList"];
//create a List of parameters and pass it over to the report
List<ReportParameter> paramList = new List<ReportParameter>();
paramList.Add(new ReportParameter("StudentList", strStudentList));
//run the report
ReportViewer1.ServerReport.SetParameters(paramList);
}
Of course some objects in here have to be defined in your ASPx page.
For example I use a master page and as you can see, I did all of this to create a mailing list for printing on special sticky label paper.
<%# Page Language="C#" MasterPageFile="~/rptsStudentAdministration/StudentAdminReports.master" AutoEventWireup="true" CodeFile="rptStudentLabels.aspx.cs" Inherits="rptsStudentAdministration_rptStudentLabels" Title="Student Mailing Labels" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<rsweb:reportviewer id="ReportViewer1" runat="server" font-names="Verdana" font-size="8pt"
height="800px" processingmode="Remote" width="900px">
<ServerReport ReportServerUrl="<%$ AppSettings:ReportServerURL %>" ReportPath="/rptsStudentAdministration/rptStudentLabels"></ServerReport>
</rsweb:reportviewer>
</asp:Content>
Make sure you are using these as well in your .cs file:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
And that's it folks!
CONCLUSION:
If you need to generate a report in SQL Server Reporting services that relies on users selecting none, one or many values to control the logic in the report, then think of passing them all as a single parameter and using a function to turn your values into a table for ease of SQL management. Once you have the SQL working, you should be able to generate the report easily in design mode and using the above ASPx logic, be able to pass all the values delimited by a comma into into your report. An added bonus is to HIDE the parameter in SSRS and that way the user does not have to see what values they chose, and you control the entire report being generated programmatically.
There are a lot of answers here which don't really point to the solution.
Problem: the #Parameter value is passed to SQL as a comma delimited NVARCHAR value, NOT a list of data values you can JOIN to or use WHERE clauses with. When passed to SQL Server via a procedure call the data type of your parameters is also lost.
Solution in SQL Server 2016+ is to use the build in "split_string" function which returns a list of values from a single delimited field.
DECLARE #Parameter nvarchar(max);
Select *
FROM [dbo].[MyTable] a
JOIN (Select [value] FROM string_split(#Parameter)) b
ON a.ID=b.Value
It may be necessary to CAST your value field depending on the data type you are expecting SSRS to pass through. For example DATE values may look like this:
DECLARE #Parameter nvarchar(max);
Select *
FROM [dbo].[MyTable] a
JOIN (Select CAST([value] as DATE) as [value] FROM string_split(#Parameter)) b
ON a.SomeDateValue=b.Value
In Sql Server 2014 or lower, you can use custom functions to separate delimited list into table rows. Many examples exists on MSDN and StackOverflow, here's very detailed blog post detailing the pros and cons of many methods.
Either method would work with Command Text and Stored Procedure data sets.
I second the answer from Canadean_AS that you should setup a multi-select parameter.
However, if for some reason you have a hard requirement to accept a single comma delimited string into #Diag, you can try the following in your query:
WHERE CHARINDEX(','+AllDiag+',' , ','+#Diag+',') > 0
Be aware that you may encounter performance issues if your where clause is filtering a large dataset with this function.
A more efficient option is to parse #Diag into a table of its own and join that table to the dataset in the FROM clause.

SSRS Data from one dataset not in another

I've been searching for a possible answer to my problem but was unable to find one. Lets assume that I have 2 datasets. dsMediaServerData has two columns: id and channel. I have other dataset (dsCatalogData) with a lot of columns including id and channel. I want to filter dsCatalogData to show only those records that are not in dsMediaServerData.
My recommendation would be to update the SQL queries used to populate the datasets so that duplicate id/channels are excluded.
If you can't do this directly through SQL because the data sets are coming from different DB's, you can accomplish this with the use of a parameter.
1) Let's start with the first dataset (dsMediaServerData in your example). Since your example shows filtering by 2 columns, but the data will be travelling through a single parameter, you will need to create a new column that combines both pieces of data.
SELECT Convert(VARCHAR(255), id) + '_' + Convert(VARCHAR(255), channel) as 'combined_columns_to_filter'
2) Now create a new parameter (let's call it #filter).
Set the data type to text
Check "Allow multiple values".
Open up "Default Values" and choose "Get values from query". Choose the dsMediaServerData data set and select the new column 'combined_columns_to_filter' as the value field.
Set the visibility to "Hidden" so that users don't try to interact with this parameter.
3) Now update the second dataset. Add the #filter parameter to the WHERE clause.
WHERE Convert(VARCHAR(255), id) + '_' + Convert(VARCHAR(255), channel) NOT IN (#filter)
This should effectively filter the second dataset by removing all records found in the first data set even though the data sets are in separate databases.

Passing multiple values using URL to a drop down parameter in SSRS

I am trying to pass multiple values for a parameter in ssrs which is delimited by ','.Limiting the result data set using where condition using split function in stored proc, this gives me results of my report data set
WHERE YEAR(a.month_start_date)IN (SELECT Value FROM dbo.FnSplit(#year,','))
--AND datename(month,month_start_date) IN (SELECT Value FROM dbo.FnSplit(#month,','))
AND b1.branch_cd IN (SELECT Value FROM dbo.FnSplit(#branch,','))
I created a data set to get available values for year filter
Configured the parameter to get available values from my filter data set and also checked option to "Allow multiple values"
Select distinct year(month_start_date) as Year
From [DB].[dbo].[table]
Then I also limited my report data set to accept parameters with following condition.I configured parameter to accept the following value
=Join(Parameters!year.Value,",")
I pass in values in url
http://<servername>/ReportServer/Pages/ReportViewer.aspx?<reportname>rs:Command=Render&year=2012,2013,2014
My filter does not select the values passed thru the url. The report only shows me the list of values in drop down but does not select the values parsed thru url
I am not sure if I am missing anything else. Please suggest.
Thanks!
The issue here is that your URL is constructed incorrectly. You are trying to pass througth the years as a single parameter and that isn't how it works. Pass it through as a whole heap of parameters and then let the reporting server put it together and put it into the SQL.
Your URL should look like this (I changed the : to %3a and broke up the year parameter)
http://<servername>/ReportServer/Pages/ReportViewer.aspx?<reportname>rs%3aCommand=Render&year=2012&year=2013&year=2014
I hope this helps someone out.

How to pass column name from variable in Conditional Split condition

I created SSIS package. I have a Data Flow Task in here, where I have OLE DB DataSource, which loads records from some table from database. Table name is assigned programmatically, so different columns may be output of that DataSource. Also I have Conditional Split connected to DataSource output, where I want to split records. I want to set condition in Conditional Split and I want to do something like that:
#[User::ConditionColumnName] >= #[User::SomeValue]
where #[User::SomeValue] is variable with some value to compare, but #[User::ConditionColumnName] is variable with name of some column from DataSource output. This value I will assign programmatically.
How can I do that? Or may be is there some other way to split data with unknown at compile time columns?
This sounds like a row-based conditional split. Perhaps you could add the variable value into your select list (so that you have a column you can compare on) something like
"SELECT '" + (DT_STR,50,1252)#[User::ConditionColumnName] + "' as MyConditionColumnName, .... FROM ... "
That way you have the column MyConditionColumnName per row that you can compare on in your Conditional Split.
(You could put this select into a variable and run the sql from variable, maybe easier to maintain) - either way you need to parse the query as an expression in order to evaluate your variable before it's run.