The total in a chart is not the sum of the individual rows of the chart.
Instead, the total and the subtotals are calculated using the expression – but on a larger subset of the data than for the individual row.
Usually, the two methods result in the same numbers, but sometimes there is a huge difference. One example of this is if you use a non-linear function, e.g. Count(distinct …) as expression. The example below clearly shows this.
The source data to the left assigns a country to each state, and if you count the number of countries per state using a Count(distinct Country), you will get the chart to the right: Each state belongs to one country only, and the total number of countries is 2, also if the chart has four rows.
A second example is if you have a many-to-many relationship in the data. In the example below, you have three products, each with a sales amount. But since each product can belong to several product groups, the sales amounts per product group will not add up: The total will be smaller than the sum of the individual rows, since there is an overlap between the product groups. The summation will be made in the fact table.
Another way to describe it would be to say that a specific dollar belongs to both product groups, and would be counted twice if you just summed the rows.
In both cases, QlikView will show the correct number, given the data. To sum the rows would be incorrect.
So, how does this affect you as an application developer?
Normally not very much. But it is good to be aware of it, and I would suggest the following:
When you write your expression, you should have the total line in mind. Usually, the expression will automatically be right also for the individual rows.
Always use an aggregation function. This will ensure that QlikView is able to calculate the total correctly.
If you want an average on the total line, you should most likely divide your expression with Count(distinct <Dim>). Then it will work both for the individual rows (where the count is 1) and the total lines. Example
Sum( Amount ) / Count( distinctCustomer )
For cases where you want to show something completely different in the total line, you should consider the Dimensionality() function, that returns 0, 1, 2, … depending on whether the evaluation takes place in a total, subtotal or row. Example:
If( Dimensionality() = 0, <Total line expression>, <Individual line expression> )
But If I want to show the sum of the individual rows? I don’t want the expression to be calculated over a larger data set. What do I do then?
There are two ways to do this. First, you can use an Aggr() function as expression:
I am stuck with the same Issue where i have Dimension and the count of Customers for those Corresponding Dimension.
I have one single dimension and I am using the expression as if(Sum(Sales)>0,Count(Distinct Customer)) this gives me the correct data as per the line items i.e (386) but the total comes to (457), I am really confused and would really appreciate if you could help me out with this.
I am looking for an aggregate expression to use to sort values of the sub dimensions of a pivot table in Qlik Sense. Find attached what I get when i try to sort by the plain expression. Somehow Qlik Sense does not work with subtotal and it sort the second dimension by its total not by the sub subtotal for each value of dimension 1.
I have an issue with a pareto chart that uses a dimension that is like a sub category to a category. The sub category exists in another table that does not contain key. The sum Total does not work correctly for all selections. So I tried using below expression (read from your post)
Sum(Aggr( Sum(Qty), [sub category]))
When I display in text object it shows correctly but cumulative % on the chart for some reason does not sum up to 100% for all selections.
You write that the chart has "a dimension that is like a sub category to a category". Does this mean that the chart has two dimensions?
What do you mean by "The sub category exists in another table that does not contain key"? Is the sub category not linked to Qty at all? If it isn't, then it is not possible to make a Pareto chart.
You write that "sum total does nto work correctly for all selections". In what way is it wrong?
Totals are not necessarily the sum of the rows. Instead, it is the same calculation made over all data records, not just a subset of records. See for instance the example in the post above, where product can belong to two different product groups, so the total is smaller than the sum of the individual product groups. As it should be.
I have a main fact table "Complaints" that has the qty measure. Within the fact table there is a "Reason" field (which will be used as a dimension on some charts). There is also a "Reason Detail" which exists in another table linked through an id field. The "Reason Detail" can have multiple rows for the same Complaint and Part Number.
The pareto chart by "Reason" works just fine but the pareto by "Reason Detail" does not work correctly (Sum Total did not work and I also tried sum(aggr(sum()) but nothing worked and I figured it is because of the one-to-many relationship between the fact and this dimension. So now I created another Fact (similar to the "Complaints" one using inner join with the Detail dimension and I think now things are working correctly.