Skip to main content
Announcements
Accelerate Your Success: Fuel your data and AI journey with the right services, delivered by our experts. Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
Hemanth_Qlik
Contributor
Contributor

Multiple fields to single field selections and calculation

Hi All,

 

I have three columns in the table namely sales, From_country,To_Country. Now i have to display country data(single field) on x axis on bar graph and measures as From sales and To sales. Basically I have to club both country fields to single fields but should retain their relationship. Please help if already such solution exists. Also I cannot divide the table into two tables in the datamodel.

 

Thanks,

Hemanth

2 Solutions

Accepted Solutions
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You need to load From_Country and To_Country again as a common field Country and link back to the source row.  Then use Country as DImension.  I'm assuming you have a keyfield named "RecId". If you don't have a key you'll have to generate one using RecNo() when you load the data. 

Countries:
LOAD RecId, From_Country as Country Resident MyData;
Concatenate 
LOAD RecId, To_Country as Country Resident MyData;

-Rob

View solution in original post

Spivey
Partner - Contributor III
Partner - Contributor III

So you want to go from this:

KeyFrom_CountryTo_CountrySales
1USAKOR5
2USAITA2
3USAFIN3
4FINITA6
5FINKOR3
6KORUSA6

 

...to this:

KeyFlowCountryTo SalesFrom Sales
1From_CountryUSA 5
2From_CountryUSA 2
3From_CountryUSA 3
4From_CountryFIN 6
5From_CountryFIN 3
6From_CountryKOR 6
6To_CountryUSA6 
3To_CountryFIN3 
5To_CountryFIN3 
1To_CountryKOR5 
2To_CountryITA2 
4To_CountryITA6 

 

In order to accomplish this, you'll want to use the CrossTable() function in the load script, like so:

 

sales:
LOAD RowNo() as Key, * Inline [
From_Country, To_Country, Sales
USA, KOR, 5
USA, ITA, 2
USA, FIN, 3
FIN, ITA, 6
FIN, FIN, 3
KOR, USA, 6
];


sales_load:
CrossTable ([Flow], [Country], 2) LOAD
	Key
  , Sales
  , From_Country
  , To_Country
Resident sales;

Drop Table sales;

 

That inline table is just a placeholder for your table, though make sure you add a Key field as I have, perhaps using that RowNo() function.

Once you run this script, you'll have something like this:

table screenshot.png

 

From here, you can create your bar chart with Country as the dimension, Sum({<Flow={'From_Country'}>} Sales) as the From Sales measure and Sum({<Flow={'To_Country'}>} Sales) as the To Sales measure.

Final product:

bar chart.png

 

 

View solution in original post

4 Replies
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You need to load From_Country and To_Country again as a common field Country and link back to the source row.  Then use Country as DImension.  I'm assuming you have a keyfield named "RecId". If you don't have a key you'll have to generate one using RecNo() when you load the data. 

Countries:
LOAD RecId, From_Country as Country Resident MyData;
Concatenate 
LOAD RecId, To_Country as Country Resident MyData;

-Rob

Spivey
Partner - Contributor III
Partner - Contributor III

So you want to go from this:

KeyFrom_CountryTo_CountrySales
1USAKOR5
2USAITA2
3USAFIN3
4FINITA6
5FINKOR3
6KORUSA6

 

...to this:

KeyFlowCountryTo SalesFrom Sales
1From_CountryUSA 5
2From_CountryUSA 2
3From_CountryUSA 3
4From_CountryFIN 6
5From_CountryFIN 3
6From_CountryKOR 6
6To_CountryUSA6 
3To_CountryFIN3 
5To_CountryFIN3 
1To_CountryKOR5 
2To_CountryITA2 
4To_CountryITA6 

 

In order to accomplish this, you'll want to use the CrossTable() function in the load script, like so:

 

sales:
LOAD RowNo() as Key, * Inline [
From_Country, To_Country, Sales
USA, KOR, 5
USA, ITA, 2
USA, FIN, 3
FIN, ITA, 6
FIN, FIN, 3
KOR, USA, 6
];


sales_load:
CrossTable ([Flow], [Country], 2) LOAD
	Key
  , Sales
  , From_Country
  , To_Country
Resident sales;

Drop Table sales;

 

That inline table is just a placeholder for your table, though make sure you add a Key field as I have, perhaps using that RowNo() function.

Once you run this script, you'll have something like this:

table screenshot.png

 

From here, you can create your bar chart with Country as the dimension, Sum({<Flow={'From_Country'}>} Sales) as the From Sales measure and Sum({<Flow={'To_Country'}>} Sales) as the To Sales measure.

Final product:

bar chart.png

 

 

Hemanth_Qlik
Contributor
Contributor
Author

Hi Rwundelich,

 

Thanks for the help ! I have done the same to achieve the result. however I have also 'from' as direction, 'to' as direction in the link table. And to calculate respective sales of the country , used below expressions.

 

Dimension: Country

From sales: Sum({<direction = 'from'>}Sales)

To sales: Sum({<direction = 'to'>}Sales)

 

Regards,

Hemanth.

Hemanth_Qlik
Contributor
Contributor
Author

Hi Spicey, Thanks for the help !

 

However, I haven't used cross table to split the rows . I have created a link table with direction as additional column and have used your measures respectively.

 

Regards,

Hemanth