Working With Power Queries Using Blue Prism
Working With Power Queries Using Blue Prism
demonstrate the usage and implementation of a power query template. First, we will go
through the input file which in my case has 1.7 lakh records and 11 columns:
Now, I have also created a sample Power Query Template file with a sheet called as
'Parameters' where I need to input the parameters for my power query from Blue Prism in
order to invoke it. You can create a similar template file for your use case as well:
One thing to note here is the cell address of all the parameter value contents, they are not like
a normal Cell Address such as A1, A2 but I have made them as Named Ranges like you can
see below. This we need to do that as power query only accepts dynamic parameters if they
are in the form of named ranges:
Now you need to first launch the power query editor from your Data tab as shown below:
Now select the excel file from where you need to read the data first by following the below
steps(For now we are hard-coding the file paths and sheet names, going ahead we will see
how I will making query dynamic with respect to the values entered in parameters sheet):
Import the sample input file into the power query by clicking on 'Import' button in the above
screenshot. Now select the input sheet from where you want to load the data, which in my
case is 'Sheet1' of the Sample.xlsx file and click on 'OK':
Once, the data has been loaded as shown below, you can click on the 'Advanced Editor'
button to access the power query steps that are being performed in backend:
Now, here you can see that all the file paths and sheet names are hard coded. So we will be
creating a dynamic logic to get the values from parameters sheet and pass them to our power
query steps:
Original Code:
We will paste the below M-Query (language used writing for power query statements) code
to make the parameters dynamic and keep the columns that we are interested in and will click
on 'Done' to save the changes:
let
FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
SheetName = Excel.CurrentWorkbook(){[Name="SheetName"]}[Content]{0}
[Column1],
ColumnsToKeep = Excel.CurrentWorkbook(){[Name="ColumnsToKeep"]}[Content]{0}
[Column1],
Source = Excel.Workbook(File.Contents(FilePath), null, true),
Sheet1_Sheet = Source{[Item=SheetName,Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet,
[PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"NCT
Number", type text}, {"Title", type text}, {"Acronym", type text},
{"Status", type text}, {"Conditions", type text}, {"Interventions", type
text}, {"Outcome Measures", type text}, {"Sponsor/Collaborators", type
text}, {"Enrollment", Int64.Type}, {"Funded Bys", type text}, {"Study
Type", type text}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed
Type",Text.Split(ColumnsToKeep,","))
in
#"Removed Other Columns"
Once they are made dynamic, your power query will show you an error as we have not filled
the values in our parameters sheet. No worries here! Simply rename the power query
connection to Data like you can see in the left hand side of my screenshot and then click on
'Close and Load':
Now, once I give the required parameter values in my power query with the Sample.xlsx file
path, sheet name and the columns I am interested in separated by commas (in my case NCT
Number and Title columns) as you can see below and click on the 'Refresh All' button, my
data gets populated in the 'Data' sheet (The sheet name is same to the query connection name
that you have renamed in prior step):
So now since our Power Query is ready, we can create a custom action which will replicate
the manual UI action of clicking on 'Refresh All' button that we are doing via VB .NET code.
But before this a very important step! You need to clear all the data and parameter
values first as we will be inserting them via Blue Prism so delete all the parameter
values and remove all rows (except column headers) from your data sheet as shown
below:
Now the next important step is to disable background refresh as we will manually invoking
the refresh of the power query from Blue Prism's code stage. So keep you cursor on the first
row below your headers row in 'Data' sheet and go to Connection Properties as shown below:
Now here in Properties dialog, you need to uncheck the 'Enable background refresh option'
and click on 'OK' button as shown below:
Now go to Blue Prism and you can extend the 'MS Excel - VBO' or 'MS Excel -Extended'
VBO whichever you wish to extend by creating another action called 'Refresh Power Query
Connections' with input parameters as 'handle' (Number), 'workbook' (text) and output
parameters as 'Message' (text) and 'Result' (Flag) as shown below:
In the code stage you need to insert the following parameters and the code as shown below:
Code:
Try
message = ""
Dim wb as Object = _
GetWorkBook(handle, workbookname)
wb.RefreshAll
result = True
Catch ex As Exception
result = False
message = ex.Message
End Try
Now to test the same, create the workflow in process studio as shown below:
Here in the workflow, I have created an instance then opened the Power Query File.xlsx
workbook and then I have written the cell values in the 'Parameters' sheet once I activated it
with the values of the file path (Path for Sample.xlsx), Sheet Name (Sheet1) and Columns To
Keep (NCT Number,Title) and then I invoked the Refresh Power Query Connection actions
via Action stage which we created in the prior step and then I am saving the output as
Output.xlsx file and then I closed all the instances.
You can see the Output.xlsx file that got created and within 5 seconds from Debug Mode in
Fast speed (Control room will be probably be even faster!)
Now going ahead you can probably use this file for any sort of operation that you require to
perform!