You're reading from Qlik Sense Cookbook. - Second Edition
We know how to use several aggregation functions such as sum
, max
, min
, and avg
. When used in a measure of a chart or table, they aggregate values over the dimension stated in that chart or table.
Sometimes, you need to create a second aggregation based on the result of the first. An example could be the value and the name of the country with the most customers. This can be done using Aggr
, combined with the Max()
and Firstsortedvalue()
functions.
Create a new Qlik Sense application, and load the following script that gives information on sales per country:
LOAD * INLINE [ Region, Country, No of Customers, Sales Americas, Uruguay, 200, 56000 Europe, France, 900, 252000 Americas, Brazil, 1500, 330000 Europe, Croatia, 500, 160000 Americas, Mexico, 1800, 122000 Americas, Argentina, 1200, 360000 Europe, Portugal, 1100, 302500 Europe, Spain, 800, 250000 Europe, Denmark, 1500, 435000 Europe, Belgium, 700, 154000 Asia, China, 2000, 370000 Asia, Japan...
The Rank()
function is likely used to calculate the relative position of the value on the dimension of the chart, starting from 1 to N.
As Rank()
requires a dimension in the object to calculate the relative position, how can we use it in a text object to show the name of the top five countries with more sales?
This recipe will show you how to use Rank()
with Aggr
to get this top five visualization in a dimensionless object.
For this recipe, we will reuse the data load for the Using nestedaggregations recipe of this chapter.
- Drag a
Text & image
object onto the content area. - Add the following text:
Top 5 Customers
, and type Enter to create a new line. - Format the text as bold.
- Go to the property editor and open the
Data
section. - Add a new measure with the following expression:
=concat( aggr( if (rank(sum(Sales)) <= 5, Country), Country) , ',', aggr(rank(sum(Sales)),Country) )
The result will look like the following screenshot...
This recipe will show you how to use set analysis with aggregation functions combined with Aggr()
.
Set analysis modifies the behavior of the filters in an aggregation expression. When using Aggr()
, you have two aggregations, one inside Aggr()
and the other outside Aggr()
, as in the following example:
= Max( Aggr(rank(Sum(Sales)), Country))
Where to insert the set analysis expression? Will the result be the same?
This recipe will show how the set analysis in the inner or the outer aggregation affects the result.
For this recipe, we will reuse the data load for the Using nested aggregation recipe from ealier in this chapter.
- Drag a
Table
object. - Add the text,
Europe Rank
, as the object title. - Add a dimension:
Country
. - Uncheck the
Include null values
property. - Add a new measure with the following expression, with the
Regional Rank
label:
= Max( Aggr(rank(Sum( {<Region={'Europe'}>} Sales)), Country))
- Add the second measure with the following...
ABC analysis divides a set of information, such as customers, products, suppliers, or any other dimension, into three categories: "A" for those contributing to the first 80% of an indicator, such as costs or sales, "B' for those contributing to the next 10%, and "C" for the final 10 %.
This recipe will show you how to create an ABC analysis coloring a dimension with this classification, and use the classification as a dimension, so you can filter only the countries that fall under the "B" category, for example.
For this recipe, we will reuse the data load for the Using nested aggregations recipe of this chapter.
- Create a new
Dimension
in theMaster items
library. - In the dimension editor, select the expression editor for the field.
- Add the following expression:
=Aggr( If(Rangesum(Above(Sum(Sales)/Sum(Total Sales),1,RowNo()))<0.8, 'A', If(Rangesum(Above(Sum(Sales)/Sum(Total Sales),1,RowNo()))<0.9, 'B', 'C')), (Country...