<?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: Create new column based on values from 2 rows in Power Query</title>
    <link>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3930663#M129083</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you. A problem is that we can only calculate 'DaysToNext' from 2 rows, if they both have other common attributes. For example, if the 2 rows have the column 'Name' in common ... I tried adding in a further condition (as highlighted in bold) but this method assume the rows are consecutive so doesn't quite work ...&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;let Source = Table.FromRecords({&lt;BR /&gt;[DateText = 20240501, Name="John"],&lt;BR /&gt;[DateText = 20240502, Name="John"],&lt;BR /&gt;[DateText = 20240503, Name="Peter"],&lt;BR /&gt;[DateText = 20240507, Name="John"],&lt;BR /&gt;[DateText = 20240508, Name="John"],&lt;BR /&gt;[DateText = 20240509, Name="John"],&lt;BR /&gt;[DateText = 20240510, Name="John"],&lt;BR /&gt;[DateText = 20240513, Name="John"]&lt;BR /&gt;}),&lt;BR /&gt;Index = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),&lt;BR /&gt;Date = Table.AddColumn(Index,"Date", each Date.From(Text.From([DateText])),type date),&lt;BR /&gt;&lt;STRONG&gt;Name = Table.Column(Source, "Name") as list,&lt;/STRONG&gt;&lt;BR /&gt;Max = List.Max(Date[Index]),&lt;BR /&gt;Days = Table.AddColumn(Date, "DaysToNext", each if [Index] &amp;lt;Max and &lt;STRONG&gt;Name{[Index]} = Name{[Index] + 1}&lt;/STRONG&gt;&lt;BR /&gt;then Date[DateText]{[Index]+1} - Date[DateText]{[Index]} else 0),&lt;BR /&gt;RemovedRedundantCols = Table.RemoveColumns(Days,{"DateText", "Index"})&lt;/P&gt;&lt;P&gt;in&lt;BR /&gt;RemovedRedundantCols&lt;/P&gt;</description>
    <pubDate>Sun, 19 May 2024 17:18:56 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2024-05-19T17:18:56Z</dc:date>
    <item>
      <title>Create new column based on values from 2 rows</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3917664#M128806</link>
      <description>&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;I have a table with a 'Date' column and wish to create column called 'DaysToNext' which is derived from the current row's Date and next row's date&amp;nbsp;&lt;/P&gt;&lt;P&gt;i.e. DaysToNext =&amp;nbsp;Next Row's Date - Current Row's Date&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone help here?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 14:49:37 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3917664#M128806</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-05-14T14:49:37Z</dc:date>
    </item>
    <item>
      <title>Re: Create new column based on values from 2 rows</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3917839#M128809</link>
      <description>&lt;P&gt;You could use an Index column and find the date with the next index and subtract the date of the current row from the one of the next row.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, in this example:&lt;/P&gt;&lt;P&gt;- I replicated the column as a number or text (just to recreate the column - you might not need this)&lt;/P&gt;&lt;P&gt;- Added an index&lt;/P&gt;&lt;P&gt;- Converted the date to type date (as it was text or number) - in your case you could also just subtract the numbers of course, but thought this was cleaner&lt;/P&gt;&lt;P&gt;- Stored the maximum index number in "variable" Max&lt;/P&gt;&lt;P&gt;- Subtracted the current date from the date with the next index number (step Days) - note the if statement as it will cause an error if not added due to it trying to subtract a date from a value that does not exist for the last row. I made the last row value for DateToNext 0, but could be any number or null.&amp;nbsp;&lt;/P&gt;&lt;P&gt;- Removed the redundant "helper "columns&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;let Source = Table.FromRecords({&lt;BR /&gt;[DateText = 20240501],&lt;BR /&gt;[DateText = 20240502],&lt;BR /&gt;[DateText = 20240503],&lt;BR /&gt;[DateText = 20240507],&lt;BR /&gt;[DateText = 20240508],&lt;BR /&gt;[DateText = 20240509],&lt;BR /&gt;[DateText = 20240510],&lt;BR /&gt;[DateText = 20240513]&lt;BR /&gt;}),&lt;BR /&gt;Index = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),&lt;BR /&gt;Date = Table.AddColumn(Index,"Date", each Date.From(Text.From([DateText])),type date),&lt;BR /&gt;Max = List.Max(Date[Index]),&lt;BR /&gt;Days = Table.AddColumn(Date, "DaysToNext", each if [Index] &amp;lt;Max then Date[DateText]{[Index]+1} - Date[DateText]{[Index]} else 0),&lt;BR /&gt;RemovedRedundantCols = Table.RemoveColumns(Days,{"DateText", "Index"})&lt;BR /&gt;in&lt;BR /&gt;RemovedRedundantCols&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2024 08:24:21 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3917839#M128809</guid>
      <dc:creator>ferryv</dc:creator>
      <dc:date>2024-05-15T08:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: Create new column based on values from 2 rows</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3919584#M128844</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is another Power Query solution for your reference. The idea is to:&lt;/P&gt;
&lt;P&gt;1. add an index column starting from 1 and the second index column starting from 0,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. merge the current query to itself by two different index columns,&lt;/P&gt;
&lt;P&gt;3. expand the merged result to get the next row date for each row,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4. add a custom column to get the duration days between two days,&lt;/P&gt;
&lt;P&gt;5. remove unnecessary columns.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;let 
Source = Table.FromRecords({
    [DateText = 20240501],
    [DateText = 20240502],
    [DateText = 20240503],
    [DateText = 20240507],
    [DateText = 20240508],
    [DateText = 20240509],
    [DateText = 20240510],
    [DateText = 20240513]
}),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateText", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 0, 1, Int64.Type),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Index"}, #"Added Index1", {"Index.1"}, "Added Index1", JoinKind.LeftOuter),
    #"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"DateText"}, {"DateText.1"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Added Index1", "DaysToNext", each Duration.Days(Date.From([DateText.1]) - Date.From([DateText]))),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"DateText", "DaysToNext"})
in
    #"Removed Other Columns"&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Jing&lt;BR /&gt;&lt;EM&gt;If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2024 07:16:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3919584#M128844</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-05-15T07:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create new column based on values from 2 rows</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3919952#M128847</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another solution without index&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;let&lt;BR /&gt;Source = &lt;FONT color="#FF0000"&gt;YourSource&lt;/FONT&gt;,&lt;BR /&gt;Schema = Table.SelectRows(Table.Schema(Source), each [TypeName]&amp;lt;&amp;gt;"Any.Type"),&lt;BR /&gt;DateFrom = List.Transform(Source[Date], each Date.From(Text.From(_, "en-US"))),&lt;BR /&gt;AddDaysToNext = Table.FromColumns(&lt;BR /&gt;     Table.ToColumns(Source) &amp;amp;&lt;BR /&gt;        {List.Transform(List.Zip({DateFrom, List.Skip(DateFrom)}), each Number.From(_{1} - _{0}))}, &lt;BR /&gt;     Table.ColumnNames(Source) &amp;amp; {"DaysToNext"}),&lt;BR /&gt;TransformTypes = Table.TransformColumnTypes(AddDaysToNext, &lt;BR /&gt;     List.Zip({Schema[Name], Schema[TypeName]}) &amp;amp; {{"DaysToNext", Int64.Type}})&lt;BR /&gt;in&lt;BR /&gt;TransformTypes&lt;/PRE&gt;
&lt;P&gt;Stéphane&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2024 08:43:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3919952#M128847</guid>
      <dc:creator>slorin</dc:creator>
      <dc:date>2024-05-15T08:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create new column based on values from 2 rows</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3926397#M129004</link>
      <description>&lt;P&gt;thank you all&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2024 08:01:47 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3926397#M129004</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-05-17T08:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: Create new column based on values from 2 rows</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3930635#M129081</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;It turns out that the problem is a bit more complicated and I'm not sure the Index solution works. We can only calculate 'DaysToNext' from 2 rows, if they both have other common attributes. For example, if the 2 rows have the column 'Name' in common&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, row 2 (with Name = 'John'), the 'DaysToNext' should be 5. The reason is that we have to skip row 3 (which has Name = 'Peter') ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone pls advise further? thank you!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Date&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Name&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;DaysToNext&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20240501&lt;/TD&gt;&lt;TD&gt;John&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20240502&lt;/TD&gt;&lt;TD&gt;John&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20240503&lt;/TD&gt;&lt;TD&gt;Peter&lt;/TD&gt;&lt;TD&gt;NA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20240507&lt;/TD&gt;&lt;TD&gt;John&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;4&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20240508&lt;/TD&gt;&lt;TD&gt;Jen&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20240510&lt;/TD&gt;&lt;TD&gt;Jen&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20240517&lt;/TD&gt;&lt;TD&gt;Jen&lt;/TD&gt;&lt;TD&gt;NA&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sun, 19 May 2024 16:34:55 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3930635#M129081</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-05-19T16:34:55Z</dc:date>
    </item>
    <item>
      <title>Re: Create new column based on values from 2 rows</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3930663#M129083</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you. A problem is that we can only calculate 'DaysToNext' from 2 rows, if they both have other common attributes. For example, if the 2 rows have the column 'Name' in common ... I tried adding in a further condition (as highlighted in bold) but this method assume the rows are consecutive so doesn't quite work ...&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;let Source = Table.FromRecords({&lt;BR /&gt;[DateText = 20240501, Name="John"],&lt;BR /&gt;[DateText = 20240502, Name="John"],&lt;BR /&gt;[DateText = 20240503, Name="Peter"],&lt;BR /&gt;[DateText = 20240507, Name="John"],&lt;BR /&gt;[DateText = 20240508, Name="John"],&lt;BR /&gt;[DateText = 20240509, Name="John"],&lt;BR /&gt;[DateText = 20240510, Name="John"],&lt;BR /&gt;[DateText = 20240513, Name="John"]&lt;BR /&gt;}),&lt;BR /&gt;Index = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),&lt;BR /&gt;Date = Table.AddColumn(Index,"Date", each Date.From(Text.From([DateText])),type date),&lt;BR /&gt;&lt;STRONG&gt;Name = Table.Column(Source, "Name") as list,&lt;/STRONG&gt;&lt;BR /&gt;Max = List.Max(Date[Index]),&lt;BR /&gt;Days = Table.AddColumn(Date, "DaysToNext", each if [Index] &amp;lt;Max and &lt;STRONG&gt;Name{[Index]} = Name{[Index] + 1}&lt;/STRONG&gt;&lt;BR /&gt;then Date[DateText]{[Index]+1} - Date[DateText]{[Index]} else 0),&lt;BR /&gt;RemovedRedundantCols = Table.RemoveColumns(Days,{"DateText", "Index"})&lt;/P&gt;&lt;P&gt;in&lt;BR /&gt;RemovedRedundantCols&lt;/P&gt;</description>
      <pubDate>Sun, 19 May 2024 17:18:56 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3930663#M129083</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-05-19T17:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: Create new column based on values from 2 rows</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3932143#M129109</link>
      <description>&lt;P&gt;The key here is to use grouping, which enables tre creation of indices by group (in this case per name).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance, if you use the following as your starting point:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;You can use something like this:&lt;/P&gt;&lt;PRE&gt;//This is just the initial source info, and might be different for you &lt;BR /&gt;let Source = Table.FromRecords({ &lt;BR /&gt;   [DateText = 20240501, Name="John"], &lt;BR /&gt;   [DateText = 20240502, Name="John"], &lt;BR /&gt;   [DateText = 20240503, Name="Peter"], &lt;BR /&gt;   [DateText = 20240507, Name="John"], &lt;BR /&gt;   [DateText = 20240508, Name="Jen"], &lt;BR /&gt;   [DateText = 20240510, Name="Jen"], &lt;BR /&gt;   [DateText = 20240517, Name="Jen"] &lt;BR /&gt;}), &lt;BR /&gt;&lt;BR /&gt;//Main Index added for sorting later if needed&lt;BR /&gt;MainIndex = Table.AddIndexColumn(Source,"MainIndex",0), &lt;BR /&gt;&lt;BR /&gt;//Rows grouped to enable name specific indexing&lt;BR /&gt;GroupRows = Table.Group(MainIndex, {"Name"}, {{"Count", each _, type table [DateText=number, Name=text, Date=date,MainIndex=number]}}), &lt;BR /&gt;&lt;BR /&gt;//Adding the index for each name&lt;BR /&gt;IndexPlaceholder = Table.AddColumn(GroupRows, "IndexPlaceholder", each Table.AddIndexColumn([Count],"Index",0)),&lt;BR /&gt;&lt;BR /&gt;//Getting the maximum index per name&lt;BR /&gt;MaxPlaceholder = Table.AddColumn(IndexPlaceholder,"Max", each List.Max([IndexPlaceholder][Index])),&lt;BR /&gt;&lt;BR /&gt;//Expanding the grouped table&lt;BR /&gt;Expand = Table.ExpandTableColumn(MaxPlaceholder, "IndexPlaceholder", {"DateText", "MainIndex", "Index"}, {"DateText", "MainIndex", "Index"}),&lt;BR /&gt;&lt;BR /&gt;//Adding the number of days to the next entry with the same name&lt;BR /&gt;Days = Table.AddColumn(Expand, "DaysToNext", each if [Index] &amp;lt;[Max] and Expand[Name]{[Index]+1} = Expand[Name]{[Index]} then Expand[DateText]{[Index]+1} - Expand[DateText]{[Index]} else "NA"),&lt;BR /&gt;&lt;BR /&gt;//Sorting the table back to the original order (as it created an ordering per name)&lt;BR /&gt;Sort = Table.Sort(Days,{{"MainIndex", Order.Ascending}}),&lt;BR /&gt;&lt;BR /&gt;//Remove redundant columns&lt;BR /&gt;Remove = Table.RemoveColumns(Sort,{"Count", "MainIndex", "Index", "Max"})&lt;BR /&gt;&lt;BR /&gt;in Remove&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your output would be something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;the 3 "NA"'s are the last date for each name...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2024 10:09:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Create-new-column-based-on-values-from-2-rows/m-p/3932143#M129109</guid>
      <dc:creator>ferryv</dc:creator>
      <dc:date>2024-05-20T10:09:27Z</dc:date>
    </item>
  </channel>
</rss>

