<?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 get percentage of values for a field based on total number from different search in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466132#M131265</link>
    <description>&lt;P&gt;@somesoni2 I've updated the question with a search and subsearch I created.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Aug 2019 18:06:28 GMT</pubDate>
    <dc:creator>fullstackdev</dc:creator>
    <dc:date>2019-08-29T18:06:28Z</dc:date>
    <item>
      <title>How to get percentage of values for a field based on total number from different search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466130#M131263</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have been pulling my hair to get this to work, but couldn't, and any help would be very much appreciated.&lt;/P&gt;

&lt;P&gt;I have a set of events created for when &lt;CODE&gt;tickets&lt;/CODE&gt; are created. One of field is &lt;CODE&gt;created&lt;/CODE&gt; time like this: &lt;CODE&gt;2019-08-26T18:20:08.930Z&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I have another set of events created for when some type of query is made for &lt;CODE&gt;ticket&lt;/CODE&gt;, and it includes time when the ticket was originally created.&lt;/P&gt;

&lt;P&gt;I would like to create a table of percentage of type of queries made from total number of orders created on the date.&lt;/P&gt;

&lt;P&gt;For example, ticket events are like the following:&lt;BR /&gt;
&lt;CODE&gt;{"event":"ticket_created","ticket_id": "id_1", "created": "2019-08-26T18:20:08.930Z"},&lt;BR /&gt;
{"event":"ticket_created","ticket_id": "id_2", "created": "2019-08-26T18:20:08.930Z"},&lt;BR /&gt;
{"event":"ticket_created","ticket_id": "id_3", "created": "2019-08-26T18:20:08.930Z"},&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;And query events would be like this: &lt;BR /&gt;
&lt;CODE&gt;{"event":"query","query_type":"type1","ticket_id": "id_1", "ticket_created": "2019-08-26T18:20:08.930Z"},&lt;BR /&gt;
{"event":"query","query_type":"type2","ticket_id": "id_2", "ticket_created": "2019-08-26T18:20:08.930Z"},&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;And table I am trying to create (from which visualization can be created):&lt;BR /&gt;
&lt;CODE&gt;Date             type1                                         type2&lt;/CODE&gt;&lt;BR /&gt;
&lt;CODE&gt;2019-08-26   33% (1 out of 3 tickets)           33% (1 out of 3 tickets)&lt;/CODE&gt;&lt;BR /&gt;
&lt;CODE&gt;2019-08-27   N%                                             M%&lt;/CODE&gt;&lt;BR /&gt;
&lt;CODE&gt;2019-08-28   I%                                               J%&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;So, far I was only able to generate just total numbers (query types by converted date appended with total ticket count by converted date). I can't seem to figure out how to dynamically divide sum of types divided by total number of tickets grouped by converted date. &lt;/P&gt;

&lt;P&gt;The following is the query I did, and it generates a table like the following:&lt;BR /&gt;
&lt;CODE&gt;sourcetype="sourcetype" event="query"&lt;BR /&gt;
| eval ticketCreated=strptime(created_at, "%Y-%m-%dT%H:%M:%S.%QZ")&lt;BR /&gt;
| eval ticketCreatedDate=strftime(ticketCreated, "%Y-%m-%d")&lt;BR /&gt;
| chart count by ticketCreatedDate, query_type &lt;BR /&gt;
| appendcols [search sourcetype="sourcetype" event="ticket_created"&lt;BR /&gt;
  | eval ticketCreated=strptime(ticket_created, "%Y-%m-%dT%H:%M:%S.%QZ")&lt;BR /&gt;
  | eval ticketCreatedDate=strftime(ticketCreated, "%Y-%m-%d")&lt;BR /&gt;
  | stats count as ticketCount by ticketCreatedDate]&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;Date                  type1                type2           ticketCount&lt;/CODE&gt;&lt;BR /&gt;
&lt;CODE&gt;2019-08-26      1                        1                    3&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Any help would be much, much appreciated. &lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 16:54:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466130#M131263</guid>
      <dc:creator>fullstackdev</dc:creator>
      <dc:date>2019-08-29T16:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to get percentage of values for a field based on total number from different search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466131#M131264</link>
      <description>&lt;P&gt;Can you share the query using which you were able to generate total numbers?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 17:51:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466131#M131264</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2019-08-29T17:51:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to get percentage of values for a field based on total number from different search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466132#M131265</link>
      <description>&lt;P&gt;@somesoni2 I've updated the question with a search and subsearch I created.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:06:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466132#M131265</guid>
      <dc:creator>fullstackdev</dc:creator>
      <dc:date>2019-08-29T18:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to get percentage of values for a field based on total number from different search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466133#M131266</link>
      <description>&lt;P&gt;What you need is a foreach command.&lt;/P&gt;

&lt;P&gt;Try something like this (with little modifications to optimize the search&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="sourcetype" event="query" 
| eval ticketCreatedDate=replace(created_at, "*.+)T.+","\1")  
| chart count by ticketCreatedDate, query_type 
| appendcols [search sourcetype="sourcetype" event="ticket_created" 
| eval ticketCreatedDate=replace(ticket_created, "*.+)T.+","\1")   
| stats count as ticketCount by ticketCreatedDate]
| foreach * [| eval "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"=if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"="ticketCount" OR "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"="ticketCreatedDate" , '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;' , tostring(round('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'*100/ticketCount))."%") ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Updates to existing query: reduced two evals to one (date is already in the format you need to extract the data part from created date)&lt;/P&gt;

&lt;P&gt;Foreach command: Your search result before foreach will have fields ticketCount and ticketCreatedDate and one field for each of ticket type. So, foreach looks at each field, it does nothing if field name is ticketCount or ticketCreatedDate, but for all other fields, it calculates the percentage using ticketCount field.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:38:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466133#M131266</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2019-08-29T18:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to get percentage of values for a field based on total number from different search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466134#M131267</link>
      <description>&lt;P&gt;@somesoni2 You are a savior!  Thank you so much!&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:47:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentage-of-values-for-a-field-based-on-total/m-p/466134#M131267</guid>
      <dc:creator>fullstackdev</dc:creator>
      <dc:date>2019-08-29T18:47:22Z</dc:date>
    </item>
  </channel>
</rss>

