<?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: ratio calculation returns wrong values in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659782#M227784</link>
    <description>&lt;P&gt;It depends on what your goal is - just presenting us with a bunch of SPL without a clear definition of what it is intended to do and without some sample events (anonymised of course) makes it challenging for volunteers to spend their time trying to figure out how to help you.&lt;/P&gt;</description>
    <pubDate>Thu, 05 Oct 2023 12:51:25 GMT</pubDate>
    <dc:creator>ITWhisperer</dc:creator>
    <dc:date>2023-10-05T12:51:25Z</dc:date>
    <item>
      <title>Why is the ratio calculation of two fields returning wrong results?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659759#M227781</link>
      <description>&lt;P&gt;Hello&lt;BR /&gt;I'm trying to calculate ratio of two fields but im getting wrong results&lt;BR /&gt;if i'm calculating each one of them separately im getting right results but together something is wrong&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=clientlogs sourcetype=clientlogs Categories="*networkLog*" "Request.url"="*v3/auth*"  Request.url!=*twofactor*  "Request.actionUrl"!="*dev*" AND "Request.actionUrl"!="*staging*"
| eval UserAgent = case(match(UserAgent, ".*ios.*"), "iOS FE",match(UserAgent, ".*android.*"), "Android FE",1=1, "Web FE")
| dedup UserAgent, _time
| stats count as AttemptsFE by UserAgent _time
| appendcols
    [search index=clientlogs sourcetype=clientlogs Categories="*networkLog*" "Request.url"="*v3/auth*"  Request.url!=*twofactor*  "Request.actionUrl"!="*dev*" AND "Request.actionUrl"!="*staging*"
    "Request.status" IN (201, 207) NOT "Request.data.twoFactor.otp.expiresInMs"="*"
    | eval UserAgent = case(match(UserAgent, ".*ios.*"), "iOS FE",match(UserAgent, ".*android.*"), "Android FE",1=1, "Web FE")
    | dedup UserAgent, _time
    | streamstats count as SuccessFE by UserAgent _time]
    | eval SuccessRatioFE = round((SuccessFE/AttemptsFE)*100, 2)
    | eval SuccessRatioFE = (SuccessFE/AttemptsFE)*100

| timechart bins=100  avg(SuccessRatioFE) as SuccessRatioFE BY UserAgent&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 15:34:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659759#M227781</guid>
      <dc:creator>sarit_s</dc:creator>
      <dc:date>2023-10-05T15:34:39Z</dc:date>
    </item>
    <item>
      <title>Re: ratio calculation returns wrong values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659768#M227782</link>
      <description>&lt;P&gt;The appendcols command does not correlate the fields, so while the initial search will have been ordered by UserAgent (then _time), the subsearch (from the appendcols) will probably still be in (reverse) _time order. Not only that, because of the extra filter on the search, there may be fewer events returned by the appendcols subsearch&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 11:15:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659768#M227782</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2023-10-05T11:15:04Z</dc:date>
    </item>
    <item>
      <title>Re: ratio calculation returns wrong values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659771#M227783</link>
      <description>&lt;P&gt;ok, so what is your suggestion ? how can i achieve my goal ?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 12:05:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659771#M227783</guid>
      <dc:creator>sarit_s</dc:creator>
      <dc:date>2023-10-05T12:05:09Z</dc:date>
    </item>
    <item>
      <title>Re: ratio calculation returns wrong values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659782#M227784</link>
      <description>&lt;P&gt;It depends on what your goal is - just presenting us with a bunch of SPL without a clear definition of what it is intended to do and without some sample events (anonymised of course) makes it challenging for volunteers to spend their time trying to figure out how to help you.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 12:51:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659782#M227784</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2023-10-05T12:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: ratio calculation returns wrong values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659790#M227785</link>
      <description>&lt;P&gt;OK. Some housekeeping stuff first:&lt;/P&gt;&lt;P&gt;1) Don't Use Wildcards At Beginning Of Your Search Term! Never! (or at least until you fully understand why you shouldn't do that). It will make Splunk have to read every single event from the given timerange, which will make it sloooooow.&lt;/P&gt;&lt;P&gt;2) Inclusion is always better than inclusion, so Request.url!=*twofactor* not only have that dreaded wildcard at the beginning, but also you are doing exclusion which again needs to parse every single event (as if p.1 didn't force Splunk to do it anyway)&lt;/P&gt;&lt;P&gt;3) Both of your searches have&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;search index=clientlogs sourcetype=clientlogs Categories="*networkLog*" "Request.url"="*v3/auth*"  Request.url!=*twofactor*  "Request.actionUrl"!="*dev*" AND "Request.actionUrl"!="*staging*"&lt;/LI-CODE&gt;&lt;P&gt;As the "base search". There's no point in running this heavy (see p.1) search twice. Just run the search once and mark some events if needed.&lt;/P&gt;&lt;P&gt;4) I'm always very cautious about the dedup command. I find it very unintuitive and producing "strange" results - I prefer stats values() or similar stuff.&lt;/P&gt;&lt;P&gt;5) dedup by _time - what are you trying to achieve here? Especially that you don't use any binning?&lt;/P&gt;&lt;P&gt;Also, as &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt; already mentioned - appendcols just pastes a set of fields along given fields without any correlation between them. (not to mention subsearch limitations which might skew results even further).&lt;/P&gt;&lt;P&gt;So - what's the business case? Because that's surely better done otherwise - most probably with one search (even if we leave for now the thing about wildcards and exclusions).&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 13:15:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659790#M227785</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-10-05T13:15:57Z</dc:date>
    </item>
    <item>
      <title>Re: ratio calculation returns wrong values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659793#M227786</link>
      <description>&lt;P&gt;Hello&lt;BR /&gt;Thanks for you detailed answer&lt;BR /&gt;What im trying to achieve is to calculate the ration between the attempts and the success&lt;BR /&gt;Actually I've searched some details regarding the base search and how to use it in other parts of the query (also i want to use base search in other panel at the same dashboard) and i couldn't find any information&lt;BR /&gt;&lt;BR /&gt;I will really appreciate if you will be able to correct my query as im not familiar with different way to achieve the same.&lt;BR /&gt;&lt;BR /&gt;P.S - at the end, the final query should append the results from this query and results from similar query but for the BE part&lt;BR /&gt;so the final query will be larger and heavier&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 13:35:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659793#M227786</guid>
      <dc:creator>sarit_s</dc:creator>
      <dc:date>2023-10-05T13:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: ratio calculation returns wrong values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659796#M227787</link>
      <description>&lt;P&gt;No, it's not about "base search" functionality in dashboards. I'm not talking about that. I'm talking about the base search conceptually.&lt;/P&gt;&lt;P&gt;But conceptually I'd do something like that:&lt;/P&gt;&lt;PRE&gt;&amp;lt;your base search&amp;gt;&lt;BR /&gt;| eval success=if(your_criteria_for_success,1,0)&lt;BR /&gt;| stats count as total sum(success) as susccessed by UserAgent&lt;BR /&gt;| eval ratio=successed/total&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Oct 2023 13:45:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659796#M227787</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-10-05T13:45:01Z</dc:date>
    </item>
    <item>
      <title>Re: ratio calculation returns wrong values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659806#M227789</link>
      <description>&lt;P&gt;ok, got it, thanks&lt;/P&gt;&lt;P&gt;but how can i change it to timechart ?&lt;BR /&gt;because when im running this :&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=clientlogs sourcetype=clientlogs Categories="*networkLog*" "Request.url"="*v3/auth*"  Request.url!=*twofactor*  "Request.actionUrl"!="*dev*" AND "Request.actionUrl"!="*staging*"
| eval SuccessFE=if(('Request.status' IN (201, 207) ),1,0)

| eval UserAgent = case(match(UserAgent, ".*ios.*"), "iOS FE",match(UserAgent, ".*android.*"), "Android FE",1=1, "Web FE")
 | stats count as total sum(SuccessFE) as SuccessFE by UserAgent
|  eval SuccessRatioFE = (SuccessFE/total)*100
| chart bins=100 values(SuccessRatioFE) over _time by UserAgent&lt;/LI-CODE&gt;&lt;P&gt;I get no results while if im running a table i see results&lt;BR /&gt;also, i cannot add the NOT filter I need inside the if statement :&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;NOT "Request.data.twoFactor.otp.expiresInMs"="*"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 14:05:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659806#M227789</guid>
      <dc:creator>sarit_s</dc:creator>
      <dc:date>2023-10-05T14:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: ratio calculation returns wrong values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659812#M227791</link>
      <description>&lt;P&gt;No. If function has a different syntax. See &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/ConditionalFunctions#if.28.26lt.3Bpredicate.26gt.3B.2C.26lt.3Btrue_value.26gt.3B.2C.26lt.3Bfalse_value.26gt.3B.29" target="_blank"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/ConditionalFunctions#if.28.26lt.3Bpredicate.26gt.3B.2C.26lt.3Btrue_value.26gt.3B.2C.26lt.3Bfalse_value.26gt.3B.29&lt;/A&gt;&lt;/P&gt;&lt;P&gt;As for converting it to timechart, you can simply do&lt;/P&gt;&lt;PRE&gt;| timechart span=1h&amp;nbsp; count as total sum(SuccessFE) as SuccessFe by UserAgent&lt;/PRE&gt;&lt;P&gt;instead of stats&lt;/P&gt;&lt;P&gt;This will however give you a table which you'll have to firstly fillnull (because the sum might be empty) and then... hmm... probably use foreach to calculate ratio for each UserAgent separately. Or do untable and then stats. Probably the latter approach is easier.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 15:40:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-is-the-ratio-calculation-of-two-fields-returning-wrong/m-p/659812#M227791</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-10-05T15:40:36Z</dc:date>
    </item>
  </channel>
</rss>

