Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Write Table now available in Qlik Cloud Analytics: Read Blog
cancel
Showing results for 
Search instead for 
Did you mean: 
Vikash938
Contributor III
Contributor III

Loops

what are loops in Qlik Sense and explain with example how it works?

 

1 Solution

Accepted Solutions
seanbruton

What Are Loops in Qlik Sense?

In the Qlik Sense Data Load Editor, a loop is a control-flow structure that repeats a section of script multiple times.

Qlik's scripting language supports three types of loops:

Loop Type

Purpose

FOR … NEXT

Loop over numeric ranges, counters, lists, or field values

DO … LOOP

Loop until a condition is met

WHILE modifier

Add a repeat condition

Why Use Loops? (Use Cases)

Loops are used in Qlik Sense for tasks that require repeated execution, such as:

  1. Loading multiple similar files

Example: loading monthly CSV files named Sales_2024_01.csv, Sales_2024_02.csv, etc.

  1. Generating dates or time periods

Example: iterating through years, months, days.

  1. API pagination (REST calls)

Loop until "nextPage" is empty.

  1. Dynamic or iterative transformations

Example: creating multiple calendars or loading repeated structures.

  1. Building variable lists and metadata

Example: iterating through table fields.

 

🟦WORKING EXAMPLE 1 — Single Loop

Scenario

You have 12 monthly sales files:

Sales_2024_01.csv

Sales_2024_02.csv

Sales_2024_12.csv

You want to load them all without writing 12 separate LOAD statements.

Script — Single FOR Loop

FOR i = 1 to 12

 

    LET vMonth = Num(i, '00');   // Formats 1 → 01, 2 → 02 …

    LET vFile  = 'lib://Data/Sales_2024_' & vMonth & '.csv';

 

    Sales:

    LOAD *

    FROM $(vFile)

    (txt, utf8, embedded labels, delimiter is ',', msq);

 

NEXT i;

How it works

  • The loop runs 12 times.
  • Each time, Qlik builds a filename like Sales_2024_03.csv.
  • Data is appended into the Sales table automatically.

 

🟦WORKING EXAMPLE 2 — Nested Loops (Loop in a Loop)

 

Scenario

You have sales files for multiple years and months:

Sales_2023_01.csv … Sales_2023_12.csv

Sales_2024_01.csv … Sales_2024_12.csv

Sales_2025_01.csv … Sales_2025_12.csv

You want Qlik Sense to automatically load 3 years × 12 months = 36 files

Script — Loop Inside a Loop

FOR year = 2023 to 2025

 

    FOR month = 1 to 12

 

        LET vMonth = Num(month, '00');

        LET vFile  = 'lib://Data/Sales_' & year & '_' & vMonth & '.csv';

 

        Sales:

        LOAD

            *,

            $(year)    AS Year,

            $(vMonth)  AS Month

        FROM $(vFile)

        (txt, utf8, embedded labels, delimiter is ',', msq);

 

    NEXT month;

 

NEXT year;

How it works

  • Outer loop iterates through years (2023 → 2025)
  • Inner loop iterates through months (01 → 12)
  • Every combination generates a file path like:

Sales_2024_07.csv

Sales_2025_12.csv

  • Each load appends into one final Sales table.

 

🟦 Additional Loop Types in Qlik (for completeness)

 

  1. DO…LOOP UNTIL

LET i = 1;

 

DO

    TRACE Loop iteration $(i);

    LET i = i + 1;

LOOP UNTIL i > 5;

  1. FOR EACH (files, variables, inline lists)

FOR EACH file IN FileList('lib://Data/*.csv')

    LOAD * FROM $(file);

NEXT file;

 

🎯Summary

Loop Type

When to Use

FOR … NEXT

Numeric ranges, counters, dates

FOR EACH

Iterating over file lists or variables

DO … LOOP UNTIL

Continue until a condition is met

Nested Loops

Multi-dimension iteration (years × months etc.)

Loops are extremely useful in dynamic data loading, eliminating repetitive scripts and making Qlik apps maintainable.

View solution in original post

1 Reply
seanbruton

What Are Loops in Qlik Sense?

In the Qlik Sense Data Load Editor, a loop is a control-flow structure that repeats a section of script multiple times.

Qlik's scripting language supports three types of loops:

Loop Type

Purpose

FOR … NEXT

Loop over numeric ranges, counters, lists, or field values

DO … LOOP

Loop until a condition is met

WHILE modifier

Add a repeat condition

Why Use Loops? (Use Cases)

Loops are used in Qlik Sense for tasks that require repeated execution, such as:

  1. Loading multiple similar files

Example: loading monthly CSV files named Sales_2024_01.csv, Sales_2024_02.csv, etc.

  1. Generating dates or time periods

Example: iterating through years, months, days.

  1. API pagination (REST calls)

Loop until "nextPage" is empty.

  1. Dynamic or iterative transformations

Example: creating multiple calendars or loading repeated structures.

  1. Building variable lists and metadata

Example: iterating through table fields.

 

🟦WORKING EXAMPLE 1 — Single Loop

Scenario

You have 12 monthly sales files:

Sales_2024_01.csv

Sales_2024_02.csv

Sales_2024_12.csv

You want to load them all without writing 12 separate LOAD statements.

Script — Single FOR Loop

FOR i = 1 to 12

 

    LET vMonth = Num(i, '00');   // Formats 1 → 01, 2 → 02 …

    LET vFile  = 'lib://Data/Sales_2024_' & vMonth & '.csv';

 

    Sales:

    LOAD *

    FROM $(vFile)

    (txt, utf8, embedded labels, delimiter is ',', msq);

 

NEXT i;

How it works

  • The loop runs 12 times.
  • Each time, Qlik builds a filename like Sales_2024_03.csv.
  • Data is appended into the Sales table automatically.

 

🟦WORKING EXAMPLE 2 — Nested Loops (Loop in a Loop)

 

Scenario

You have sales files for multiple years and months:

Sales_2023_01.csv … Sales_2023_12.csv

Sales_2024_01.csv … Sales_2024_12.csv

Sales_2025_01.csv … Sales_2025_12.csv

You want Qlik Sense to automatically load 3 years × 12 months = 36 files

Script — Loop Inside a Loop

FOR year = 2023 to 2025

 

    FOR month = 1 to 12

 

        LET vMonth = Num(month, '00');

        LET vFile  = 'lib://Data/Sales_' & year & '_' & vMonth & '.csv';

 

        Sales:

        LOAD

            *,

            $(year)    AS Year,

            $(vMonth)  AS Month

        FROM $(vFile)

        (txt, utf8, embedded labels, delimiter is ',', msq);

 

    NEXT month;

 

NEXT year;

How it works

  • Outer loop iterates through years (2023 → 2025)
  • Inner loop iterates through months (01 → 12)
  • Every combination generates a file path like:

Sales_2024_07.csv

Sales_2025_12.csv

  • Each load appends into one final Sales table.

 

🟦 Additional Loop Types in Qlik (for completeness)

 

  1. DO…LOOP UNTIL

LET i = 1;

 

DO

    TRACE Loop iteration $(i);

    LET i = i + 1;

LOOP UNTIL i > 5;

  1. FOR EACH (files, variables, inline lists)

FOR EACH file IN FileList('lib://Data/*.csv')

    LOAD * FROM $(file);

NEXT file;

 

🎯Summary

Loop Type

When to Use

FOR … NEXT

Numeric ranges, counters, dates

FOR EACH

Iterating over file lists or variables

DO … LOOP UNTIL

Continue until a condition is met

Nested Loops

Multi-dimension iteration (years × months etc.)

Loops are extremely useful in dynamic data loading, eliminating repetitive scripts and making Qlik apps maintainable.