Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Group by Dimension based on Max Sales

I have a scenario where a customer can exist in multiple territories.  I want to show a list of customers where only the territory with the most sales are shown, but still have the total sales, independent of territory.

Example of data (other fields exist, such as month):

CustomerTerrPrioritySales
Organization 11B2High100
Organization 21B2Medium100
Organization 21B3Medium200
Organization 21B4Medium300
Organization 32B1Low100
Organization 32B3Low200
Organization 42B3High100
Organization 52B4Medium100
Organization 62B4Low100
Organization 72B4High100
Organization 73B1High200

This is how I would like to show the data, preferably without changing the load script:

CustomerTerrPrioritySales
Organization 11B2High100
Organization 21B4Medium600
Organization 32B3Low300
Organization 42B3High100
Organization 52B4Medium100
Organization 62B4Low100
Organization 72B4High300

How can I build the Territory dimension to accomplish this?

Thanks for any help that you can provide!

Sam

1 Solution

Accepted Solutions
Not applicable
Author

Hi ,

I think that using distinct you can solve the problem:

FirstSortedValue(distinct Terr,-aggr(Sum(Sales),Customer,Terr))

The  function (FirstSortedValue) has a parameter for this case (n as order), but i've tried apply it unsuccessfully...

Regards

View solution in original post

6 Replies
Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

List Territory as an Expression, using the function FirstSortedValue, with the amount of Sales as the sorting parameter.

Not applicable
Author

Thanks for the quick response.

I just tried this and although the formula validates, I cannot get it to return the correct results.  Here is my formula:

=FirstSortedValue([Terr], (-aggr(sum( { < [Month]=, [Month Rank] = {1,2,3},  [Brand]= {'BRANDA'} >}  [Sales]), [Customer])))

Any idea what I am doing wrong?  My understanding is that adding the minus sign before the aggr function will change it to the last sorted value (Largest sales number).  I can't see any specific logic to how the Terr is selected.

Not applicable
Author

Hi,

Try this:

Dimension: Customer

Expression 1 (Terr):  FirstSortedValue(Terr,-aggr(Sum(Sales),Customer,Terr))

Expression 2 (Priority) : FirstSortedValue(Priority,-aggr(Sum(Sales),Customer,Terr))

Expression 3 (Sales): Sum(Sales)

Regards!

Not applicable
Author

Very helpful Francisco!

Last thing.  I have one example where the sales are the same for an account in each territory.  It returns null and I am fine with it showing either territory.  How can I have it populate with a territory?

Not applicable
Author

Hi ,

I think that using distinct you can solve the problem:

FirstSortedValue(distinct Terr,-aggr(Sum(Sales),Customer,Terr))

The  function (FirstSortedValue) has a parameter for this case (n as order), but i've tried apply it unsuccessfully...

Regards

Not applicable
Author

Perfect.  Thanks!!!