<?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 How to get percentages of value pairs in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentages-of-value-pairs/m-p/367011#M165282</link>
    <description>&lt;P&gt;I have a very large set of retail data. The significant fields for this query are store_no, transaction_amt, zip, ethnicity. &lt;/P&gt;

&lt;P&gt;I am attempting to get a report for  a single store the percentage of sales amounts by ethnicity for each zip code. &lt;/P&gt;

&lt;P&gt;So what I am aiming for is a result set like this: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;                        46208      46219       46222
"A: African American"   .25         .23         .40
"B: Asian"              .06         .10         .14
"C: Hispanic"           .21         .22         .35
"D: White"              .48         .45         .11
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So far I have been able to get the total sales for each ethnicity at a store, but I don't know how to report out on the zip code values. &lt;/P&gt;

&lt;P&gt;Here is what I have so far:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="hhg-butler-data.csv" host="jw0mt32" sourcetype="hhg"  
| where (store_no = 243) 
| search ethnicity!="E: Unknown"
| stats 
  sum(transaction_amt) as TotalSales 
  sum(eval(if(ethnicity="A: African American", transaction_amt, null()))) as TotalAfricanAmericanSales
  sum(eval(if(ethnicity="B: Asian", transaction_amt, null()))) as TotalAsianSales
  sum(eval(if(ethnicity="C: Hispanic", transaction_amt, null()))) as TotalHispanicSales
  sum(eval(if(ethnicity="D: White", transaction_amt, null()))) as TotalWhiteSales  
  count as TotalCount
  dc(zip) as dcZip
  by store_no

| eval PercentAfricanAmericanSales=round(TotalAfricanAmericanSales/TotalSales, 6) * 100
| eval PercentAsianSales=round(TotalAsianSales/TotalSales, 6) * 100 
| eval PercentHispanicSales=round(TotalHispanicSales/TotalSales, 6) * 100 
| eval PercentWhiteSales=round(TotalWhiteSales/TotalSales, 6) * 100

| table TotalSales, TotalAfricanAmericanSales, PercentAfricanAmericanSales, TotalAsianSales, PercentAsianSales, TotalHispanicSales, PercentHispanicSales, TotalWhiteSales, PercentWhiteSales, TotalCount, dcZip
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thank you and please let me know if I can clarify what I am asking. &lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 16:46:00 GMT</pubDate>
    <dc:creator>behudelson</dc:creator>
    <dc:date>2020-09-29T16:46:00Z</dc:date>
    <item>
      <title>How to get percentages of value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentages-of-value-pairs/m-p/367011#M165282</link>
      <description>&lt;P&gt;I have a very large set of retail data. The significant fields for this query are store_no, transaction_amt, zip, ethnicity. &lt;/P&gt;

&lt;P&gt;I am attempting to get a report for  a single store the percentage of sales amounts by ethnicity for each zip code. &lt;/P&gt;

&lt;P&gt;So what I am aiming for is a result set like this: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;                        46208      46219       46222
"A: African American"   .25         .23         .40
"B: Asian"              .06         .10         .14
"C: Hispanic"           .21         .22         .35
"D: White"              .48         .45         .11
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So far I have been able to get the total sales for each ethnicity at a store, but I don't know how to report out on the zip code values. &lt;/P&gt;

&lt;P&gt;Here is what I have so far:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="hhg-butler-data.csv" host="jw0mt32" sourcetype="hhg"  
| where (store_no = 243) 
| search ethnicity!="E: Unknown"
| stats 
  sum(transaction_amt) as TotalSales 
  sum(eval(if(ethnicity="A: African American", transaction_amt, null()))) as TotalAfricanAmericanSales
  sum(eval(if(ethnicity="B: Asian", transaction_amt, null()))) as TotalAsianSales
  sum(eval(if(ethnicity="C: Hispanic", transaction_amt, null()))) as TotalHispanicSales
  sum(eval(if(ethnicity="D: White", transaction_amt, null()))) as TotalWhiteSales  
  count as TotalCount
  dc(zip) as dcZip
  by store_no

| eval PercentAfricanAmericanSales=round(TotalAfricanAmericanSales/TotalSales, 6) * 100
| eval PercentAsianSales=round(TotalAsianSales/TotalSales, 6) * 100 
| eval PercentHispanicSales=round(TotalHispanicSales/TotalSales, 6) * 100 
| eval PercentWhiteSales=round(TotalWhiteSales/TotalSales, 6) * 100

| table TotalSales, TotalAfricanAmericanSales, PercentAfricanAmericanSales, TotalAsianSales, PercentAsianSales, TotalHispanicSales, PercentHispanicSales, TotalWhiteSales, PercentWhiteSales, TotalCount, dcZip
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thank you and please let me know if I can clarify what I am asking. &lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 16:46:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentages-of-value-pairs/m-p/367011#M165282</guid>
      <dc:creator>behudelson</dc:creator>
      <dc:date>2020-09-29T16:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to get percentages of value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentages-of-value-pairs/m-p/367012#M165283</link>
      <description>&lt;P&gt;@behudelson, &lt;/P&gt;

&lt;P&gt;1) Your first should where and search should be merged in the base search for filtering required results upfront.&lt;BR /&gt;
2) If your final output requires transaction amount by zip and ethnicity, store number is not required for stats.&lt;/P&gt;

&lt;P&gt;Since we need statistics by zip code and then output by ethnicity, I have used &lt;CODE&gt;eventstats&lt;/CODE&gt; and &lt;CODE&gt;foreach&lt;/CODE&gt; to calculate percent by zip and then did a &lt;CODE&gt;transpose&lt;/CODE&gt; to reverse axis in the end.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; source="hhg-butler-data.csv" host="jw0mt32" sourcetype="hhg" store_no="243" ethnicity!="E: Unknown"
|  stats sum(transaction_amt) as transaction_amt by zip ethnicity
|  eventstats sum(transaction_amt) as Total by zip
|  chart values(Total) as Total sum(transaction_amt) as transaction_amt by zip ethnicity
|  rename "Total: A: African American" as Total
|  fields - "Total: *"
|  foreach "transaction_amt: *" 
    [eval "perc: &amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;"=round(('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'/Total)*100,1)]
|  fields zip perc*
|  rename "perc: *" as *
|  transpose header_field="zip" column_name="ethnicity"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Following is a run anywhere search to test out (makeresults with append command mock data as per the question):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|  makeresults
|  eval ethnicity="A: African American", zip=46208, transaction_amt=2000
|  append 
    [|  makeresults
|  eval ethnicity="A: African American", zip=46219, transaction_amt=1500]
|  append 
    [|  makeresults
|  eval ethnicity="A: African American", zip=46222, transaction_amt=2500]
|  append 
    [|  makeresults
|  eval ethnicity="B: Asian", zip=46208, transaction_amt=1100]
|  append 
    [|  makeresults
|  eval ethnicity="B: Asian", zip=46219, transaction_amt=1300]
|  append 
    [|  makeresults
|  eval ethnicity="B: Asian", zip=46222, transaction_amt=2500]
|  append 
    [|  makeresults
|  eval ethnicity="C: Hispanic", zip=46208, transaction_amt=1200]
|  append 
    [|  makeresults
|  eval ethnicity="C: Hispanic", zip=46219, transaction_amt=2000]
|  append 
    [|  makeresults
|  eval ethnicity="C: Hispanic", zip=46222, transaction_amt=2300]
|  append 
    [|  makeresults
|  eval ethnicity="D: White", zip=46208, transaction_amt=1800]
|  append 
    [|  makeresults
|  eval ethnicity="D: White", zip=46219, transaction_amt=1570]
|  append 
    [|  makeresults
|  eval ethnicity="D: White", zip=46222, transaction_amt=1350]
|  stats sum(transaction_amt) as transaction_amt by zip ethnicity
|  eventstats sum(transaction_amt) as Total by zip
|  chart values(Total) as Total sum(transaction_amt) as transaction_amt by zip ethnicity
|  rename "Total: A: African American" as Total
|  fields - "Total: *"
|  foreach "transaction_amt: *" 
    [eval "perc: &amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;"=round(('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'/Total)*100,1)]
|  fields zip perc*
|  rename "perc: *" as *
|  transpose header_field="zip" column_name="ethnicity"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Nov 2017 05:14:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentages-of-value-pairs/m-p/367012#M165283</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-11-12T05:14:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to get percentages of value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentages-of-value-pairs/m-p/367013#M165284</link>
      <description>&lt;P&gt;Hi @niketnilay, I will study this and follow up if I have questions. Thank you for such a quick and detailed response - very cool!&lt;/P&gt;</description>
      <pubDate>Sun, 12 Nov 2017 23:05:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentages-of-value-pairs/m-p/367013#M165284</guid>
      <dc:creator>behudelson</dc:creator>
      <dc:date>2017-11-12T23:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to get percentages of value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentages-of-value-pairs/m-p/367014#M165285</link>
      <description>&lt;P&gt;@behudelson, second search is a run anywhere search. From the &lt;CODE&gt;| stats&lt;/CODE&gt; command onward you can try out one command at a time to see and understand how each one of them behave. &lt;/P&gt;

&lt;P&gt;You should also refer to Splunk Docs for each command used for reference. For example, &lt;CODE&gt;transpose&lt;/CODE&gt; command has a default limit of 5. You will definitely have more zip codes than that, so you may have to use transpose with required limit.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; |  transpose 5 header_field="zip" column_name="ethnicity"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Transpose"&gt;http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Transpose&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Do let us know if you have any questions and accept the Answer once your issue is resolved &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 02:12:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-percentages-of-value-pairs/m-p/367014#M165285</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-11-13T02:12:38Z</dc:date>
    </item>
  </channel>
</rss>

