25 December 2013

Sending Qlikview Reports in Excel

Would not it be nice if we could send some qlikview charts by email to a recipient automatically?

By using qlikview OCX and a piece of C# code, we can do this!!!

Let's examine the attached solution:

1) We need an excel template under template folder in which the position and the name of the qlikview chart to be transferred into excel is defined.
For example:
In the screenshot below, the definition CH01;image;0.5 in cell A1 means, export the chart CH01 from qlikview as an image and resize it by factor 0.5 and position its top-left corner with cell A1.
The definition CH02;text in cell H1 means, export the chart CH02 from qlikview as text and position its top-left cell with cell H1.

2) The output folder is used to copy the template into with a unique name and transfer the data from qlikview to this unique excel file before it is sent by email.

3) The qlikview file(s) should be placed in the same directory as template and output folder.

4) The excel template should have the same name as qlikview file. For example: if the qlikview file is named as Contoso-Simple.qvw then the excel file should be named as Contoso-Simple.xlsx.

5) The email account credentials which will be used to send the email are stored in app.config file.

How to use the application:

Before using the application, provide the email account credentials in app.config file which is under reportertest\Reporter\Reporter folder.

1) Manual use: double click on the reporter.exe under reportertest\Reporter\Reporter\bin\Debug folder. In the opening screen:
  • select the folder where qlikview files and template and output folders are located
  • select the qlikview file
  • make any selection in the qlikview file shown in the big frame
  • enter email address to which the excel report will be sent
  • click on Send XLS

2) Automatic use:  The reporter.exe can also be called from console or scheduled to run automatically in windows scheduled tasks. While calling from console or scheduling it requires five arguments:

arg 1: full path of the file
arg 2: user name (if section access is used in qlikview file)
arg 3: user password (if section access is used in qlikview file)
arg 4: selections to be made in the qlikview file 
arg 5: the email address to which the report will be sent

For example: The following console command will open the reporter application, access the contoso-simple.qvw file, then make the selections (20071 and 20072 as CalendarQuarter Online as ChannelName) and send out the excel report to xyz@gmail.com.

Reporter.exe "C:\\Users\\Bilge\\Desktop\\HappyStartinUSA\\OCX\\Contoso-Simple.qvw" "" "" "<CalendarQuarter:number:20071,20072><ChannelName:text:Online>" "xyz@gmail.com"

Source Code

Here is the source code and sample files (template, qvw file): reportertest.zip

05 August 2013

Qlikview: Missing data points due to data reduction

If data reduction is enabled, the data available in the qlikview application is filtered according to the user logging in. For example, if the logging in user has no "sales" data associated with him/her for "Product A", "Product A" will be filtered out for this user.

This may make sense in most of the cases, however same thing exists for "date" data too. If the logging in user has no "sales" data for "July 2012", then once logged in, (s)he will not have "July 2012" data point in his/her "sales per month" chart. This may not be what is expected; expected chart behavior might be to have a "0" value data point for "July 2012".

A possible workaround for this issue is as follows:

1) Since data reduction is filtering out data, we need to prevent data reduction in "date" data. This can be achieved by the STAR function of qlikview (Pls see the script in the sample qlikview app). In the sample qlikview app, I preferred to create a duplicate of DateDim table in which data reduction is prevented, that way I can use the regular DateDim table as usual and the duplicate table (DateDim_ALL) for recuperating the filtered out "date" data.

In the sample qlikview application, please login as "User1" and observe that "CalendarMonth" field is missing "200802" and "200803" but  "CalendarMonth_ALL" field does not.

2) Create a chart with "CalendarMonth_ALL" as dimension and observe that missing dates are still missing :(.

3) Add a hidden expression (as first expression) that counts the "CalendarMonth_ALL" between the MAX and MIN dates available in fact table.

4) Set dimension limit to show "greater than zero", as dimension limit is applied to first expression, it is applied to the expression mentioned in the step above.

5) Make the chart read-only, as selections made on "CalendarMonth_ALL" won't be reflected across the application.

Please see the bottom chart in the sample application as an example. Try logging in with "User1", "User2" and "User3".

Here is the link to download the sample application: missing.qvw

19 April 2013

Qlikview: Highlighting selected dimensions

If there are too many data points (or lines, or bars, etc) displayed in a chart, the user might want to highlight the ones which (s)he is interested in while still keeping the others in a lose color to be able to make comparison.

This can be done as follows:

  1. Let user make selections in dimensions
  2. Get selected values of each dimension into a separate variable when a button (e.g HIGHLIGHT) is clicked
  3. If a dimension value of a chart do not exists in the values kept in the variable, then color it as light gray. This can be done by background color setting.

Here is a link to the example qlikview application: Highlight.qvw

Please, open the highlight.qvw, make a couple of selections in list boxes and click on Highlight Selected button and observe the highlighting.

13 December 2012

Qlikview: How to send a report by email from access point

Step 1)
Create a report on a qlikview application which has some dummy data. (e.g. sales report in the attached qlikview file.)

Step 2)
Create a field in the qlikview applications to keep recipient email address. (e.g. recipient field in the attached qlikview file.)  and display it in a listbox.

Step 3)
Create a variable in the qlikview application to keep a string which will contain recipient's email address and the name of the output PDF file separated by semi colon.  (e.g vPdfName2 variable in the attached qlikview file.)

Step 4)
Create a listbox in the qlikview application which will display the content of the variable.

Step 5) 
Create a button in the qlikview application that will call the following macro:

Sub PrintRpt()
        'print the report and get the reponse back in a variable
set printResponse=ActiveDocument.PrintReportEx("RP01")

'get the name of the PDF file created by the print action above

        'get the email address of the recipient selected in the recipients listbox
set recipient=ActiveDocument.fields("Recipient").GetSelectedValues
set email=recipient.Item(0)

        'set the concatenated string (email address and name of the PDF file) as content of the listbox created in step 4
ActiveDocument.Variables("vPdfName2").SetContent chr(39) & email.text & ";" & sURL & chr(39), true

'set the path of the folder in which the text file will be crated
sFile="C:\_QVW\PDFfolder\WatchThis\email_" & replace(sURL,".pdf","") & ".txt"

        'Export the content of the listbox into a text file that contains the email address and name of the PDF file
set lb = ActiveDocument.GetSheetObject("LB05")
lb.ServerSideExportEx sFile , ";" , 1 '0=HTML, 1=Text, 2=Bitmap, 3=XML, 4=QVD, 5=BIFF
End Sub

Step 6)
Write a powershell script that can read a text file to get email address and path of the pdf file and send it by email.

Following posts have related examples:
how to loop through files in a folder by windows powershell
How to send email by windows powershell

Step 7) 
Schedule a windows task which will monitor the folder for incoming files and trigger the powershell script to read the file's content and send email with the PDF attached.

Following post have a related example:
How to monitor a folder and trigger an action for incoming files, in Windows 7
The only difference from the above example will be the source defined in the XML definition of the event filter. Instead of  "C:\windows\explorer.exe", we should now have "C:\Program Files\QlikView\Server\QVS.exe", because the text file will be created by qlikview server.

Deploy the qlikview application on the server and open it from access point and click on the button to see the result.

Example File: email_report.rar

11 December 2012

How to loop through files in a folder by windows powershell

Step 1:
Create a powershell file named as loopfiles.ps1 (you can simply create a new text file and rename it as loopfiles.ps1).

Step 2:
Open the loopfiles.ps1 file and paste the following and save it:


$fso = new-object -com scripting.filesystemobject
$folder = $fso.getfolder("C:\temp")

foreach ($f in $folder.files) {
    $f.path >> filesvisited.txt
$c = Get-Content $f.path

Step 3:
Right click on the loopfiles.ps1 and select Run with Powershell.

At the end, one should have an output file (filesvisited.txt) with the names of the files in the current folder.

How to send email by windows powershell

The example below is built to run on gmail accounts.

Step 1:
Create a powershell file named as send_email.ps1 (you can simply create a new text file and rename it as send_email.ps1).

Step 2:
Open the send_email.ps1 file and paste the following and save it:

function fnSendEmail($Too,$Filee)
$smtpServer = "smtp.gmail.com"
$att = new-object Net.Mail.Attachment($Filee)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer, 587)
$smtp.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential("your-email@gmail.com", "your-password");
$msg.From = "your-email@gmail.com"
$msg.Subject = "powershell test"
$msg.Body = "This is a test email with an attachment"

Step 3:

  • Start windows powershell 
  • change the directory to where the send_email.ps1 file is located (pls see the line 1 in the screenshot below)
  • call send_email.ps1 file (pls see the line 2 in the screenshot below), like that, one can use the fnSendEmail function in it
  • call fnSendEmail function by passing to parameters (recepient's email address and the full path of the file to be attached) (pls see the line 3 in the screenshot below)

10 December 2012

How to monitor a folder and trigger an action for incoming files, in Windows 7

Step 1:

Copy a file into a folder and look for the details of this event in windows event viewer. For example, if  copy a file named test - Copy.txt in C:\_QVW\PDFfolder\WatchThis folder, I found the following entry in the windows event viewer:

Make note of the event id (4656) and keyword (Audit Success) and that we are looking at security windows logs.

Step 2:
Go to the windows task scheduler and click on create task.

Step 2.1:
In general tab, name your trigger as My Folder Monitor as follows:

Step 2.2:
In triggers tab, click on New. In New Trigger window, set begin the task to On an event and select custom in settings, then click on New Event Filter button. In New Event Filter window, set the options as follows:
Note that we set the options according to what we have found in step 1.
Click OK and OK to finish this step.

Step 2.3:
In the action tabs, click on New. In the New Action window, set the options as follows:
Click OK and OK to finish this step.

Step 3:
Now, we need to tell the task which folder to monitor. 

Step 3.1:
Go back to the windows event that we looked at in step 1. Open the event details by double clicking the event and take note of the details of the event. We can use ObjectName and ProcessName details which are under the EventData node to fine tune our scheduled task.

Step 3.2:
Go to triggers tab of the scheduled task and click on Edit and then edit event filter. Note that, it is opened in XML tab and one cannot go back to Filter tab. From now on, the event filter can only be modified in XML. 
Modify the XML expression to restrict the scheduled task to trigger the action only for the files in C:\_QVW\PDFfolder\WatchThis folder, as follows:
Please note that &gt; means > (i.e greater than).

Now if you copy & paste or create a file in C:\_QVW\PDFfolder\WatchThis folder, a pop-up message should appear in the screen.

Hope it works on your side as well.