Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us to spark ideas for how to put the latest capabilities into action. Register here!
cancel
Showing results for 
Search instead for 
Did you mean: 
Saurabh07
Contributor III
Contributor III

To create pivot structure with conditional measures

Hi,

I have a set of mentioned data;

RegionCountryClassSegmentClass 1 ValueClass 3 ValueClass 2 ValueClass 4 Value
AsiaIndiaClass 1Seg A10  50
AsiaIndiaClass 1Seg B30 90 
AsiaIndiaClass 1Seg C 40  
AsiaJapanClass 2Seg A 1520 
AsiaJapanClass 2Seg B10   
AsiaJapanClass 2Seg C 606015
AsiaAustraliaClass 3Seg A 70  
AsiaAustraliaClass 3Seg B30409080
AsiaAustraliaClass 3Seg C5040  
EuropeGermanyClass 4Seg A20  10
EuropeGermanyClass 4Seg B 7060 
EuropeGermanyClass 4Seg C90804020

 

My requirement here is to have a pivoted structure with the segments at column level. But the real challenge is to get the sum of values based on the conditions. Basically, if a country belongs to Class 1, I want the values to be picked from 'Class 1 Value' field, if a country belongs to Class 2, the values should be picked from 'Class 2 Value' field and so on. Similarly the values for each segments should be picked from respective Class value columns. 

Below is the desired table I want in Qlik Sense for better clarity of my requirement;

RegionCountryClassTotal ValueSeg ASeg BSeg C
AsiaIndiaClass 14010300
AsiaJapanClass 28020060
AsiaAustraliaClass 3150704040
EuropeGermanyClass 43010020

 

I tried a lot, but with no luck. Any help here would be highly appreciated!!

1 Solution

Accepted Solutions
Kushal_Chawda

@Saurabh07  try below

Data:
CrossTable(ClassName,ClassValue,4)
LOAD
    Region,
    Country,
    "Class",
    Segment,
    "Class 1 Value",
    "Class 3 Value",
    "Class 2 Value",
    "Class 4 Value"
FROM [lib://Web]
(html, utf8, embedded labels, table is @1);
​
Final:
NoConcatenate
Load *,
    if(WildMatch(trim(Lower(ClassName)),'*'& lower(trim("Class"))&'*'),1,0) as Class_Match
Resident Data;
​
Drop Table Data;
​

 

then you can create the pivot table with below expression

Sum({<Class_Match={1}>}ClassValue)

View solution in original post

6 Replies
Kushal_Chawda

@Saurabh07  do you want it in script or frond end?

Saurabh07
Contributor III
Contributor III
Author

@Kushal_Chawda The desired table is required in the front end in form of a Pivot or data Table. The challenge I am facing is to model the data to get this view, which I believe would be done in script editor! Correct me here if I am wrong!

Kushal_Chawda

@Saurabh07  try below

Data:
CrossTable(ClassName,ClassValue,4)
LOAD
    Region,
    Country,
    "Class",
    Segment,
    "Class 1 Value",
    "Class 3 Value",
    "Class 2 Value",
    "Class 4 Value"
FROM [lib://Web]
(html, utf8, embedded labels, table is @1);
​
Final:
NoConcatenate
Load *,
    if(WildMatch(trim(Lower(ClassName)),'*'& lower(trim("Class"))&'*'),1,0) as Class_Match
Resident Data;
​
Drop Table Data;
​

 

then you can create the pivot table with below expression

Sum({<Class_Match={1}>}ClassValue)
Saurabh07
Contributor III
Contributor III
Author

@Kushal_Chawda Thanks man for the solution, it worked! But there is only one small addition which I mentioned earlier. I also need the aggregated value for each country as 'Total Value'. How do I get that ? Do you suggest if I aggregate it in the backend and convert it to text, hence I can add it as a dimension in my Pivot table? This way Ill have only one measure splitted on the Segment and the Total Value will remain unaffected!

Kushal_Chawda

@Saurabh07  for total you can simply enable total option for the dimension segment.

Saurabh07
Contributor III
Contributor III
Author

@Kushal_Chawda  Yup it worked! Thank you for this. It helped a lot, this was most important part of my report! Appreciate it!