<?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 to sort a table by a time field in text format in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-sort-a-table-by-a-time-field-in-text-format/m-p/470525#M132373</link>
    <description>&lt;P&gt;Here's the solution.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    | eval convertedRECEIPTDATE=strptime(RECEIPTDATE, "%m/%d/%Y") 
    | eval receiptDateStart=strftime("$tok_time.earliest$", "%m/%d/%Y") 
    | eval receiptDateEnd=strftime("$tok_time.latest$", "%m/%d/%Y")
    | where convertedRECEIPTDATE &amp;amp;gt;= $tok_time.earliest$ AND convertedRECEIPTDATE &amp;amp;lt;= $tok_time.latest$
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The results were what I needed, sort of. The rows were not sorted according to the &lt;STRONG&gt;RECEIPTDATE&lt;/STRONG&gt; field. And when I manually sort on that field/column in the dashboard, the sort order is incorrect. It is based on text and not date.&lt;BR /&gt;
&lt;EM&gt;11/21/2019&lt;BR /&gt;
12/2/2019&lt;BR /&gt;
3/1/2019&lt;BR /&gt;
3/11/2019&lt;BR /&gt;
3/2/2019&lt;BR /&gt;
etc.....&lt;/EM&gt;&lt;/P&gt;

&lt;P&gt;I tried sorting on the &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt;, &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    | sort convertedRECEIPTDATE
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but this did not work, and the column for &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt; field was empty. This did not make sense, because when I ran a separate search of this dashboard panel, &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt; is a field with valid &lt;EM&gt;epoch date values&lt;/EM&gt;.&lt;/P&gt;

&lt;P&gt;I considered adding &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt; to my stats command, before the &lt;EM&gt;sort&lt;/EM&gt; and &lt;EM&gt;table&lt;/EM&gt; commands. But thought this might be an issue.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats sum(TOTAMT) AS TOTAMT1, sum(ORIGCOSAMT) AS ORIGCOSAMT1, sum(ORIGFEEAMT) AS ORIGFEEAMT1 BY APPLICATIONNAME, RECEIPTDATE, convertedRECEIPTDATE
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Does anyone know if this adds any &lt;EM&gt;CPU cycles&lt;/EM&gt; or &lt;EM&gt;IOPS&lt;/EM&gt; (running the same stats on identical BY clauses (&lt;STRONG&gt;RECEIPTDATE&lt;/STRONG&gt; and &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt;)? However, it does work.&lt;/P&gt;

&lt;P&gt;Here is the entire SPL for this panel.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    index=$tok_index$
        AND FAILCODE=N
        AND APPLICATIONNAME=$tok_payApp$
    | eval convertedRECEIPTDATE=strptime(RECEIPTDATE, "%m/%d/%Y") 
    | eval receiptDateStart=strftime("$tok_time.earliest$", "%m/%d/%Y") 
    | eval receiptDateEnd=strftime("$tok_time.latest$", "%m/%d/%Y")
    | where convertedRECEIPTDATE &amp;amp;gt;= $tok_time.earliest$ AND convertedRECEIPTDATE &amp;amp;lt;= $tok_time.latest$
    | dedup TOKEN 
    | eval TOTAMT=coalesce(TOTALAMOUNT, RPT_ORIG_TRANS_TOTAL_AMOUNT) 
    | eval ORIGCOSAMT=coalesce(ORIG_COS_AMOUNT, RPT_ORIG_COS_AMOUNT) 
    | eval ORIGFEEAMT=coalesce(ORIG_FEE_AMOUNT,RPT_ORIG_FEE_AMOUNT) 
    | stats sum(TOTAMT) AS TOTAMT1, sum(ORIGCOSAMT) AS ORIGCOSAMT1, sum(ORIGFEEAMT) AS ORIGFEEAMT1 BY APPLICATIONNAME, RECEIPTDATE, convertedRECEIPTDATE 
    | eval TOTAMT2="$".tostring(round(TOTAMT1,2),"commas") 
    | eval ORIGCOSAMT2="$".tostring(round(ORIGCOSAMT1,2),"commas") 
    | eval ORIGFEEAMT2="$".tostring(round(ORIGFEEAMT1,2),"commas") 
    | rename APPLICATIONNAME AS "Pay Credit Card Application", RECEIPTDATE AS "Receipt Date", TOTAMT2 AS "Total Amount", ORIGCOSAMT2 AS "Original Cost Amount", ORIGFEEAMT2 AS "Transaction Fees Amount" 
    | sort convertedRECEIPTDATE
    | table "Pay Credit Card Application", "Receipt Date", "Total Amount", "Original Cost Amount", "Transaction Fees Amount"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So the moral of the story (problem) is, if you need to sort on a date field, but it is in text format, &lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Create a new field using &lt;STRONG&gt;eval&lt;/STRONG&gt; and &lt;STRONG&gt;strptime&lt;/STRONG&gt;.&lt;/LI&gt;
&lt;LI&gt;Use both the new and original fields in the &lt;EM&gt;BY&lt;/EM&gt; clause of your &lt;EM&gt;stats&lt;/EM&gt; command.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;sort&lt;/EM&gt; your results by the new field only.&lt;/LI&gt;
&lt;LI&gt;Leave the new field out of your &lt;EM&gt;table&lt;/EM&gt; command.&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;Thanks and God bless,&lt;BR /&gt;
Genesius&lt;/P&gt;</description>
    <pubDate>Mon, 30 Dec 2019 18:57:20 GMT</pubDate>
    <dc:creator>genesiusj</dc:creator>
    <dc:date>2019-12-30T18:57:20Z</dc:date>
    <item>
      <title>How to sort a table by a time field in text format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-sort-a-table-by-a-time-field-in-text-format/m-p/470523#M132371</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;
Here's the problem.&lt;/P&gt;

&lt;P&gt;Dashboard - Time picker is used to select a date range. &lt;BR /&gt;
But this date range is not checked against &lt;STRONG&gt;_time&lt;/STRONG&gt; field. &lt;BR /&gt;
It is checked against a date field &lt;STRONG&gt;RECEIPTDATE&lt;/STRONG&gt; in text format: Ex. &lt;EM&gt;11/21/2019&lt;/EM&gt;.&lt;/P&gt;

&lt;P&gt;Thanks and God bless,&lt;BR /&gt;
Genesius&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 16:30:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-sort-a-table-by-a-time-field-in-text-format/m-p/470523#M132371</guid>
      <dc:creator>genesiusj</dc:creator>
      <dc:date>2019-12-30T16:30:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort a table by a time field in text format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-sort-a-table-by-a-time-field-in-text-format/m-p/470524#M132372</link>
      <description>&lt;P&gt;Thanks for sharing, @genesiusj .  To help future readers find your solution, please edit your posting into a question and put the solution in an answer.  Then accept the answer.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 18:07:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-sort-a-table-by-a-time-field-in-text-format/m-p/470524#M132372</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2019-12-30T18:07:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort a table by a time field in text format</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-sort-a-table-by-a-time-field-in-text-format/m-p/470525#M132373</link>
      <description>&lt;P&gt;Here's the solution.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    | eval convertedRECEIPTDATE=strptime(RECEIPTDATE, "%m/%d/%Y") 
    | eval receiptDateStart=strftime("$tok_time.earliest$", "%m/%d/%Y") 
    | eval receiptDateEnd=strftime("$tok_time.latest$", "%m/%d/%Y")
    | where convertedRECEIPTDATE &amp;amp;gt;= $tok_time.earliest$ AND convertedRECEIPTDATE &amp;amp;lt;= $tok_time.latest$
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The results were what I needed, sort of. The rows were not sorted according to the &lt;STRONG&gt;RECEIPTDATE&lt;/STRONG&gt; field. And when I manually sort on that field/column in the dashboard, the sort order is incorrect. It is based on text and not date.&lt;BR /&gt;
&lt;EM&gt;11/21/2019&lt;BR /&gt;
12/2/2019&lt;BR /&gt;
3/1/2019&lt;BR /&gt;
3/11/2019&lt;BR /&gt;
3/2/2019&lt;BR /&gt;
etc.....&lt;/EM&gt;&lt;/P&gt;

&lt;P&gt;I tried sorting on the &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt;, &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    | sort convertedRECEIPTDATE
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but this did not work, and the column for &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt; field was empty. This did not make sense, because when I ran a separate search of this dashboard panel, &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt; is a field with valid &lt;EM&gt;epoch date values&lt;/EM&gt;.&lt;/P&gt;

&lt;P&gt;I considered adding &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt; to my stats command, before the &lt;EM&gt;sort&lt;/EM&gt; and &lt;EM&gt;table&lt;/EM&gt; commands. But thought this might be an issue.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats sum(TOTAMT) AS TOTAMT1, sum(ORIGCOSAMT) AS ORIGCOSAMT1, sum(ORIGFEEAMT) AS ORIGFEEAMT1 BY APPLICATIONNAME, RECEIPTDATE, convertedRECEIPTDATE
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Does anyone know if this adds any &lt;EM&gt;CPU cycles&lt;/EM&gt; or &lt;EM&gt;IOPS&lt;/EM&gt; (running the same stats on identical BY clauses (&lt;STRONG&gt;RECEIPTDATE&lt;/STRONG&gt; and &lt;STRONG&gt;convertedRECEIPTDATE&lt;/STRONG&gt;)? However, it does work.&lt;/P&gt;

&lt;P&gt;Here is the entire SPL for this panel.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    index=$tok_index$
        AND FAILCODE=N
        AND APPLICATIONNAME=$tok_payApp$
    | eval convertedRECEIPTDATE=strptime(RECEIPTDATE, "%m/%d/%Y") 
    | eval receiptDateStart=strftime("$tok_time.earliest$", "%m/%d/%Y") 
    | eval receiptDateEnd=strftime("$tok_time.latest$", "%m/%d/%Y")
    | where convertedRECEIPTDATE &amp;amp;gt;= $tok_time.earliest$ AND convertedRECEIPTDATE &amp;amp;lt;= $tok_time.latest$
    | dedup TOKEN 
    | eval TOTAMT=coalesce(TOTALAMOUNT, RPT_ORIG_TRANS_TOTAL_AMOUNT) 
    | eval ORIGCOSAMT=coalesce(ORIG_COS_AMOUNT, RPT_ORIG_COS_AMOUNT) 
    | eval ORIGFEEAMT=coalesce(ORIG_FEE_AMOUNT,RPT_ORIG_FEE_AMOUNT) 
    | stats sum(TOTAMT) AS TOTAMT1, sum(ORIGCOSAMT) AS ORIGCOSAMT1, sum(ORIGFEEAMT) AS ORIGFEEAMT1 BY APPLICATIONNAME, RECEIPTDATE, convertedRECEIPTDATE 
    | eval TOTAMT2="$".tostring(round(TOTAMT1,2),"commas") 
    | eval ORIGCOSAMT2="$".tostring(round(ORIGCOSAMT1,2),"commas") 
    | eval ORIGFEEAMT2="$".tostring(round(ORIGFEEAMT1,2),"commas") 
    | rename APPLICATIONNAME AS "Pay Credit Card Application", RECEIPTDATE AS "Receipt Date", TOTAMT2 AS "Total Amount", ORIGCOSAMT2 AS "Original Cost Amount", ORIGFEEAMT2 AS "Transaction Fees Amount" 
    | sort convertedRECEIPTDATE
    | table "Pay Credit Card Application", "Receipt Date", "Total Amount", "Original Cost Amount", "Transaction Fees Amount"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So the moral of the story (problem) is, if you need to sort on a date field, but it is in text format, &lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Create a new field using &lt;STRONG&gt;eval&lt;/STRONG&gt; and &lt;STRONG&gt;strptime&lt;/STRONG&gt;.&lt;/LI&gt;
&lt;LI&gt;Use both the new and original fields in the &lt;EM&gt;BY&lt;/EM&gt; clause of your &lt;EM&gt;stats&lt;/EM&gt; command.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;sort&lt;/EM&gt; your results by the new field only.&lt;/LI&gt;
&lt;LI&gt;Leave the new field out of your &lt;EM&gt;table&lt;/EM&gt; command.&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;Thanks and God bless,&lt;BR /&gt;
Genesius&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 18:57:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-sort-a-table-by-a-time-field-in-text-format/m-p/470525#M132373</guid>
      <dc:creator>genesiusj</dc:creator>
      <dc:date>2019-12-30T18:57:20Z</dc:date>
    </item>
  </channel>
</rss>

