<?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: For loop to create dynamic field names in Qlik Sense in Connectivity &amp; Data Prep</title>
    <link>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2490371#M14271</link>
    <description>&lt;P&gt;Thanks for having a look, Kushal.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below I have shared sample data. To be clear, the data in columns A to F I have already available in the table. I want to create column G and further.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think Qlik should first create the (dynamic) field names which are essentially the first 14 month-end dates after the&amp;nbsp;&lt;EM&gt;Reference&amp;nbsp;&lt;/EM&gt;date. Then it should calculate the numbers as shown in G2 until AF3.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Nov 2024 15:48:10 GMT</pubDate>
    <dc:creator>Ostuni</dc:creator>
    <dc:date>2024-11-01T15:48:10Z</dc:date>
    <item>
      <title>For loop to create dynamic field names in Qlik Sense</title>
      <link>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2489324#M14250</link>
      <description>&lt;P&gt;I have a table (Table1) with the following columns:&lt;BR /&gt;Job number |&amp;nbsp;Start date |&amp;nbsp;End date |&amp;nbsp;Reference date |&amp;nbsp;Remaining job amount | Total job days&lt;/P&gt;
&lt;P&gt;Remaining job amount: monetary value that signals how much company still needs to pay for the job&lt;BR /&gt;Total job days: total number of days that the job is running; calculcated&amp;nbsp; by end date - start date + 1&lt;/P&gt;
&lt;P&gt;I want to create a loop that adds 14 new columns to the table. The column names should be the next 14 month-ends starting from the Refence date. The loop should calculate and assign what&amp;nbsp;&lt;EM&gt;remaining job amount&amp;nbsp;&lt;/EM&gt;is to be paid in the next 14 months, so the company can see what payments are due for those next 14 months.&lt;/P&gt;
&lt;P&gt;I do this by calculating the number of days for the 14 next months, divide this number by the&amp;nbsp;&lt;EM&gt;total job days&amp;nbsp;&lt;/EM&gt;and multiply it by the&amp;nbsp;&lt;EM&gt;remaining job amount&lt;/EM&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wrote the following FOR loop for this:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;LET vStartDate = MakeDate(2024, 09, 30);
LET vEndDate = MakeDate(2025, 10, 31);
LET vMaxColumns = 14; 
LET vColumnCount = 0;
SET vColumns = ''; 

FOR vDate = vStartDate TO vEndDate STEP 1
   IF Day(vDate) = Day(MonthEnd(vDate)) THEN

      LET vDateFormatted = Date(vDate, 'DD-MM-YYYY');

      LET vCurrentColumn =
         'max(0, min([Start date], Date(' &amp;amp; vDate &amp;amp; ')) - ' &amp;amp;
         'max([Start date], Date(' &amp;amp; vDate &amp;amp; ') - Day(Date(' &amp;amp; 
         vDate &amp;amp; ')) + 1) + 1) * [Remaining job amount] / 
         [Total job days] ' &amp;amp;
         'AS [Month' &amp;amp; vDateFormatted &amp;amp; ']';

      LET vColumns =
         If(Len(Trim(vColumns)) &amp;gt; 0, vColumns &amp;amp; ', ', '') &amp;amp; 
        vCurrentColumn;

      LET vColumnCount = vColumnCount + 1;

      TRACE Generated column: Month_$(vDateFormatted);

      // Stop loop if maximum # columns has been reached
      IF vColumnCount &amp;gt;= vMaxColumns THEN
         EXIT FOR; 
      ENDIF
   ENDIF
NEXT vDate

NoConcatenate
Table2:
LOAD
[Job number],
[Start date],
[End date],
[Reference date],
[Remaining job amount],
[Total job days],
$(vColumns) 
RESIDENT Table1;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;By adding the TRACE statement I can see in the log that Qlik is not exiting the loop properly and is not moving on to the next part of the script when it hits the maximum of 14 columns. So, it is stuck at creating the last month-end column 'Month_31-10-2025'.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I suspect it has to do with my ending of the loop; but I cannot figure out exactly what it is.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Oct 2024 14:35:00 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2489324#M14250</guid>
      <dc:creator>Ostuni</dc:creator>
      <dc:date>2024-10-27T14:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: For loop to create dynamic field names in Qlik Sense</title>
      <link>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2489408#M14253</link>
      <description>&lt;P&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/318065"&gt;@Ostuni&lt;/a&gt;&amp;nbsp; It doesn't look right approach to create a fields in the script this way. You could do it on frond end expression instead. If you can share sample data with expected output in excel with dummy data, you will get a better solution if possible&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 10:32:55 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2489408#M14253</guid>
      <dc:creator>Kushal_Chawda</dc:creator>
      <dc:date>2024-10-28T10:32:55Z</dc:date>
    </item>
    <item>
      <title>Re: For loop to create dynamic field names in Qlik Sense</title>
      <link>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2490371#M14271</link>
      <description>&lt;P&gt;Thanks for having a look, Kushal.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below I have shared sample data. To be clear, the data in columns A to F I have already available in the table. I want to create column G and further.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think Qlik should first create the (dynamic) field names which are essentially the first 14 month-end dates after the&amp;nbsp;&lt;EM&gt;Reference&amp;nbsp;&lt;/EM&gt;date. Then it should calculate the numbers as shown in G2 until AF3.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2024 15:48:10 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2490371#M14271</guid>
      <dc:creator>Ostuni</dc:creator>
      <dc:date>2024-11-01T15:48:10Z</dc:date>
    </item>
    <item>
      <title>Re: For loop to create dynamic field names in Qlik Sense</title>
      <link>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2490413#M14276</link>
      <description>&lt;P&gt;Hi, in case it helps: I think it gets stuck because you are using max() and Min(). Try using RangeMax() and RangeMin().&lt;/P&gt;
&lt;P&gt;Max and min uses first parameter as the field/value, and the second as the rank, in case you want to get the 2nd, 3rd... max/min.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://help.qlik.com/en-US/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/AggregationFunctions/max.htm" target="_blank"&gt;https://help.qlik.com/en-US/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/AggregationFunctions/max.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;RangeMax and RangeMin returns the max/min value of all values in parameters.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://help.qlik.com/en-US/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/ChartFunctions/RangeFunctions/rangemax.htm" target="_blank"&gt;https://help.qlik.com/en-US/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/ChartFunctions/RangeFunctions/rangemax.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Nov 2024 08:13:57 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2490413#M14276</guid>
      <dc:creator>rubenmarin</dc:creator>
      <dc:date>2024-11-02T08:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: For loop to create dynamic field names in Qlik Sense</title>
      <link>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2490454#M14278</link>
      <description>&lt;P&gt;Hi, this was indeed the solution. I replaced all max() and min() within the loop by RangeMax() and RangeMin().&lt;/P&gt;
&lt;P&gt;Thanks a lot!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Nov 2024 13:54:20 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2490454#M14278</guid>
      <dc:creator>Ostuni</dc:creator>
      <dc:date>2024-11-03T13:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: For loop to create dynamic field names in Qlik Sense</title>
      <link>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2490458#M14279</link>
      <description>&lt;P&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/318065"&gt;@Ostuni&lt;/a&gt;&amp;nbsp; You might have got the solution but as I mentioned, it is not optimal solution as creates columns for each month and involved complex loops. It should be row based so that you can use it as dimension. Just a suggestion, you can take a call based on your scenario&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;Data:
LOAD
    "Job number",
    "Start date",
    "End date",
    "Reference date",
    MonthEnd("Reference date",IterNo()) as MonthEnd_Next_14_Months,
    "Remaining job amount",
    "Total job days"
FROM [lib://DataFiles/Example table Qlik.xlsx]
(ooxml, embedded labels, table is Sheet1)
While MonthEnd("Reference date",IterNo())&amp;lt;=monthend(addmonths("Reference date",14)); // here you can set the variable to create values for next 'n' months instead of 14

Final:
Load *,
      rangemax(0, floor(RangeMin("End date",MonthEnd_Next_14_Months))-
      RangeMax("Start date",MonthEnd_Next_14_Months-day(MonthEnd_Next_14_Months)+1)+1)*
      "Remaining job amount"/"Total job days" as Required_Next_14_Month_Amount
Resident Data;

Drop Table Data;

&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-11-03 144749.png" style="width: 647px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/173732i5B4F1646B1833427/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-11-03 144749.png" alt="Screenshot 2024-11-03 144749.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-11-03 145056.png" style="width: 647px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/173734iA5FF378411E68BB9/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-11-03 145056.png" alt="Screenshot 2024-11-03 145056.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Nov 2024 14:51:38 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2490458#M14279</guid>
      <dc:creator>Kushal_Chawda</dc:creator>
      <dc:date>2024-11-03T14:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: For loop to create dynamic field names in Qlik Sense</title>
      <link>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2497944#M14386</link>
      <description>&lt;P&gt;Hi Kushal,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again for your support, your solution is more flexible as it is row-based as you said. I implemented it and it works pretty well so far.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have one more question: when implementing this in the data load editor, the expression sum([Remaining job amount]) in the front-end is multiplied by 14. Any idea how to account for this in the script?&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2024 16:50:36 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Connectivity-Data-Prep/For-loop-to-create-dynamic-field-names-in-Qlik-Sense/m-p/2497944#M14386</guid>
      <dc:creator>Ostuni</dc:creator>
      <dc:date>2024-12-13T16:50:36Z</dc:date>
    </item>
  </channel>
</rss>

