Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
SGood
Contributor III
Contributor III

Merging Year from column into X Axis with Months

Hi,

I am importing from an excel spreadsheet, into #QlikView.  Along with a number of other columns, the table contains the following structure, with the Allocation year in a separate column from the months:

BP IDAllocation YearJanFebMarAprMayJunJulAugSepOctNovDec
888220200000.850.850.850.850.850.850.850.850.85
100372021000000000000.4
1003720220.40.40.40.40.40.40.40.40.40.40.40.4
100542022111111110000
1060320200.150.150.150.150.150.150.150.150.150.150.150.15
1060320210.150.150.150.150.150.150.150.150.150.150.150.15
1060320220.150.150.150.150.150.150.150.150.150.150.150.15

 

However, I need the year merged in with the month columns, to output like this:

BP IDJan-20Feb-20Mar-20Apr-20May-20Jun-20Jul-20Aug-20Sep-20Oct-20Nov-20Dec-20Jan-21Feb-21Mar-21Apr-21May-21
88820000.850.850.850.850.850.850.850.850.850.850.850.850.850.85
10037000000000000.40.40.40.40.40.4
100370.40.40.40.40.40.40.40.40.40.40.40.40.40.40.40.40.4
1005411111111000012345
106030.150.150.150.150.150.150.150.150.150.150.150.150.150.150.150.150.15
106030.150.150.150.150.150.150.150.150.150.150.150.150.150.150.150.150.15
106030.150.150.150.150.150.150.150.150.150.150.150.150.150.150.150.150.15

 

Any suggestions on how to create script to load the data into this structure would be greatly appreciated.

Thank you.

Sally

 

 

 

1 Solution

Accepted Solutions
stevejoyce
Specialist II
Specialist II

I thought it could be done simpler, but this should work:

 

temp_data:
LOAD
"BP ID",
"Allocation Year",
Jan,
Feb,
Mar,
Apr,
May,
Jun,
Jul,
Aug,
Sep,
Oct,
Nov,
Dec
FROM https://community.qlik.com/t5/QlikView-App-Dev/Merging-Year-from-column-into-X-Axis-with-Months/td-p...
(html, utf8, embedded labels, table is @1);


final_data:
load "BP ID" as BP_ID
resident temp_data;


//loop through each month
for month_iter = 1 to 12

let vListOfTables = '';

let vMonthName = month($(month_iter) &'/'&'1'&'/'&'2000') ;

//do generic load for current month. This will create a table & column for each years value.
temp_generic:
generic load "BP ID", "Allocation Year", $(vMonthName)
resident temp_data;

//get list of tables created
for vTableNo = 0 to NoOfTables()
let vTableName = TableName($(vTableNo));

if subfield(vTableName, '.', 1) = 'temp_generic' then

Let vListOfTables = vListOfTables & If(Len(vListOfTables)>0,',') & Chr(39) & vTableName & Chr(39) ;

End If

Next vTableNo

//join list of tables created to final_data table and name field as Year - Month
For each vTableName in $(vListOfTables)

let vTempFieldName = subfield('$(vTableName)', '.', 2);
let vNewFieldName = subfield('$(vTableName)', '.', 2) & ' - ' & '$(vMonthName)';


Left Join (final_data)
Load
"BP ID" as BP_ID
,[$(vTempFieldName)] as [$(vNewFieldName)]
Resident [$(vTableName)];

//drop generic table
Drop Table [$(vTableName)];

//do next table (i.e. year)
Next vTableName

//do next month
next month_iter

drop table temp_data;


;

View solution in original post

11 Replies
stevejoyce
Specialist II
Specialist II

I thought it could be done simpler, but this should work:

 

temp_data:
LOAD
"BP ID",
"Allocation Year",
Jan,
Feb,
Mar,
Apr,
May,
Jun,
Jul,
Aug,
Sep,
Oct,
Nov,
Dec
FROM https://community.qlik.com/t5/QlikView-App-Dev/Merging-Year-from-column-into-X-Axis-with-Months/td-p...
(html, utf8, embedded labels, table is @1);


final_data:
load "BP ID" as BP_ID
resident temp_data;


//loop through each month
for month_iter = 1 to 12

let vListOfTables = '';

let vMonthName = month($(month_iter) &'/'&'1'&'/'&'2000') ;

//do generic load for current month. This will create a table & column for each years value.
temp_generic:
generic load "BP ID", "Allocation Year", $(vMonthName)
resident temp_data;

//get list of tables created
for vTableNo = 0 to NoOfTables()
let vTableName = TableName($(vTableNo));

if subfield(vTableName, '.', 1) = 'temp_generic' then

Let vListOfTables = vListOfTables & If(Len(vListOfTables)>0,',') & Chr(39) & vTableName & Chr(39) ;

End If

Next vTableNo

//join list of tables created to final_data table and name field as Year - Month
For each vTableName in $(vListOfTables)

let vTempFieldName = subfield('$(vTableName)', '.', 2);
let vNewFieldName = subfield('$(vTableName)', '.', 2) & ' - ' & '$(vMonthName)';


Left Join (final_data)
Load
"BP ID" as BP_ID
,[$(vTempFieldName)] as [$(vNewFieldName)]
Resident [$(vTableName)];

//drop generic table
Drop Table [$(vTableName)];

//do next table (i.e. year)
Next vTableName

//do next month
next month_iter

drop table temp_data;


;

MarcoWedel

Hi,

another solution might be:

 

 

table1:
CrossTable (Month, SomeFact, 2)
LOAD *
FROM [https://community.qlik.com/t5/QlikView-App-Dev/Merging-Year-from-column-into-X-Axis-with-Months/m-p/1831139] (html, codepage is 1252, embedded labels, table is @1);

table2:
LOAD [BP ID], 
     [Allocation Year],
     Month(Date#(Month,'MMM'))						as Month,
     Date#(Month&'-'&[Allocation Year],'MMM-YYYY')	as AllocationYearMonth,
     SomeFact
Resident table1;

DROP Table table1;

 

 

QlikCommunity_Thread_t5_QlikView-App-Dev_Merging-Year-from-column-into-X-Axis-with-Months_m-p_1831139_Pic1.PNG

hope this helps

regards

Marco

SGood
Contributor III
Contributor III
Author

Thanks, Steve, I've tried this, but not having much joy. 

First it was staying on Jan, then I realised it was because the date format was US and I am in the UK, so I had to swap the DD and MM in this line: let vMonthName = month($(month_iter) &'/'&'1'&'/'&'2000') .  

Having changed this when it gets to the second month, Feb 2020, it fails on the Left Join (final_date) line.  First it freezes, with the typical "Not responding"  message in the header, then it either crashes or I see the following script error (and I'm not cancelling anything). 

Action canceled by user
Left Join (final_data)
Load
"BP ID" as BP_ID
,[2020] as [2020 - Feb]
Resident [temp_generic.2020]

Thanks Sally.

 

SGood
Contributor III
Contributor III
Author

Thanks Marco.  

I have used this loading all the data from my excel file, which includes another 43 columns.  All the data loads fine, however, I have tried to add additional columns to the main "SomeFact" table, you had created.  The dimensions in your original table were simply  "BP ID" and "AllocationYearMonth".  I've added BP Name as an additional Dimension: 1st dim:"BP ID", 2nd dim: "BP Name", 3rd dim: "AllocationYearMonth", but the pivot treats the 2nd dim as a column heading rather than a row heading, and it needs to be a row heading.  Would you know if it's possible to fix this?

Sally  

stevejoyce
Specialist II
Specialist II

Ok, you should just need to format this part into whatever your DateFormat variable is: month('1/'&$(month_iter)&'/'&'2000') .

But I think I over complicated it anyway.  I thought you wanted your data model structured like your 2nd table so you had a seperate field for each month-year.  If that's just your front end design, then it would be better to unpivot in the data model and use a pivot table like @MarcoWedel was saying.

SGood
Contributor III
Contributor III
Author

Yes, you were correct, ideally I do want the data model structured like the second table.  🙂

I fixed the month format, as you described above.  However, following this amendment, it's failing when it starts to process February.

 

 

stevejoyce
Specialist II
Specialist II

Ok.  I'd try and take a look, looks good on my end :). 

Maybe a couple troubleshooting steps and if you can post the error screenshot.

1) can you make sure in a text box this formula month('1/'& '6' &'/'&'2000') returns June 1 2000?

2 )Can you add in a trace for some troubleshooting.

//loop through each month
for month_iter = 1 to 12

let vListOfTables = '';

let vMonthName = month('1/'&$(month_iter)&'/'&'2000') ;

TRACE vMonthName = $(vMonthName );

stevejoyce_0-1629996150005.png

 

SGood
Contributor III
Contributor III
Author

Hi Steve,

As suggested I created a text box and I entered the below formula.  As you can see, this works fine:

SGood_0-1629999971095.png

The edited code in the script looks like this:

SGood_1-1630000037021.png

When stepping through the code, it runs through '2020 - Jan', '2021 - Jan' and '2022 - Jan' no problem, but gets stuck on '2020 - Feb' at this point:

SGood_2-1630000123069.png

It eventually threw the following error:

Action canceled by user
Left Join (final_data)
Load
"BP ID" as BP_ID
,[2020] as [2020 - Feb]
Resident [temp_generic.2020]

I took a screenshot, but then my laptop simply shut itself down.  First time, it's done this at this point, typically I've been able to stop the script, or QV crashes.

I'm going to try running it again shortly, with all other apps switched off.

Thanks Sally.

 

stevejoyce
Specialist II
Specialist II

yea so vMonthName is returning null.  can you update your calculation to remove the leading 0 in your day parameter.

let vMonthName = month('1'& '/' & $(month_iter)  &'/'&'2000') ;  

 

If that doesnt work let me see if there is another way that can pull month name based on our iteration counter.

certainly this will work...

let vMonthName = pick($(month_iter) , 'Jan', 'Feb', 'Mar', 'Apr'...FINISH THE MONTHS);

 

or how about this, i dont think this should be impacted by different date formatting...

let vMonthName =month(makedate(2020, $(month_iter), 1));