<?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 Power Query: Search in previous rows in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Power-Query-Search-in-previous-rows/m-p/535229#M16467</link>
    <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I have the following input from a text file:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;11,1,11,DT:17.09.18/13:04:30&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,2,12,NR:5963&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,3,13,NR:102&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,4,21,NR:200170006&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,4,23,NR:200170000&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,4,24,TX:'1'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,4,31,CA:1/3.5&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,5,11,DT:17.09.18/16:53:13&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,6,12,NR:5423&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,7,13,NR:5&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,8,21,NR:200010002&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,8,23,NR:200010000&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,8,24,TX:'1'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,8,31,CA:1/3.8&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Column 1: constant (11)&lt;/FONT&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Column 2: consecutive counter&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Column 3: row type (e.g. 11 = Date; 12: Key for person; 21: product key; 23: product group key; 31: number of units and revenue&amp;nbsp;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;The rows with types 11 always embed a unit of transaction and I'd like to create a new column, which holds an identifier for the transaction. This could be the counter (Col 2) of the date row (Col 3 = 11).&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;BR /&gt;&lt;BR /&gt;That means, for each row, the value of column 2 of the last previous row of type 11 has to be found. In the above example, the new column would hold 1 for the first seven rows and 5 for the last seven rows.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;That's quite easy in DAX, but I'd like to do it on the Power Query level in M.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Any ideas?&lt;BR /&gt;&lt;BR /&gt;Thanks ind advance&lt;BR /&gt;Frank&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 06 Oct 2018 07:48:05 GMT</pubDate>
    <dc:creator>fbackes</dc:creator>
    <dc:date>2018-10-06T07:48:05Z</dc:date>
    <item>
      <title>Power Query: Search in previous rows</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-Query-Search-in-previous-rows/m-p/535229#M16467</link>
      <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I have the following input from a text file:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;11,1,11,DT:17.09.18/13:04:30&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,2,12,NR:5963&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,3,13,NR:102&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,4,21,NR:200170006&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,4,23,NR:200170000&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,4,24,TX:'1'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,4,31,CA:1/3.5&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,5,11,DT:17.09.18/16:53:13&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,6,12,NR:5423&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,7,13,NR:5&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,8,21,NR:200010002&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,8,23,NR:200010000&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,8,24,TX:'1'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;11,8,31,CA:1/3.8&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Column 1: constant (11)&lt;/FONT&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Column 2: consecutive counter&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Column 3: row type (e.g. 11 = Date; 12: Key for person; 21: product key; 23: product group key; 31: number of units and revenue&amp;nbsp;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;The rows with types 11 always embed a unit of transaction and I'd like to create a new column, which holds an identifier for the transaction. This could be the counter (Col 2) of the date row (Col 3 = 11).&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;BR /&gt;&lt;BR /&gt;That means, for each row, the value of column 2 of the last previous row of type 11 has to be found. In the above example, the new column would hold 1 for the first seven rows and 5 for the last seven rows.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;That's quite easy in DAX, but I'd like to do it on the Power Query level in M.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Any ideas?&lt;BR /&gt;&lt;BR /&gt;Thanks ind advance&lt;BR /&gt;Frank&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Oct 2018 07:48:05 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-Query-Search-in-previous-rows/m-p/535229#M16467</guid>
      <dc:creator>fbackes</dc:creator>
      <dc:date>2018-10-06T07:48:05Z</dc:date>
    </item>
    <item>
      <title>Re: Power Query: Search in previous rows</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-Query-Search-in-previous-rows/m-p/536394#M16550</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/8286"&gt;@fbackes&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I found a solution for you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Add index column&lt;/P&gt;&lt;P&gt;2. Calculate transaction ID per each row as per below code (see &lt;STRONG&gt;Result&lt;/STRONG&gt; step):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;let
    &lt;EM&gt;//Entered your data&lt;/EM&gt;
    &lt;STRONG&gt;Source&lt;/STRONG&gt; = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRU0lECY0OlWB0o3wjEN0LwjUF8YwTfBKTGEI2PLm+CyjdGUm+KZp8Zmn3maPZZoNlngWafBZp9FlD7YgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [constant = _t, #"consecutive counter" = _t, #"row type" = _t]),
    &lt;STRONG&gt;ChangedDataTypes&lt;/STRONG&gt; = Table.TransformColumnTypes(Source,{{"constant", Int64.Type}, {"consecutive counter", Int64.Type}, {"row type", Int64.Type}}),
    &lt;EM&gt;//Add index starting from zero&lt;/EM&gt;
    &lt;STRONG&gt;AddIndexColumn&lt;/STRONG&gt; = Table.AddIndexColumn(ChangedDataTypes, "Row", 0, 1),
    &lt;EM&gt;//Calculate transaction id&lt;/EM&gt;
    &lt;STRONG&gt;Result&lt;/STRONG&gt; = 
            Table.AddColumn(AddIndexColumn, "TransactionID", 
                each 
                    let 
                        Ix = [Row],
                        value = List.Last(Table.SelectRows(AddIndexColumn,each [Row]&amp;lt;=Ix and [row type] = 11)[consecutive counter])
                    in 
                        value
                        )
in
    &lt;STRONG&gt;Result&lt;/STRONG&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="result.JPG" style="width: 400px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/124596iFEEBCDB5B0802E74/image-size/medium?v=v2&amp;amp;px=400" role="button" title="result.JPG" alt="result.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;BR /&gt;Ruslan&lt;BR /&gt;-------------------------------------------------------------------&lt;BR /&gt;&lt;STRONG&gt;Did I answer your question? Mark my post as a solution!&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 14:24:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-Query-Search-in-previous-rows/m-p/536394#M16550</guid>
      <dc:creator>zoloturu</dc:creator>
      <dc:date>2018-10-08T14:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: Power Query: Search in previous rows</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-Query-Search-in-previous-rows/m-p/537781#M16597</link>
      <description>&lt;P&gt;Many thanks, Ruslan! It works perfect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards&lt;BR /&gt;Frank&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Oct 2018 19:55:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-Query-Search-in-previous-rows/m-p/537781#M16597</guid>
      <dc:creator>fbackes</dc:creator>
      <dc:date>2018-10-09T19:55:51Z</dc:date>
    </item>
  </channel>
</rss>

