<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to do a count distinct by a certain value in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/How-to-do-a-count-distinct-by-a-certain-value/m-p/904754#M1004640</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Lindsay&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I have understood correctly I am thinking that you wish to be able to determine the number of distinct entries of values held in another field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are a few ways to achieve this, both in script or dynamically.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;By means of example let's say that you have the following data:&lt;/P&gt;&lt;P&gt;&lt;IMG class="jive-image image-1" src="https://community.qlik.com/legacyfs/online/91577_pastedImage_0.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;My assumption from your &lt;EM&gt;&lt;STRONG&gt;'count distinct syntax in a variable but it has to be counted by another field'&lt;/STRONG&gt;&lt;/EM&gt; is that you want to know how many distinct ID's there are per Item.&amp;nbsp; So for example in the data above we can see that there are two distinct ID's for Item 'One'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In the script:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;You can create the count of distinct ID's per item in the script using the following code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Load&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;&lt;BR /&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;Item&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;,&lt;BR /&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Count&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Distinct&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;countOfDistinctIDs&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;&lt;BR /&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Resident&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; SampleData&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Group&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;By&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;Item&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;&lt;BR /&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the above script the 'Group By' function is used to organise the data by the field 'Item' so that distinct ID's against each 'Item' can be counted.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will liberate a result set of:&lt;/P&gt;&lt;P&gt;&lt;IMG class="jive-image image-2" src="https://community.qlik.com/legacyfs/online/91578_pastedImage_6.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;The disadvantage if this method is that it is not dynamic and so it may not play well with any selections you make in your presentation application.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In the application:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;There are ways you can create this result dynamically.&amp;nbsp; For example, if you wish to replicate the result set created by the script above but in a dynamic format that will work with your selections then you can use a straight table&lt;/P&gt;&lt;P&gt;&lt;IMG class="jive-image image-3" src="https://community.qlik.com/legacyfs/online/91579_pastedImage_8.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;Here the dimension is Item and the expression is Count(Distinct ID)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Alternatively, let's say you wanted to only find out the distinct count of ID's where the Item is 'Two'.&amp;nbsp; We know in this example the answer should be 9.&amp;nbsp; You can use set analysis to make this kind of dynamic selection and can set the result as a variable if you wish for example to display the result in a text box.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The set analysis statement for this would be: &lt;SPAN style="font-size: 8pt;"&gt;=&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Count&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;({$&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;Item&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;={'Two'}&amp;gt;}&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Distinct&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;)&amp;nbsp; which is actually saying 'Count the number of distinct ID's where the Item is 'Two'.&amp;nbsp; If we &lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;placed the formula in a text box this would give the result 9 which we know to be correct.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you were to put this in the variable overview (on the settings menu) you could create it as a variable.&amp;nbsp; For example, if you created a new variable called vCountDistinctIDforItemTwo then placing the set expression above as the definition&lt;/P&gt;&lt;P&gt;of this variable would allow you to call it in a text box using &lt;SPAN style="font-size: 8pt;"&gt;=&lt;/SPAN&gt;&lt;STRONG&gt;&lt;EM style=": ; color: #808080; font-size: 8pt;"&gt;vCountOfDistinctIDforItemTwo.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You could even make it dynamic so you can choose which item you want to use e.g. if you create a variable called &lt;STRONG&gt;&lt;EM style=": ; color: #808080; font-size: 8pt;"&gt;vCountOfDistinctIDforItem&lt;/EM&gt;&lt;/STRONG&gt; and set its definition as the set expression &lt;SPAN style="font-size: 8pt;"&gt;=&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Count&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;({$&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;Item&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;={&lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;"$(=GetFieldSelections(Item))"&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;}&amp;gt;}&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Distinct&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;) &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;and use the &lt;SPAN style="font-size: 8pt;"&gt;=&lt;/SPAN&gt;&lt;STRONG&gt;&lt;EM style=": ; color: #808080; font-size: 8pt;"&gt;vCountOfDistinctIDforItem&lt;/EM&gt;&lt;/STRONG&gt; in a text box then this would update the text box with the count of distinct ID's for whichever single Item you select in a list box.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hopefully somewhere in this response I have managed to help you with your issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Steve&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 02 Jul 2015 01:29:49 GMT</pubDate>
    <dc:creator />
    <dc:date>2015-07-02T01:29:49Z</dc:date>
    <item>
      <title>How to do a count distinct by a certain value</title>
      <link>https://community.qlik.com/t5/QlikView/How-to-do-a-count-distinct-by-a-certain-value/m-p/904752#M1004638</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I need to setup count distinct syntax in a variable but it has to be counted by another field. Here is the syntax I have but it is not working. Any help is appreciated, thank you!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;count&lt;SPAN style="font-size: 8pt;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;distinct&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;) for AFD&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 May 2015 11:51:41 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/How-to-do-a-count-distinct-by-a-certain-value/m-p/904752#M1004638</guid>
      <dc:creator />
      <dc:date>2015-05-21T11:51:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to do a count distinct by a certain value</title>
      <link>https://community.qlik.com/t5/QlikView/How-to-do-a-count-distinct-by-a-certain-value/m-p/904753#M1004639</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;could you please explain clearly what you are trying to achieve?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Jul 2015 21:41:03 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/How-to-do-a-count-distinct-by-a-certain-value/m-p/904753#M1004639</guid>
      <dc:creator>Qrishna</dc:creator>
      <dc:date>2015-07-01T21:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to do a count distinct by a certain value</title>
      <link>https://community.qlik.com/t5/QlikView/How-to-do-a-count-distinct-by-a-certain-value/m-p/904754#M1004640</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Lindsay&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I have understood correctly I am thinking that you wish to be able to determine the number of distinct entries of values held in another field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are a few ways to achieve this, both in script or dynamically.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;By means of example let's say that you have the following data:&lt;/P&gt;&lt;P&gt;&lt;IMG class="jive-image image-1" src="https://community.qlik.com/legacyfs/online/91577_pastedImage_0.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;My assumption from your &lt;EM&gt;&lt;STRONG&gt;'count distinct syntax in a variable but it has to be counted by another field'&lt;/STRONG&gt;&lt;/EM&gt; is that you want to know how many distinct ID's there are per Item.&amp;nbsp; So for example in the data above we can see that there are two distinct ID's for Item 'One'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In the script:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;You can create the count of distinct ID's per item in the script using the following code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Load&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;&lt;BR /&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;Item&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;,&lt;BR /&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Count&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Distinct&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;countOfDistinctIDs&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;&lt;BR /&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Resident&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; SampleData&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Group&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;By&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;Item&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;&lt;BR /&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the above script the 'Group By' function is used to organise the data by the field 'Item' so that distinct ID's against each 'Item' can be counted.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will liberate a result set of:&lt;/P&gt;&lt;P&gt;&lt;IMG class="jive-image image-2" src="https://community.qlik.com/legacyfs/online/91578_pastedImage_6.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;The disadvantage if this method is that it is not dynamic and so it may not play well with any selections you make in your presentation application.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In the application:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;There are ways you can create this result dynamically.&amp;nbsp; For example, if you wish to replicate the result set created by the script above but in a dynamic format that will work with your selections then you can use a straight table&lt;/P&gt;&lt;P&gt;&lt;IMG class="jive-image image-3" src="https://community.qlik.com/legacyfs/online/91579_pastedImage_8.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;Here the dimension is Item and the expression is Count(Distinct ID)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Alternatively, let's say you wanted to only find out the distinct count of ID's where the Item is 'Two'.&amp;nbsp; We know in this example the answer should be 9.&amp;nbsp; You can use set analysis to make this kind of dynamic selection and can set the result as a variable if you wish for example to display the result in a text box.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The set analysis statement for this would be: &lt;SPAN style="font-size: 8pt;"&gt;=&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Count&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;({$&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;Item&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;={'Two'}&amp;gt;}&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Distinct&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;)&amp;nbsp; which is actually saying 'Count the number of distinct ID's where the Item is 'Two'.&amp;nbsp; If we &lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;placed the formula in a text box this would give the result 9 which we know to be correct.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you were to put this in the variable overview (on the settings menu) you could create it as a variable.&amp;nbsp; For example, if you created a new variable called vCountDistinctIDforItemTwo then placing the set expression above as the definition&lt;/P&gt;&lt;P&gt;of this variable would allow you to call it in a text box using &lt;SPAN style="font-size: 8pt;"&gt;=&lt;/SPAN&gt;&lt;STRONG&gt;&lt;EM style=": ; color: #808080; font-size: 8pt;"&gt;vCountOfDistinctIDforItemTwo.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You could even make it dynamic so you can choose which item you want to use e.g. if you create a variable called &lt;STRONG&gt;&lt;EM style=": ; color: #808080; font-size: 8pt;"&gt;vCountOfDistinctIDforItem&lt;/EM&gt;&lt;/STRONG&gt; and set its definition as the set expression &lt;SPAN style="font-size: 8pt;"&gt;=&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Count&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;({$&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;Item&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;={&lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;"$(=GetFieldSelections(Item))"&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;}&amp;gt;}&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; font-size: 8pt;"&gt;Distinct&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #800000; font-size: 8pt;"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="font-size: 8pt;"&gt;) &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;and use the &lt;SPAN style="font-size: 8pt;"&gt;=&lt;/SPAN&gt;&lt;STRONG&gt;&lt;EM style=": ; color: #808080; font-size: 8pt;"&gt;vCountOfDistinctIDforItem&lt;/EM&gt;&lt;/STRONG&gt; in a text box then this would update the text box with the count of distinct ID's for whichever single Item you select in a list box.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hopefully somewhere in this response I have managed to help you with your issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Steve&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Jul 2015 01:29:49 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/How-to-do-a-count-distinct-by-a-certain-value/m-p/904754#M1004640</guid>
      <dc:creator />
      <dc:date>2015-07-02T01:29:49Z</dc:date>
    </item>
  </channel>
</rss>

