This is Article 22 of the Qlik Sense Data Modeling Course.
📚 Qlik Sense Course – Article 22 of 28
← Previous Article: Monitoring & Logging
→ Next Article: Subroutines
What are Variables & Includes? The foundation of modular Qlik scripts. They enable reuse of configurations, database connections, and code modules for maintainable and scalable solutions. This is the first step toward implementing a true Three-Stage Architecture.
What Will You Learn About Variables & Include Files in Qlik Scripts?
After this guide you will be able to:
- Understand the difference between
SETandLETand use them for optimal performance. - Use Include files (
$(Must_Include)) for modular scripts and centralized configurations. For the full syntax, see the Qlik $(Include) statement reference. - Create environment-specific configurations for DEV/TEST/PROD environments.
Time investment: 20 min reading + 3 hrs hands-on
Quick win: You can have your first Includes implemented in 15 minutes
What Is the Don’t Repeat Yourself (DRY) Principle?
The problem: You’ve copied the same database connection or the same complex date check into 15 different apps. When the password changes or the logic needs to be adjusted, you have to touch all 15 apps individually — a maintenance nightmare!
The solution: Centralized Include files. A change in one place (the central Include file) is automatically propagated to all apps that call that module.
What Is the Critical Difference Between SET and LET?
The difference between SET and LET is fundamental to the performance of your application and the stability of your Expressions. For the full documentation on how variables work in scripts, see the Qlik variables in scripts documentation.
| Command | Behavior | When to use? |
|---|---|---|
| SET | Stores the value as a literal string (text). Evaluation happens at the point of use (e.g., in the chart). | Frontend expressions, paths, static strings, complex Set Analysis. |
| LET | Evaluates the expression immediately (at script load time) and stores the result (usually a number). | Calculations, functions, dates, counters for error handling. |
What Is SET – The Dynamic (Chart) Expression in Qlik Scripts?
// SET stores the text EXACTLY: "Sum(Sales) * 1.2"
SET vFormula = Sum(Sales) * 1.2;
// Usage in a chart expression
=$(vFormula)
// Important: The formula is only evaluated in the chart object and cached there.
Performance advantage: When you define a complex formula (e.g., Set Analysis) with SET, the text is stored in the frontend and optimized by the Qlik engine cache.
What Is LET – The Static (Script) Calculation in Qlik?
// LET calculates IMMEDIATELY (in the script)
LET vTotal = 100 + 50;
// vTotal contains: 150 (number)
LET vToday = Today();
// vToday contains today's date as a numeric serial number
Use case: Whenever you work with functions like `Today()`, `Max()`, or `Year()`, you must use `LET` to store the result.
What Is Dollar-Sign Expansion in Qlik Scripts?
Dollar-Sign Expansion $(variable) expands the value of the variable **before** Qlik parses and executes the statement. It is the key to being able to use variables at all.
SET vRegion = 'North';
LET vCurrentAmount = 5000;
// Expansion in a LOAD statement
LOAD *
FROM;
// Becomes: LOAD * FROM;
// Expansion in a WHERE clause (numbers don't need quotes)
WHERE Amount > $(vCurrentAmount);
// Becomes: WHERE Amount > 5000;
Tip: Be careful when using SET variables in Set Analysis. Here you often need to combine the formula (SET) with the expansion (LET). Read more about advanced Qlik Expressions.
What Are Include Files in Modular Scripts?
Include files are external text files (often with the `.qvs` extension) that Qlik inserts and executes into the main script during load.
What Are the Differences Between $(Include) and $(Must_Include)?
| Command | Error if file is missing? | When to use? |
|---|---|---|
$(Include) |
No, the script continues running. | Optional files or non-critical code blocks. |
$(Must_Include) |
Yes, the script stops immediately (Error Code 8: File not found). | Critical configuration or connection files. |
What Does the Basic Structure of the Main Script Look Like?
//============================================
// MAIN SCRIPT
//============================================
// Core Configuration (MUST exist)
$(Must_Include=lib://Scripts/01_Config.qvs)
// Subroutines (see next article)
$(Must_Include=lib://Scripts/03_Subroutines.qvs)
// Here: The actual load statements of the app
$(Must_Include=lib://Scripts/04_Load_Staging.qvs)
How Do Variables & Include Files Work in Qlik Scripts?
The configuration file collects all constants needed throughout the app in one central location:
//============================================
// 01_Config.qvs - Central Configuration
//============================================
// System Settings
SET DateFormat='DD.MM.YYYY';
// Path Variables (central paths to QVD archive, etc.)
SET vQVDPath = 'lib://QVDArchive/'; // Portable thanks to lib://
SET vLogPath = 'lib://Logs/';
// Date Variables
LET vToday = Today();
LET vCurrentYear = Year(Today());
LET vPreviousYear = Year(Today()) - 1;
// Business Thresholds
SET vHighValueThreshold = 10000;
SET vTargetMargin = 0.25;
// Optional: Central definition for the Master Calendar here
// Read our guide on the Master Calendar.
Best Practice: Often, LIB CONNECT TO statements are also stored in a dedicated Include file to manage connection details centrally. This is especially useful during Qlik Cloud migration, since connections there must be administered centrally.
What Are Environment-Specific Configurations: DEV vs. PROD?
To keep the codebase in sync between development (DEV) and production (PROD), environment variables should be used. This prevents test data from leaking into production (and vice versa).
The goal is that **only a single value** (`vEnvironment`) needs to be changed in the script to switch the entire behavior of the app.
How Does Environment Detection Work in the Main Script?
// 1. Retrieve environment setting (can be set manually or automatically)
SET vEnvironment = 'DEV'; // E.g., manually set to DEV in the dev script
// 2. Load the environment-specific config
// Qlik expands this to Config_DEV.qvs or Config_PROD.qvs
$(Must_Include=lib://Scripts/Config_$(vEnvironment).qvs)
What Does the Example File Config_DEV.qvs Look Like for Variables & Include Files?
//============================================
// Config_DEV.qvs - DEVELOPMENT Configuration
//============================================
SET vDataPath = 'C:DevQlikData';
SET vDBServer = 'DEV-SQL-01';
// Important: A row-limit variable can be used in DEV
SET vDebugMode = 1;
SET vRowLimit = 100000; // Limit for faster development
What Does the Example File Config_PROD.qvs Look Like for Variables and Include Files?
//============================================
// Config_PROD.qvs - PRODUCTION Configuration
//============================================
SET vDataPath = 'prod-nasQlikData';
SET vDBServer = 'PROD-SQL-CLUSTER';
SET vDebugMode = 0;
SET vRowLimit = 0; // No limits in production
What Are lib:// Connections and Portability in Qlik Scripts?
Always use `lib://` connections instead of absolute paths (`C:…` or `server…`).
A `lib://` connection is a **data connection name** defined centrally in the Qlik Management Console (QMC). When you move your code to a different server or to Qlik Cloud, you only need to update the `lib://Scripts` connection — not the code in 50 different apps.
// Wrong (not portable)
$(Must_Include=serverscripts 1_Config.qvs)
// Right (portable)
$(Must_Include=lib://ScriptLibrary/01_Config.qvs)
Efficiency boost: Use variables together with QVDs to dramatically reduce your load times. Define the QVD path centrally ($(vQVDPath)) and use it across all applications.
What Are the Best Practices for Variables & Include Files in Qlik Scripts?
Follow these rules to create stable and maintainable modules.
✓ Variable usage:
- [ ] **SET** for static text, paths, and **all** chart expressions.
- [ ] **LET** for dates, functions, and counters.
- [ ] Use the prefix `v` (e.g., `vCurrentYear`) for variables.
✓ Include files:
- [ ] Modular structure: Config → Functions → Subroutines.
- [ ] `$(Must_Include)` for critical configuration or error-handling files.
- [ ] **Always** use `lib://` connections for portability.
✓ Environments:
- [ ] Separate configs (e.g., `Config_DEV.qvs`) for each environment.
- [ ] Use `$(vRowLimit)` during development to limit large data volumes.
How Do I Troubleshoot Problems With Variables & Include Files in Qlik Scripts?
⚠️ Problem: Variable does not expand
Solution 1: Check the syntax
// Wrong
TRACE Value: $vYear
// Right
TRACE Value: $(vYear)
Solution 2: Check SET vs LET
If the variable expects a numeric result (e.g., a date number) but contains the wrong value, it was incorrectly defined with `SET` instead of `LET`, or it is being expanded too late.
⚠️ Problem: Include file not found
Solution: Check path and connection
- Make sure the `lib://` connection (e.g., `ScriptLibrary`) in the QMC points correctly to the storage location.
- Check with
TRACE FileSize('lib://Scripts/Config.qvs')in the script whether the file is visible at all.
⚠️ Problem: SET vs LET confusion
Rule of thumb:
// Functions? → LET
LET vDate = Today();
// Static text/path/expression? → SET
SET vPath = 'C:Data';
SET vExpr = Sum(Sales);
What Are the Next Steps With Variables & Include Files in Qlik Scripts?
You can now build modular scripts! Next, you will learn how to create reusable code blocks:
1. Subroutines: In the next article you will learn how to use `SUB` and `CALL` to encapsulate complex load processes into reusable, parameterizable functions.
2. Monitoring: The logging functions discussed in the Monitoring & Logging article should also be moved into a central Include file so they can be used in every app.
What Related Topics Are Covered in the Course on Variables & Include Files?
- Subroutines – Code reuse with SUB/CALL
- Three-Stage Architecture – The architecture concept behind modularity
Previous: Error Handling | Next: Subroutines