<?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 can I merge 2 tabled rows and add field values from columns as new fields? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326433#M97314</link>
    <description>&lt;P&gt;Give this a try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your current search giving fields Date A B C
| eventstats first(Date) as Date
| untable Date metrics data
| streamstats count by metrics | eval metrics=metrics."_".count
| xyseries Date metrics data
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 27 Feb 2017 17:13:11 GMT</pubDate>
    <dc:creator>somesoni2</dc:creator>
    <dc:date>2017-02-27T17:13:11Z</dc:date>
    <item>
      <title>How can I merge 2 tabled rows and add field values from columns as new fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326432#M97313</link>
      <description>&lt;P&gt;I am looking to combine columns/values from row 2 to row 1 as additional columns.  I am not sure which commands should be used to achieve this and would appreciate any help.&lt;/P&gt;

&lt;P&gt;Example:&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Current format&lt;/STRONG&gt;&lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/2549iB98A0DB9DBCCEF12/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Desired format&lt;/STRONG&gt;&lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/2550iFA96DB77CC04A08B/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Feb 2017 15:46:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326432#M97313</guid>
      <dc:creator>nidhsha2</dc:creator>
      <dc:date>2017-02-27T15:46:36Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge 2 tabled rows and add field values from columns as new fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326433#M97314</link>
      <description>&lt;P&gt;Give this a try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your current search giving fields Date A B C
| eventstats first(Date) as Date
| untable Date metrics data
| streamstats count by metrics | eval metrics=metrics."_".count
| xyseries Date metrics data
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Feb 2017 17:13:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326433#M97314</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-02-27T17:13:11Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge 2 tabled rows and add field values from columns as new fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326434#M97315</link>
      <description>&lt;P&gt;somesoni2's answer is perfect if either (1) you will only ever have 2 days in your search, or (3) however many days ARE returned, you want to display them all horizontally.  &lt;/P&gt;

&lt;P&gt;Where you have more than two days, this strategy will compare each day's results to the following day's.&lt;/P&gt;

&lt;P&gt;This part just makes test data-&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval mydata="7/1/2016,1,2,3 7/2/2016,4,5,6 7/3/2016,7,8,9" 
| makemv mydata
| mvexpand mydata 
| makemv delim="," mydata
| eval Date = strptime(mvindex(mydata,0),"%m/%d/%Y")
| eval A=mvindex(mydata,1)
| eval B=mvindex(mydata,2)
| eval C=mvindex(mydata,3)
| table Date A B C
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This part spreads each day's data into two sets, one to be the current day and one to compare with the prior day.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval switcher="left right"
| makemv switcher 
| mvexpand switcher
| eval Date=if(switcher="left",Date,Date-86400)
| eval A_2=if(switcher="left",Null(),A)
| eval A=if(switcher="left",A,Null())
| eval B_2=if(switcher="left",Null(),B)
| eval B=if(switcher="left",B,Null())
| eval C_2=if(switcher="left",Null(),C)
| eval C=if(switcher="left",C,Null())
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This part collects it up and displays it.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats values(*) as * by Date 
| where like(A,"%") AND like(A_2,"%")
| eval DateDisplay=strftime(Date,"%Y-%m-%d")
| table DateDisplay A B C A_2 B_2 C_2
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Feb 2017 21:08:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326434#M97315</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-02-27T21:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge 2 tabled rows and add field values from columns as new fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326435#M97316</link>
      <description>&lt;P&gt;Here's test code for the curious.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval mydata="7/1/2016,1,2,3 7/2/2016,4,5,6" 
| makemv mydata
| mvexpand mydata 
| makemv delim="," mydata
| eval Date = strptime(mvindex(mydata,0),"%m/%d/%Y")
| eval A=mvindex(mydata,1)
| eval B=mvindex(mydata,2)
| eval C=mvindex(mydata,3)
| table Date A B C


| eventstats first(Date) as Date
| untable Date metrics data
| streamstats count by metrics 
| eval metrics=metrics."_".count
| xyseries Date metrics data
| eval DateDisplay=strftime(Date,"%Y-%m-%d")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;Explanation of the code:&lt;/P&gt;

&lt;P&gt;Everything before the space just creates test data.&lt;/P&gt;

&lt;P&gt;Eventstats keeps only the earliest date , throwing away the unneeded date from the second record.&lt;/P&gt;

&lt;P&gt;Untable creates records that (in this case) all have the same Date, each record with the field name in "metrics" and the field value in "data".&lt;/P&gt;

&lt;P&gt;Streamstats determines a suffix for each field name so that the combined field name will be unique.  This usage assumes the data for the earliest date will appear first in the underlying data.&lt;/P&gt;

&lt;P&gt;Eval appends the suffix onto the field name in metrics in order to create the unique field name.&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Being a belt and suspenders and duct-tape sort of a person, I dislike verbs and functions --first() and streamstats in this case-- that depend on an event order that I can't see by inspection of the code.  I'd probably amend the code to this :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| sort 0 Date
| untable Date metrics data
| streamstats count by metrics 
| eval metrics=metrics."_".count
| eventstats min(Date) as Date
| xyseries Date metrics data
| eval DateDisplay=strftime(Date,"%Y-%m-%d")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Feb 2017 21:24:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326435#M97316</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-02-27T21:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge 2 tabled rows and add field values from columns as new fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326436#M97317</link>
      <description>&lt;P&gt;Thanks!  This is exactly what I am looking for.  The only issue I am hitting now is the last line....transforming to xyseries.  I do not necessarily care about the date now but if I execute your last line I get "no results found".&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 15:27:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326436#M97317</guid>
      <dc:creator>nidhsha2</dc:creator>
      <dc:date>2017-03-01T15:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge 2 tabled rows and add field values from columns as new fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326437#M97318</link>
      <description>&lt;P&gt;So the xyseries command is failing? Do you get results with field name Date, metrics and data (case sensitive)? You can also replace xyseries command with following&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| chart values(data) over Date by metrics
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2017 15:38:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-merge-2-tabled-rows-and-add-field-values-from-columns/m-p/326437#M97318</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-03-01T15:38:13Z</dc:date>
    </item>
  </channel>
</rss>

