<?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: Unpivot multi-value fields in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333198#M4920</link>
    <description>&lt;P&gt;It's not pretty, but adding this at the end of the Search should fix-up the duplicate inverse problem:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Checks=mvappend(sha256(Email1.Email1_Type.Email2.Email2_Type.email_id),sha256(Email2.Email2_Type.Email1.Email1_Type.email_id))
| mvexpand Checks
| dedup Checks
| dedup Email1,Email1_Type,Email2,Email2_Type,email_id
| fields - Checks
| sort + email_id,Email1,Email1_Type,Email2,Email2_Type
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But worth trying with a bigger dataset to make sure that the whole thing scales enough for you.&lt;/P&gt;</description>
    <pubDate>Thu, 02 Mar 2017 07:25:46 GMT</pubDate>
    <dc:creator>gvmorley</dc:creator>
    <dc:date>2017-03-02T07:25:46Z</dc:date>
    <item>
      <title>Unpivot multi-value fields</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333196#M4918</link>
      <description>&lt;P&gt;I am working with some email header data, starting with generating some multi-value fields and now get to this point... &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=teed sourcetype="emaildata" | eval max_date_sent= max(DateTimeSent) | dedup message_id, max_date_sent |table FromEmailAddress, Subject, DateTimeSent, ToRecipient, CcRecipient, BccRecipient | where FromEmailAddress != "" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&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/2574iC42CD908666CC0A4/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;but what I really want is to examine the individual relationship, so the final output will like this...&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/2575i8BDE90F422B1ABBF/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;I've tried this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=cms sourcetype="exchangeemails" | eval max_date_sent= max(DateTimeSent) | dedup InternetMessageID, max_date_sent | eval tocc=mvzip(ToRecipients,CcRecipients) | eval receivers=mvzip(tocc,CcRecipients) | mvexpand receivers | makemv delim="," receivers | table FromEmailAddress, receivers
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but this returns some duplicated records for the receivers, and cannot distinguish if the receiver was originally a to/cc/bcc. &lt;BR /&gt;
Can someone point direction? Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 00:04:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333196#M4918</guid>
      <dc:creator>angliu</dc:creator>
      <dc:date>2017-03-02T00:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: Unpivot multi-value fields</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333197#M4919</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I'm not sure if this is the 'best' way, as it feels somewhat overly complicated, but I think I can achieve close to what you're looking for with the following...&lt;/P&gt;

&lt;P&gt;This first bit of code is just to simulate your data:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| fields - _time 
| eval Sender="xxx@gmail.com", Subject="something", Date="3/1/2017", ToRecipient="xxx@hotmail.com", CcRecipient="xxx@msn.com", BccRecipient="xxx@wa.com" 
| eval CcRecipient=mvappend(CcRecipient,"xxx@yahoo.com","xxx@splunk.com") 
| append 
    [| makeresults 
    | fields - _time 
    | eval Sender="yyy@gmail.com", Subject="something else", Date="3/1/2017", ToRecipient="yyy@hotmail.com", CcRecipient="yyy@msn.com", BccRecipient="yyy@wa.com" 
    | eval CcRecipient=mvappend(CcRecipient,"yyy@yahoo.com","yyy@splunk.com")] 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This gives 2 results like this:&lt;/P&gt;

&lt;P&gt;&lt;IMG src="https://community.splunk.com/storage/temp/188228-screen-shot-2017-03-02-at-132040.png" alt="alt text" /&gt;&lt;/P&gt;

&lt;P&gt;Next we need some sort of unique ID for each result. You may well have this in your original data, but if not, you can just hash some (or all) fields:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval email_id=sha256(Sender.Subject.Date)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then we add the 'type' to the different address, so that when we manipulate these address later, we don't lose what track of what they were:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex max_match=0 mode=sed field=CcRecipient "s/(.*)/\1|Cc/g" 
| rex max_match=0 mode=sed field=BccRecipient "s/(.*)/\1|Bcc/g" 
| rex max_match=0 mode=sed field=ToRecipient "s/(.*)/\1|To/g" 
| rex max_match=0 mode=sed field=Sender "s/(.*)/\1|Sender/g" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Next, put all of the addresses together into a single field and drop the other fields that we don't need:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval AllEmails=mvappend(ToRecipient,CcRecipient,BccRecipient,Sender) 
| fields - ToRecipient,CcRecipient,BccRecipient,Sender,Date,Subject 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now we want to create a result for each individual email address. This is where the 'email_id' field is useful, so that we have an association between the addresses of individual emails:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| mvexpand AllEmails 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And we can now create a 'Type' field to represent if the address was Sender,To,Cc or Bcc. And at the same time, tidy up the original fields:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=AllEmails "^[^|]+\|(?&amp;lt;Email_Type&amp;gt;(?:To|Cc|Bcc|Sender))" 
| rex field=AllEmails mode=sed "s/([^|]+).*/\1/g" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;At this point, you should now have a result for every single address.&lt;/P&gt;

&lt;P&gt;Now the fun bit. Since you want to associate some data that you've already got, with itself, we can use an appendpipe and a bit of stats to 'duplicate' the results. At the same time, we rename a number of fields to Email2 and Email2_Type:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| appendpipe 
    [| stats list(AllEmails) AS Email2 list(Email_Type) AS Email2_Type list(email_id) AS email_id by AllEmails
    | fields - AllEmails]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now we use a selfjoin on the email_id to match results from the first part with the second part. Importantly we need to max=0 to ensure that there is an 'everything-to-everything' result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| selfjoin max=0 email_id 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Finally we rename some of the original fields to make more sense. And we filter out the results which didn't have a join, plus a bit of sorting:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rename AllEmails AS Email1 Email_Type AS Email1_Type 
| where isnotnull(Email2) AND Email1!=Email2 
| sort + email_id,Email1,Email1_Type,Email2,Email2_Type
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And finally, we should have the table which you were looking for:&lt;/P&gt;

&lt;P&gt;&lt;IMG src="https://community.splunk.com/storage/temp/188229-screen-shot-2017-03-02-at-134338.png" alt="alt text" /&gt;&lt;/P&gt;

&lt;P&gt;At this point you can drop the email_id field as it's no longer needed.&lt;/P&gt;

&lt;P&gt;IMPORTANT: There is still one problem with this. It doesn't yet deal with the 'inverse' duplication problem.&lt;/P&gt;

&lt;P&gt;I.e. Where you have:&lt;/P&gt;

&lt;P&gt;Email1              Email1_Type     Email2              Email2_Type&lt;BR /&gt;
&lt;A href="mailto:yyy@gmail.com" target="_blank"&gt;yyy@gmail.com&lt;/A&gt;       Sender          &lt;A href="mailto:yyy@hotmail.com" target="_blank"&gt;yyy@hotmail.com&lt;/A&gt; To&lt;BR /&gt;
&lt;A href="mailto:yyy@hotmail.com" target="_blank"&gt;yyy@hotmail.com&lt;/A&gt; To              &lt;A href="mailto:yyy@gmail.com" target="_blank"&gt;yyy@gmail.com&lt;/A&gt;       Sender&lt;/P&gt;

&lt;P&gt;Sorry - I ran out of time for that bit, but if I get a chance, I'll see if I can figure it out later!&lt;/P&gt;

&lt;P&gt;Hope that helps.&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Below is the full search, so it's easier to copy and paste:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| fields - _time 
| eval Sender="xxx@gmail.com", Subject="something", Date="3/1/2017", ToRecipient="xxx@hotmail.com", CcRecipient="xxx@msn.com", BccRecipient="xxx@wa.com" 
| eval CcRecipient=mvappend(CcRecipient,"xxx@yahoo.com","xxx@splunk.com") 
| append 
    [| makeresults 
    | fields - _time 
    | eval Sender="yyy@gmail.com", Subject="something else", Date="3/1/2017", ToRecipient="yyy@hotmail.com", CcRecipient="yyy@msn.com", BccRecipient="yyy@wa.com" 
    | eval CcRecipient=mvappend(CcRecipient,"yyy@yahoo.com","yyy@splunk.com")] 
| eval email_id=sha256(Sender.Subject.Date) 
| rex max_match=0 mode=sed field=CcRecipient "s/(.*)/\1|Cc/g" 
| rex max_match=0 mode=sed field=BccRecipient "s/(.*)/\1|Bcc/g" 
| rex max_match=0 mode=sed field=ToRecipient "s/(.*)/\1|To/g" 
| rex max_match=0 mode=sed field=Sender "s/(.*)/\1|Sender/g" 
| eval AllEmails=mvappend(ToRecipient,CcRecipient,BccRecipient,Sender) 
| fields - ToRecipient,CcRecipient,BccRecipient,Sender,Date,Subject 
| mvexpand AllEmails 
| rex field=AllEmails "^[^|]+\|(?&amp;lt;Email_Type&amp;gt;(?:To|Cc|Bcc|Sender))" 
| rex field=AllEmails mode=sed "s/([^|]+).*/\1/g" 
| appendpipe 
    [| stats list(AllEmails) AS Email2 list(Email_Type) AS Email2_Type list(email_id) AS email_id by AllEmails 
    | fields - AllEmails] 
| selfjoin max=0 email_id 
| rename AllEmails AS Email1 Email_Type AS Email1_Type 
| where isnotnull(Email2) AND Email1!=Email2
| sort + email_id,Email1,Email1_Type,Email2,Email2_Type
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 13:03:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333197#M4919</guid>
      <dc:creator>gvmorley</dc:creator>
      <dc:date>2020-09-29T13:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: Unpivot multi-value fields</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333198#M4920</link>
      <description>&lt;P&gt;It's not pretty, but adding this at the end of the Search should fix-up the duplicate inverse problem:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Checks=mvappend(sha256(Email1.Email1_Type.Email2.Email2_Type.email_id),sha256(Email2.Email2_Type.Email1.Email1_Type.email_id))
| mvexpand Checks
| dedup Checks
| dedup Email1,Email1_Type,Email2,Email2_Type,email_id
| fields - Checks
| sort + email_id,Email1,Email1_Type,Email2,Email2_Type
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But worth trying with a bigger dataset to make sure that the whole thing scales enough for you.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 07:25:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333198#M4920</guid>
      <dc:creator>gvmorley</dc:creator>
      <dc:date>2017-03-02T07:25:46Z</dc:date>
    </item>
    <item>
      <title>Re: Unpivot multi-value fields</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333199#M4921</link>
      <description>&lt;P&gt;Wow. You're the best. Thank you so much. &lt;BR /&gt;
If it works, it works! At least it gets me much further down the path... thanks again!&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 19:48:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333199#M4921</guid>
      <dc:creator>angliu</dc:creator>
      <dc:date>2017-03-02T19:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Unpivot multi-value fields</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333200#M4922</link>
      <description>&lt;P&gt;No worries. I'm pleased that it helped. Enjoy your Splunk journey!&lt;/P&gt;

&lt;P&gt;(And if you're happy with the answer, just mark the question as 'answered' - as it helps the rest of the community know that this one is sorted.)&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 01:59:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Unpivot-multi-value-fields/m-p/333200#M4922</guid>
      <dc:creator>gvmorley</dc:creator>
      <dc:date>2017-03-03T01:59:07Z</dc:date>
    </item>
  </channel>
</rss>

