<?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: Find nearest value in numeric multivalue field to other numeric field in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Find-nearest-value-in-numeric-multivalue-field-to-other-numeric/m-p/673572#M230644</link>
    <description>&lt;P&gt;Not sure if you still need this answered but figure I'd give it a shot in case anybody else has a similar problem they need to solve.&lt;BR /&gt;&lt;BR /&gt;I think something like this would do what you are looking for.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;base_search&amp;gt;
    ``` lookup historical values for each user (potentially mv field) ```
    | lookup &amp;lt;lookup_name&amp;gt; user OUTPUT Amount_Hist
    ``` loop through Amount_Hist values and finding the diff of each compared to the users current value, then take the minimum value and assign it to it's own field "Amount_Hist_min_diff" ```
    | eval
        Amount_Hist_min_diff=min(
            case(
                mvcount(Amount_Hist)==1, abs('Amount_Hist'-'Amount'),
                mvcount(Amount_Hist)&amp;gt;1, mvmap(Amount_Hist, abs('Amount_Hist'-'Amount'))
                )
            ),
        ``` loop back through historical values and only return the values whos diff is equal to the minimum diff value assigned previously ```
        Closest_Amount_Hist_value=mvdedup(
            case(
                mvcount(Amount_Hist)==1, if(abs('Amount_Hist'-'Amount')=='Amount_Hist_min_diff', 'Amount_Hist', null()),
                mvcount(Amount_Hist)&amp;gt;1, mvmap(Amount_Hist, if(abs('Amount_Hist'-'Amount')=='Amount_Hist_min_diff', 'Amount_Hist', null()))
                )
            )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;You can see that the field "Closest_Amount_Hist_value" holds the value closest the the user's current value, this can potentially hold multiple values if they are are equidistant from the current value. As show below.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dtburrows3_1-1704753142009.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/28796iEB33F4DF512A9449/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dtburrows3_1-1704753142009.png" alt="dtburrows3_1-1704753142009.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 08 Jan 2024 22:32:59 GMT</pubDate>
    <dc:creator>dtburrows3</dc:creator>
    <dc:date>2024-01-08T22:32:59Z</dc:date>
    <item>
      <title>Find nearest value in numeric multivalue field to other numeric field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Find-nearest-value-in-numeric-multivalue-field-to-other-numeric/m-p/399163#M115731</link>
      <description>&lt;P&gt;I have events with a numeric field "Amount" and a field "User". In a KV Store collection I keep the Amount history values for a each User  (Amount_Hist). With a lookup I can get the Amount_Hist for a user in a numeric multivalue field.&lt;/P&gt;

&lt;P&gt;Given a new event for a user and the the value of Amount, I need to get the nearest value from the Amount_Hist (where Amount_Hist is a multivalue field and Amount a single value field).&lt;/P&gt;

&lt;P&gt;I cant use mvexpand to do it because Amount_Hist is very large and mvexpand produce exesive memory usage when is applied for multiple events.&lt;/P&gt;

&lt;P&gt;Thanks a lot for any sugerence.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 00:45:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Find-nearest-value-in-numeric-multivalue-field-to-other-numeric/m-p/399163#M115731</guid>
      <dc:creator>sematag</dc:creator>
      <dc:date>2020-09-30T00:45:30Z</dc:date>
    </item>
    <item>
      <title>Re: Find nearest value in numeric multivalue field to other numeric field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Find-nearest-value-in-numeric-multivalue-field-to-other-numeric/m-p/673572#M230644</link>
      <description>&lt;P&gt;Not sure if you still need this answered but figure I'd give it a shot in case anybody else has a similar problem they need to solve.&lt;BR /&gt;&lt;BR /&gt;I think something like this would do what you are looking for.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;base_search&amp;gt;
    ``` lookup historical values for each user (potentially mv field) ```
    | lookup &amp;lt;lookup_name&amp;gt; user OUTPUT Amount_Hist
    ``` loop through Amount_Hist values and finding the diff of each compared to the users current value, then take the minimum value and assign it to it's own field "Amount_Hist_min_diff" ```
    | eval
        Amount_Hist_min_diff=min(
            case(
                mvcount(Amount_Hist)==1, abs('Amount_Hist'-'Amount'),
                mvcount(Amount_Hist)&amp;gt;1, mvmap(Amount_Hist, abs('Amount_Hist'-'Amount'))
                )
            ),
        ``` loop back through historical values and only return the values whos diff is equal to the minimum diff value assigned previously ```
        Closest_Amount_Hist_value=mvdedup(
            case(
                mvcount(Amount_Hist)==1, if(abs('Amount_Hist'-'Amount')=='Amount_Hist_min_diff', 'Amount_Hist', null()),
                mvcount(Amount_Hist)&amp;gt;1, mvmap(Amount_Hist, if(abs('Amount_Hist'-'Amount')=='Amount_Hist_min_diff', 'Amount_Hist', null()))
                )
            )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;You can see that the field "Closest_Amount_Hist_value" holds the value closest the the user's current value, this can potentially hold multiple values if they are are equidistant from the current value. As show below.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dtburrows3_1-1704753142009.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/28796iEB33F4DF512A9449/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dtburrows3_1-1704753142009.png" alt="dtburrows3_1-1704753142009.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2024 22:32:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Find-nearest-value-in-numeric-multivalue-field-to-other-numeric/m-p/673572#M230644</guid>
      <dc:creator>dtburrows3</dc:creator>
      <dc:date>2024-01-08T22:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: Find nearest value in numeric multivalue field to other numeric field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Find-nearest-value-in-numeric-multivalue-field-to-other-numeric/m-p/673573#M230645</link>
      <description>&lt;P&gt;An alternative which will just return a single 'closest' value using foreach is&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;your_search_and_lookup
| eval diff=max(Amount_Hist)
| foreach Amount_Hist mode=multivalue [ eval new_diff=min(diff, abs(Amount-&amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;)), closest=if(new_diff&amp;lt;diff, &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;, closest), diff=new_diff ]
| fields - new_diff&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2024 23:15:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Find-nearest-value-in-numeric-multivalue-field-to-other-numeric/m-p/673573#M230645</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2024-01-08T23:15:03Z</dc:date>
    </item>
  </channel>
</rss>

