Case- In some
cases we need to filter the data that has been imported from a SQL server
database where it is not advisable to create a new query all the time.
Prerequisite
– You need to have a SQL server database where it should
contain a two column table with "town-id" and ''town-name'' also you need to create a stored procedure to accept town-id as input parameter and return the town-name as out put.
We are going to run a native query in Power BI with changing parameter values so in order to avoid too many confirmations go to "Files" then "Option and Settings" then "Option" then "Security" then untick Require user approval for new database queries in Power BI.
We are going to run a native query in Power BI with changing parameter values so in order to avoid too many confirmations go to "Files" then "Option and Settings" then "Option" then "Security" then untick Require user approval for new database queries in Power BI.
Calling a stored procedure from SQL server when importing data to Power
BI
As you can see above I have chosen “SQL Server” in the “get
data” option from task bar in order to import data from SQL Server database. Then I have given the server
name and the database that I want to connect and chosen to import the data instead of direct query.
Below that you can notice the “Advanced option”, I have entered a SQL statement which have to run before retrieval and will result in a query output.
"EXECUTE pTown 1"
You can notice an “Execute” function in the above SQL statement and you all know stored procedures can be executed in the SQL Server and Power BI is allowing to run a stored procedure and import the query out put.
"EXECUTE pTown 1"
You can notice an “Execute” function in the above SQL statement and you all know stored procedures can be executed in the SQL Server and Power BI is allowing to run a stored procedure and import the query out put.
Once you click ok we can retrieve data from the SQL Server Database
corresponding to the parameter value "1"
When the stored procedure “pTown” is executed with the parameter value "1" the result is as shown below.
In the above image you can notice that the outcome of the
stored procedure is displayed and ready for you to load but now it is time for
you to edit that before loading.
How to create a new parameter in Power BI
Now it is time for you
to create a new parameter. You can do this by selecting manage parameter from
the task bar and there you need to select the new parameter option and you will
get something similar to what you can see above.
Creating a new parameter named TownId in Power BI
As you can see above I have given details for the new parameter such as the name TownId and a little description stating "it should contain values in-between
1-203" also changed the type to text in order to avoid data conversion
problems and provided "1" as the current value. Now click OK to create this parameter.
Using the parameter created in the power BI in the query when calling
the stored procedure
In SQL Server database there is a table for town and it has
203 rows with town id and town name, now we have created a parameter in Power BI which we
can take in put from the user. Now the problem is how we are going to link the stored procedure with
the newly created parameter in power BI instead of the value “1” which has been used for importing.
In order to solve this go to the advanced editor by right
clicking the query and what you will get is something similar as shown below.
Check the above image, can you notice the stored procedure that we have called during the time of import in the very beginning is clearly shown in the advanced editor as a query? What we are going to do is a small change
in that by replacing the value 1 to the parameter TownId that we have created in Power BI. If you
don’t understand check the image below.
Can you notice the difference I have made by removing “1” and
adding the parameter "TownId'' that I have created?
Now click ok to apply the
changes. You might wonder why nothing has happened but the truth is your
parameter is influencing the stored procedure in the query, in order to check
this give different values to the parameter and see the outcome of the query.
Some examples are given below.
Some examples are given below.
When 15 is given as parameter value.
When 105 is given as parameter value.
Can you notice the value that we have given for the parameter TownId is their within the bracket also for each different parameter corresponding different towns are being displayed in the query.
What is
happening – In a holistic view what we are doing is simply calling a
stored procedure to import data from SQL Server database. The best part is the
parameter for that stored procedure can be given from Power BI using a new parameter, in other words you are filtering what you want with style.
Thanks for sharing this Informative content. Well explained. Got to learn new things from your Blog onPower BI Online Training Bangalore
ReplyDelete