Coding Techniques in QV
Transcript of Coding Techniques in QV
-
8/10/2019 Coding Techniques in QV
1/5
Coding Techniques Qlikview
Posted onFebruary 27, 2012byKulbir Minhas
This document covers couple of scenario, showing the power of Qlikview scripting and
way to achieve it
Some of sample coding techniques in Qlikview
1. Use of subfield
2. Pick and Match to replace if command
3. Dual Command Usage
4. Use of variable in variable in variable
5. Replacing IntervalMatch with Full Join
6. Optimization using Qlikview7. Set Analysis
8. Use of library
9. Others
10.Sorting Techniques for Pivot table
11.Showing the value for each Dimension (in Pivot table)
In this document, I will cover the first 5 points and remaining points and any additional
topics in separate document (Coding Techniques 2)
Sample Code
Subfield/Pick/Dual/Match
Data:
Load
Dual(RegionText, Match(RegionText,LN,TK,HK))as
Region,
Pick(Match(RegionText, LN,HK,TK),1.0,1.25, 1.50) as
Factor,
*;
LoadSubfield(LN,TK,HK,,)as
RegionText,
Ceil(Rand() * 100) as
Amount
AutoGenerate(100);
http://kulbirminhas.wordpress.com/2012/02/27/coding-techniques-qlikview/http://kulbirminhas.wordpress.com/2012/02/27/coding-techniques-qlikview/http://kulbirminhas.wordpress.com/2012/02/27/coding-techniques-qlikview/http://kulbirminhas.wordpress.com/2012/02/27/coding-techniques-qlikview/http://kulbirminhas.wordpress.com/author/kulbirminhas/http://kulbirminhas.wordpress.com/author/kulbirminhas/http://kulbirminhas.wordpress.com/2012/02/27/coding-techniques-qlikview/http://kulbirminhas.wordpress.com/2012/02/27/coding-techniques-qlikview/ -
8/10/2019 Coding Techniques in QV
2/5
The following example shows you how to use subfield to generate 3 fields from a single
value ->
Subfield(LN,TK,HK, ,) as RegionText,
Pick and Match to replace If condition, applicable only for equaltoexpression
Pick(Match(RegionText, LN, HK, TK, *), 1.0,1.25, 1.50,0.75) as Factor,
Use of Dual Command to assign Numeric value to Region (Generally used for regional
priority or Sorting)
Dual(RegionText, Match(RegionText,LN,TK,HK)) as Region,
Use of Dual Command to assign Numeric value to Region (Generally used for regional
priority or Sorting)
Use of variables:
The following 2 examples slightly different in approach are used in the attached qvwshowing the sum by region
Set 1 using AmountYTD, AmountQTD etc, which is predefined
Set 1:
Let
ReportType = WTD';// The values of variable can WTD, MTD, QTD, YTD
Let
AmountYTD = sum(if( CurrentYearFlag = 1, Amount))';
Let
AmountQTD = sum(if( CurrentQtrFlag = 1, Amount))';
Let
AmountMTD = sum(if( CurrentMonthFlag = 1, Amount))';
Let
AmountWTD = sum(if( CurrentWeekFlag = 1, Amount))';
Set 2 using FlagName and will be using this flag to find the values
Set 2:
Let
FlagName = Current& Pick(match(ReportType,WTD,MTD,QTD,YTD),Week,Month,Qtr,Year)& Flag';
The use of variables can be multiple levels, in Qlikview, the output of one variable can be
used as input to anothervariable
The good part is it can be used on either of equationas in case-1, it is used on LHS and
in case-2 it is used on RHS
Use of Peek/Previous and Interval Match
-
8/10/2019 Coding Techniques in QV
3/5
Scenario
Table1: Records with Amount and TimeKey
Table2: Records with FxRate and TimeKey
Table 2 Does not have FxRate For all TimeKey, so assuming it has records for
TimeKey 1,7, 30 only
Then records
1-6 -> will use TimeKey = 1
7-29 -> will use TimeKey = 2
And 30 onwards will use TimeKey = 3
Example with Interval Match
Keys:
Load
Alt(Peek(KeyEnd),-1)+1 asKeyStart,
Key
as
KeyEnd,
FX;
Load
(Recno() 1 )* 10 + 1 as
Key,
Rand() as
FX
Autogenerate(10);
Concatenate(Key)
Load
KeyEnd+ 1 as
KeyStart,
1000 as
KeyEnd,FX
ResidentKey
where
KeyEnd= peek(KeyEnd,-1);
Data:
Load
-
8/10/2019 Coding Techniques in QV
4/5
recno() as
Key,
Round(Rand()*100) as
Amount
Autogenerate(100);
IntervalMatch( Key)
Load
KeyStart,
KeyEnd
ResidentKeys;
Alternate Example with Left Join
Data:
Loadrecno() as
Key,
Round(Rand()*100) as
Amount
Autogenerate(100);
Key:
Load
Alt(Peek(KeyEnd),-1)+1 as
KeyStart,
Key
as
KeyEnd,
FX;
Load
(Recno() 1 )* 10 + 1 as
Key,
Rand() asFX
Autogenerate(10);
Concatenate(Key)
Load
KeyEnd+ 1 as
KeyStart,
-
8/10/2019 Coding Techniques in QV
5/5
1000 as
KeyEnd
ResidentKey
where
KeyEnd= peek(KeyEnd,-1);
Left
Join(Data)
Load
*
ResidentKey;
Drop
TableKey;
DataNew:Load
Key,
Amount,
FX,
KeyStart
as
Start,
KeyEnd
as
End
ResidentData
Where
Key>= KeyStart
and
Key