Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Qlik_dev03
Contributor II
Contributor II

How can I unpivot data and add year field pulling from a row?

I am dealing with data from an excel file that looks like Chart A but I want to load it to look like chart B (photos attached) Fields and Data is just an example and not the actual data I'm analyzing. I could get it to load but my big issue is trying to pull year from that row. Thanks in advance for the help. 

Qlik_dev03_0-1709911871748.png

Qlik_dev03_1-1709911901599.png

 

 

Labels (3)
6 Replies
JonnyPoole
Former Employee
Former Employee

Can you share a sample that shows multiple years? I can give you a sample, but unless I see how it looks with multiple years, it wouldn't work when you go to try it with multiple years

Qlik_dev03
Contributor II
Contributor II
Author

So this is the format of a report I will receive monthly. IT will get dropped into a folder to be loaded by qlik. Each file will have the year it applies to just as you see in the example I gave. In other words everything from this file is from 2021 and there are other files with their years located in the same location.

Hope that makes sense (pun intended) 😂

Anil_Babu_Samineni

@Qlik_dev03 You can try like this?

CrossTable(Metric, Score)
LOAD Location,
F1,
F2,
F3,
F4
FROM
[YourSource]
(ooxml, embedded labels, table is Sheet1) Where Not IsNull(Location);

CrossTable(Year, Score)
LOAD F1,
[2021],
[20211] as [2021],
[20212] as [2021],
[20213] as [2021]
FROM
[YourSource]
(ooxml, embedded labels, header is 1 lines, table is Sheet1);

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
Qlik_dev03
Contributor II
Contributor II
Author

@Anil_Babu_Samineni Very Close. When I use this script I loose Metric. I need Metric, Year and Score in the same table. 

JonnyPoole
Former Employee
Former Employee

This worked for me. And it handles a changing number of rows, columns (metrics), and years 

 

JonnyPoole_0-1709921220942.png

 

ExcelContents:
LOAD 
*
FROM [lib://Community Answers:DataFiles/2428811.xlsx]
(ooxml, embedded labels, table is Sheet1);
 
let vFirstMetricField=FIELDNAME('2','ExcelContents');
 
LET vYear=PEEK('$(vFirstMetricField)',0,'ExcelContents');
 
LET vNumFields=NOOFFIELDS('ExcelContents');
 
FOR field = 2 TO $(vNumFields)
 
LET vField=FIELDNAME('$(field)','ExcelContents');
 
Data:
LOAD
    Location,
        '$(vField)' AS Metric,
        [$(vField)] AS Score,
        $(vYear) AS Year
RESIDENT ExcelContents
    WHERE [$(vField)]<>$(vYear);
 
NEXT field
 
DROP TABLE ExcelContents

 

JonnyPoole_1-1709921269283.png

 

 

 

PhanThanhSon
Creator II
Creator II

Hi,

it was indeed a bit challenging to dynamically structure this task, especially with an unknown number of columns. You can find the QVF file as well as the Excel file attached. That should meet your requirements.

 

Let vPath = 'lib://Git-Test/script/';

Pivot:
First 2 LOAD
  *
FROM [$(vPath)Pivot.xlsx]
(ooxml, no labels, table is Tabelle1); // no labels is important

Let vConcatenateName = '';
FOR i = 1 to NoOfFields('Pivot')
  Let vFieldName = FieldName(i ,'Pivot');
  Let vConcatenateName$(i) = FieldValue('$(vFieldName)', 1) & '-' & FieldValue('$(vFieldName)', 2);
  Let vConcatenateName = '$(vConcatenateName)' & FieldValue('$(vFieldName)', 1) & '-' & FieldValue('$(vFieldName)', 2) & ',';
NEXT i

LET i =;
DROP TABLE Pivot;

Pivot:
Load * Inline [
$(vConcatenateName)
];

PivotTemp:
LOAD
    *
FROM [$(vPath)Pivot.xlsx]
(ooxml, embedded labels, header is 1 lines, table is Tabelle1); //header is 1 lines

Let vFieldNameFinal = '';
FOR i = 1 to NoOfFields('PivotTemp')
  Let vFieldName = FieldName(i ,'PivotTemp');
  Let vFieldNameFinal = '$(vFieldNameFinal)' &  '[' & '$(vFieldName)' &  ']'& ' AS '  & '[' &'$(vConcatenateName$(i))' & ']' & ',' ;
Let vConcatenateName$(i) =;
NEXT i
LET i =;

DROP TABLE PivotTemp;
Let vFieldNameFinal = LEFT('$(vFieldNameFinal)',len('$(vFieldNameFinal)')-1); //Eliminate last comma
Concatenate(Pivot)
LOAD
    $(vFieldNameFinal)
FROM [$(vPath)Pivot.xlsx]
(ooxml, embedded labels, header is 1 lines, table is Tabelle1);

FinalTableTemp:
Crosstable (MetricTemp, Score)
Load *
Resident Pivot;

Let vFieldNameFinal =;
Let vPath =;
Let vConcatenateName =;
Let vFieldName=;


NoConcatenate
FinalTable:
LOAD [Location-] AS Location,
Subfield(MetricTemp, '-', 1) AS Metric,
    Subfield(MetricTemp, '-', 2) AS Year,
    Score
Resident FinalTableTemp;

Drop table FinalTableTemp, Pivot;
Let vFieldNameFinal =;
Let vPath =;
Let vConcatenateName =;
Let vFieldName=;

PhanThanhSon_1-1709921454437.png

 

Best regards Son