<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to Create a Dynamic Sliding Window in Qlik Script Using Window() Function in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527229#M107364</link>
    <description>&lt;P&gt;Does it occur immediately which means the syntax isn't accepted or during the execution which would mean that there is a not intercepted run-time error?&lt;/P&gt;&lt;P&gt;You may try to out-source this expressions by calculating the offset at first and applying the window within a following load-step - means executed in a preceding part or maybe in the load which created the source for the used resident-table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Aug 2025 14:02:00 GMT</pubDate>
    <dc:creator>marcus_sommer</dc:creator>
    <dc:date>2025-08-13T14:02:00Z</dc:date>
    <item>
      <title>How to Create a Dynamic Sliding Window in Qlik Script Using Window() Function</title>
      <link>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527197#M107351</link>
      <description>&lt;P&gt;Hello Team,&lt;/P&gt;&lt;P&gt;I’m trying to create a sliding window calculation directly in the Data Load Editor using the Window() function.&lt;/P&gt;&lt;P&gt;My goal is to apply this sliding window on transactional data to calculate metrics (e.g., average unit price) for the last 3 months.&lt;BR /&gt;The challenge is that the number of rows in my dataset varies by period and grouping, so the start and end range of the window needs to be determined dynamically rather than using a fixed offset tipicaly this would be the case of RANGE framing if I'm not mistaken.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Table_X:
NoConcatenate
LOAD
    [Purchase #],
    [Invoice Date],
    [Month],
    [Company],
    [Client],
    [Segment],    
    [Index],
    [Constituents],
    [Package],
    [Description],
    [Unit Price],

    // Window Calculation  
    // Avg unit price across all transactions in the 3-month window
    Window(
        Avg([Unit Price]),
        [Constituents], [Package], [Description],
        'ASC', Month, RANGE_START, RANGE_END
    ) AS [Avg_3M]
RESIDENT SomeSourceTable;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;What I need help with:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;P&gt;How can I make RANGE_START and RANGE_END &lt;STRONG&gt;dynamic&lt;/STRONG&gt; so that they always capture the last 3 months based on the current row’s month in the transactional data?&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;Is there a way to pass a calculated start/end position to Window() inside the script rather than hardcoding it?&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I’ve reviewed the Qlik documentation on Window(), but all the examples use fixed numeric offsets.&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Aug 2025 10:30:36 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527197#M107351</guid>
      <dc:creator>guilherme-matte</dc:creator>
      <dc:date>2025-08-13T10:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create a Dynamic Sliding Window in Qlik Script Using Window() Function</title>
      <link>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527217#M107357</link>
      <description>&lt;P&gt;The help states them clearly as expression which means they are in general dynamically. In your case you may try to apply something like this:&lt;/P&gt;&lt;P&gt;window(..., addmonths(myDate, -3), myDate) as ...&lt;/P&gt;</description>
      <pubDate>Wed, 13 Aug 2025 12:39:20 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527217#M107357</guid>
      <dc:creator>marcus_sommer</dc:creator>
      <dc:date>2025-08-13T12:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create a Dynamic Sliding Window in Qlik Script Using Window() Function</title>
      <link>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527223#M107359</link>
      <description>&lt;P&gt;Hey Marcus!&lt;/P&gt;&lt;P&gt;Thank you for your input, really appreciate it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried using expressions before, as well as your suggestion; however, I face the following issues whenever adding expressions to the range_start and range_end:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;The following error occurred:
Window start expression is invalid
The error occurred here:
Tx_with_3m:
NoConcatenate
Load
    [Purchase #],
    [Invoice Date],
    [Month],
    [Company],
    [Client],
    [Segment],    
    [Index],
    [Constituents],
    [Package],
    [Description],
    [Unit Price],
    Window( Avg([Unit Price]),
    	[Constituents], [Package],
        'ASC', Month, AddMonths(MonthStart(Month), -3),  AddMonths(MonthStart(Month), -1)
  ) AS [Avg_3M]
 Resident tempMaster
The following error occurred:
Window start expression is invalid&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basicaly:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The following error occurred:&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;Window start expression is invalid&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This happens only when expressions are on those fields, if I change it for an offset like (-3, -1) it works. However, since it's transactional data, the offset depends on the number of records.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Aug 2025 13:15:33 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527223#M107359</guid>
      <dc:creator>guilherme-matte</dc:creator>
      <dc:date>2025-08-13T13:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create a Dynamic Sliding Window in Qlik Script Using Window() Function</title>
      <link>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527229#M107364</link>
      <description>&lt;P&gt;Does it occur immediately which means the syntax isn't accepted or during the execution which would mean that there is a not intercepted run-time error?&lt;/P&gt;&lt;P&gt;You may try to out-source this expressions by calculating the offset at first and applying the window within a following load-step - means executed in a preceding part or maybe in the load which created the source for the used resident-table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Aug 2025 14:02:00 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527229#M107364</guid>
      <dc:creator>marcus_sommer</dc:creator>
      <dc:date>2025-08-13T14:02:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create a Dynamic Sliding Window in Qlik Script Using Window() Function</title>
      <link>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527237#M107365</link>
      <description>&lt;P&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/193288"&gt;@guilherme-matte&lt;/a&gt;&amp;nbsp;it sounds like you're &lt;STRONG&gt;misinterpreting the purpose&lt;/STRONG&gt; or RANGE_START and RANGE_END parameters of the Window function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Purpose of Range Start and End Expressions&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;RANGE_START and RANGE_END are &lt;STRONG&gt;not&lt;/STRONG&gt; meant to be the "&lt;STRONG&gt;searching&lt;/STRONG&gt;" expressions. Meaning they're &lt;STRONG&gt;not&lt;/STRONG&gt; created to find all records three months prior to the month on current line.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Instead, they're meant to be more of a "&lt;STRONG&gt;filter&lt;/STRONG&gt;" expressions.&amp;nbsp;RANGE_START and RANGE_END are meant to specify &lt;STRONG&gt;how many rows&lt;/STRONG&gt; before and after the current &lt;STRONG&gt;row&lt;/STRONG&gt; you would like the Window function to consider to calculate the average unit price.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Why You're Seeing an Error for Range Start&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The reason you're getting an error for the Window start expression is because the AddMonth function returns a &lt;STRONG&gt;positive number&lt;/STRONG&gt;&amp;nbsp;- like 45778 for May 1st, 2025. But the RANGE_START expression is expecting a &lt;STRONG&gt;negative number&lt;/STRONG&gt;&amp;nbsp;- like how many of previous rows you want the function to consider.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Calculating Prior Three Months' Average&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;As for calculating average for the prior three months, you can do something like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;Table_X:
LOAD *,
//  add key that will be used to map prior 3 months' average unit prices
    AutoNumber(
    	Constituents &amp;amp; '|' &amp;amp;
        Package &amp;amp; '|' &amp;amp;
        [Item Name] &amp;amp; '|' &amp;amp;
        Num(Month)
    )					as avgKey
;
LOAD
    [Purchase #],
    [Invoice Date],
    [Month],
    [Company],
    [Client],
    [Segment],
    [Index],
    [Constituents],
    [Package],
    [Description],
    [Unit Price]
RESIDENT SomeSourceTable;

// loop through each available month
For Each vMonth in FieldValueList('Month')

    // identify month to start calculating averages from
    Let vStartMonth = AddMonths(vMonth, -3);

    // identify transactions of prior three months
    transactions_prior_3_months:
    Load Constituents,
        Package,
        Description,
        [Unit Price],
        Date('$(vMonth)')		as Month
    Resident Table_X
    Where Month &amp;gt;= Date('$(vStartMonth)')
      and Month &amp;lt; Date('$(vMonth)')
    ;

    // calculate prior 3 months' average unit price
    averages:
    Load Constituents,
    	Package,
        Description,
        Avg([Unit Price])		as prior3MonthsAvgUnitPrice
    Resident transactions_prior_3_months
    Group By Constituents,
    	Package,
        Description
    ;

    // create mapping table containing keys to average prices
    averagesMap:
    Mapping
    Load AutoNumber(
    		Constituents &amp;amp; '|' &amp;amp;
            Package &amp;amp; '|' &amp;amp;
            Description &amp;amp; '|' &amp;amp;
            Num(Date('$(vMonth)'))
        )				as avgKey,
    	prior3MonthsAvgUnitPrice
    Resident averages
    ;
    
    Drop Tables transactions_prior_3_months,
    	averages
    ;

Next vMonth;

// map prior three months' average unit prices to each record (where prices are available)
// make average price null if average price in prior 3 months is not available
transactions:
Load *,
	ApplyMap('averagesMap', avgKey, Null())	as prior3MonthsAvgUnitPrice
Resident Table_X
;

Drop Table Table_X;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Aug 2025 01:55:55 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527237#M107365</guid>
      <dc:creator>howdash</dc:creator>
      <dc:date>2025-08-14T01:55:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create a Dynamic Sliding Window in Qlik Script Using Window() Function</title>
      <link>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527240#M107366</link>
      <description>&lt;P&gt;Beside the current struggles with the window-feature be aware that there might be other ways to get the wanted information, for example with classical aggregation-loads with group by and/or using interrecord-functions like peek() and previous().&lt;/P&gt;&lt;P&gt;All methods will have their pros and cons and it will probably be depending on the overall requirements which one is the most suitable.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Aug 2025 14:40:07 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/How-to-Create-a-Dynamic-Sliding-Window-in-Qlik-Script-Using/m-p/2527240#M107366</guid>
      <dc:creator>marcus_sommer</dc:creator>
      <dc:date>2025-08-13T14:40:07Z</dc:date>
    </item>
  </channel>
</rss>

