Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
SatyaPaleti
Creator III
Creator III

Need Last 3 years Data

Hi Folks,

I have a requirement. In a folder there are qvd's from 2011 January to 2019 February. I need to fetch only last 3 years qvd's only. How can we do this. Could some one can help me on this

abc_201101.qvd

abc_201102.qvd

abc_201103.qvd

abc_201104.qvd

abc_201105.qvd

abc_201106.qvd

abc_201107.qvd

          .

          .

          .

abc_201902.qvd

Thanks and Regards,

Satya

 

Labels (1)
4 Replies
PrashantSangle

Hi,

Try below logic

Let vMaxYearMonth=num(Today());
Let vMinYearMonth=num(AddYears(Today(),-3));

TempCalendar:
LOAD
Date(AddMonths($(vMinYearMonth),IterNo() - 1),'YYYYMM') as TempDate
AutoGenerate 1
While AddMonths($(vMinYearMonth),IterNo()-1) <= $(vMaxYearMonth)
;

FOR i = 0 TO NoOfRows('TempCalendar') - 1
LET vYearMonth = peek('TempDate', $(i), 'TempCalendar');

Final:
Load *
FROM [lib://$(qvdPath)\abc_$(vYearMonth).qvd]
(qvd);

Next i;

Drop table TempCalendar;

Regards,
Prashant Sangle
Great dreamer's dreams never fulfilled, they are always transcended.
Please appreciate our Qlik community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved 🙂
jonathandienst
Partner - Champion III
Partner - Champion III

I use this code pattern:

Let vMinDate = AddYears(Today() - 3);

// Creating the start point means that we can force concatenate, even if the field list changes
Data:
LOAD '' as Source Autogenerate 0;

For Each vFile in FileList('*.qvd')  // change to correct path to qvd files
	Let vDate = Date#(TextBetween(vFile, 'abc_', '.qvd'), 'yyyyMM');
	If vDate >= vMinDate Then
	
		Concatenate(Data)
		LOAD *,
			FileBaseName() as Source
		FROM [$(vFile)];
		
	End If
Next

This code will not throw an error if any files are missing, and they will concatenate into a single table even if the field lists change over time. Also, the Source field will have the lineage for the data rows.

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
SatyaPaleti
Creator III
Creator III
Author

Thank you so much Prashanth it works


@PrashantSangle wrote:
Hi,

Try below logic

Let vMaxYearMonth=num(Today());
Let vMinYearMonth=num(AddYears(Today(),-3));

TempCalendar:
LOAD
Date(AddMonths($(vMinYearMonth),IterNo() - 1),'YYYYMM') as TempDate
AutoGenerate 1
While AddMonths($(vMinYearMonth),IterNo()-1) <= $(vMaxYearMonth)
;

FOR i = 0 TO NoOfRows('TempCalendar') - 1
LET vYearMonth = peek('TempDate', $(i), 'TempCalendar');

Final:
Load *
FROM [lib://$(qvdPath)\abc_$(vYearMonth).qvd]
(qvd);

Next i;

Drop table TempCalendar;

Regards,
Prashant Sangle

@PrashantSangle wrote:
Hi,

Try below logic

Let vMaxYearMonth=num(Today());
Let vMinYearMonth=num(AddYears(Today(),-3));

TempCalendar:
LOAD
Date(AddMonths($(vMinYearMonth),IterNo() - 1),'YYYYMM') as TempDate
AutoGenerate 1
While AddMonths($(vMinYearMonth),IterNo()-1) <= $(vMaxYearMonth)
;

FOR i = 0 TO NoOfRows('TempCalendar') - 1
LET vYearMonth = peek('TempDate', $(i), 'TempCalendar');

Final:
Load *
FROM [lib://$(qvdPath)\abc_$(vYearMonth).qvd]
(qvd);

Next i;

Drop table TempCalendar;

Regards,
Prashant Sangle

Thank you Prashanth it works

SatyaPaleti
Creator III
Creator III
Author

Thank you Jhonty. It works well