Skip to main content
Announcements
New: No-code data prep in Qlik Cloud Analytics™ TAKE A TOUR
cancel
Showing results for 
Search instead for 
Did you mean: 
tinkerz1
Creator II
Creator II

Joining Aggr and Rank to Count Distinct

Hi,

I am trying to find a formula that can rank a list of countries then perform a distinct count on a field for each date

My table is as follows:

1. How can the formula =Aggr(Rank(Country),Country) be poplulate to the bottom of the page?

2. How after ranking by country and date can a count distinct on ID be performed?

Thanks.

   

DateCountry=if(Aggr(Rank(Country),Country,date)=2,Country)=Aggr(Rank(Country),Country)=count(distinct ID)
--36
01/07/2015Brazil-91
01/07/2015Canada-81
01/07/2015China-71
01/07/2015France-61
01/07/2015Germany-51
01/07/2015Japan-41
01/07/2015South Korea-31
01/07/2015UK-21
01/07/2015USA-11
01/08/2015Brazil--1
01/08/2015Canada--1
01/08/2015China--1
01/08/2015France--1
01/08/2015Germany--1
01/08/2015Japan--1
01/08/2015South Korea--1
01/08/2015UK--1
01/08/2015USA--1

   

1 Solution

Accepted Solutions
sunny_talwar

What about this?

=Aggr(if(Rank(Country)=2,count(distinct ID)), Date, Country)

View solution in original post

8 Replies
sunny_talwar

Rank them based on what? Population? GDP?

tinkerz1
Creator II
Creator II
Author

I would like the below formula in a chart as an expression with DATE as the dimension

=if(Aggr(Rank(Country),Country)=2,Country)

This would then find the 2nd country in the list, from that information I want to find the count of distinct ID's for that country by date.

If I have this information I could populate a chart with many expressions look at different ranks.

This would get round the problem of not being able to have a stacked bar chart and a line on the chart.

Thanks,

ie many expressions setup as a stacked bar chart

=if(Aggr(Rank(Country),Country)=1,Country)

=if(Aggr(Rank(Country),Country)=2,Country)

=if(Aggr(Rank(Country),Country)=3,Country)

=if(Aggr(Rank(Country),Country)=4,Country)

=sum([budget])

tinkerz1
Creator II
Creator II
Author

it was as simple as

=if(num(Rank(Country))=2,count(distinct ID))

sunny_talwar

So you got it to work now?

tinkerz1
Creator II
Creator II
Author

I am afraid not quite.

I have now added a chart to te QVW as a demo on what I am after.

What I am trying to do is make flexable expressions using RANK, that means I can add a line to a combo stacked bar chart.

//=if(Rank(Country)=2,count(distinct ID))

But this wont return a result without country in the dimension, so the charts fails to stack.

But this will work.

=count(distinct if (Country='USA', ID)) but I can't make the country part of the formula flexable.

If I had 5 countries but 20 expressions made up that would be enough.

sunny_talwar

What about this?

=Aggr(if(Rank(Country)=2,count(distinct ID)), Date, Country)

tinkerz1
Creator II
Creator II
Author

OK thanks,

Because you wrapped the =if(Rank(Country)=2,count(distinct ID)) formula up in AGGR.

Did it mean that Qlik could associate that table to the date dimension and Country? Where my formula just returned a number?

sunny_talwar

Basically since your Rank was working when there was Country available, we created a in memory table using aggregate function with Country as one of the dimensions. I am not sure if Date is required or not, but since Date was the actual dimension, I added that in. You can try and check if this work as well or not:

=Aggr(if(Rank(Country)=2,count(distinct ID)), Date, Country)