Power Query: Copy earliest date/timestamp for connected rows to each of those rows

Lexer

New Member
Joined
Oct 3, 2016
Messages
3
Hi,

Please see attached mini sheet. I am trying to transform a table via Power Query. The input data can be seen in the columns A to E and the desired output from Power Query are the same columns, but also including column F.

The table rows in the mini sheet are sorted (partially manually) for to better illustrate how rows are related. However, in real data sets, the rows are not sorted. I have formulas in column K and L which create the output I want to see in column F. These formulas work fine with small data samples, but the workbook becomes too slow with larger data sets.

Some explanation of the principle behind the data generated in the table:

The table contains database entries. I have only included those columns which are relevant for solving the problem at hand. When a new row is entered into the database, it will get the value "false" in column C. The value "false" simply means that the entry hasn’t (yet) been revised; that all columns (many of which haven’t been included in this sample) retain its original entry. This will also be evident in that the date/timestamp in column E, named Changed, is equal to the date/timestamp in column D, named Created.

However, sometimes a database entry will be revised. What happens then is that the originally entered row will be kept as it is, except that the value in column C will be changed to "true", signifying that the row has been changed. Also, the date/timestamp in column E will be changed to the date/timestamp that the change happened.

Changing a row like this will automatically create a new row in the database, which contains the altered information. This new row will have the value "false" in column C, like the originally entered row once had, and the date/timestamp in column D will equal the date/timestamp in column E. Notably, the date/timestamp in column D named Created for this row will be equal to the date/timestamp in column E named Changed for the row that was altered, since the new row was created in the same instant that the original row was altered. This date/timestamp relationship makes it possible to identify that these two rows in the table are indeed related and should be grouped together.

If there are several revisions for a specific database entry, it is the latest row for this entry, containing the value "false" in column C and the most recent date/timestamp in columns D and E, that will be revised, leaving behind yet another row with the value "true" in column C, as explained earlier. Thus, there may be several rows with the value "true" in column C that are chronologically linked to the final entry, for which the value in column C will always be "false".

I am trying to use Power Query to group those items from column A and column B which have an unbroken relationship through date/timestamps as explained above, find the earliest related date/timestamp for these, and copy that date/timestamp to each row for the group in a new column.

Here is the formula that, for each row within a specific group, identifies the row containing the earliest date/timestamp for that group:

Excel Formula:
=IF(SUMPRODUCT(($A$2:$A$42=$A2)*($B$2:$B$42=$B2)*($C$2:$C$42="True")*(E$2:E$42=$D2)*(ROW($C$2:$C$42)<>ROW()))<>0,INDEX($K$1:$K$42,AGGREGATE(14,6,ROW($C$2:$C$42)/(($A$2:$A$42=$A2)*($B$2:$B$42=$B2)*(C$2:$C$42="True")*($E$2:$E$42=$D2)),1)),ROW()-ROW($K$1))

Here is the formula for retrieving the relevant date/timestamp based on the rows identified with the previous formula:

Excel Formula:
=INDEX($D$2:$D$42,$K2,)


Power Query timestamp lookup.xlsx
ABCDEFGHIJKLM
1BatchCodeRow changed (true = line has one or more successors and may have one or more predeccessors. false = line has no successors, but may have one ore more predeccessors).Created (timestamp will be equal to immediate predecessor's (if any) "Changed" timestamp)Changed (timestamp will be equal to immediate successor's (if any) "Created" timestamp)Desired result from Power Query operationsTable rowsTable rows which contain correct results in "Created" column (reflected in column "Desired result from Power Query operations"Hierarchy for colored example rows (1 = oldest/ original)FORMULA for traced back rowsFORMULA for traced back datesTest
2123456AAfalse12.11.2018 09:38:2012.11.2018 09:38:2012.11.2018 09:38:2011112.11.2018 09:38:20TRUE
3123456AAfalse12.11.2018 09:38:2112.11.2018 09:38:2112.11.2018 09:38:2122212.11.2018 09:38:21TRUE
4123456BBfalse12.11.2018 09:38:5912.11.2018 09:38:5912.11.2018 09:38:5933312.11.2018 09:38:59TRUE
5123456BBfalse12.11.2018 09:38:5912.11.2018 09:38:5912.11.2018 09:38:5955412.11.2018 09:38:59TRUE
6123457AAfalse12.11.2018 09:41:2512.11.2018 09:41:2512.11.2018 09:41:2566512.11.2018 09:41:25TRUE
7123457AAfalse12.11.2018 09:41:2512.11.2018 09:41:2512.11.2018 09:41:253737612.11.2018 09:41:25TRUE
8123457BBfalse12.11.2018 09:41:2512.11.2018 09:41:2512.11.2018 09:41:2577712.11.2018 09:41:25TRUE
9123457BBtrue12.11.2018 09:41:2512.11.2018 09:47:2712.11.2018 09:41:2588I_1812.11.2018 09:41:25TRUE
10123457BBfalse12.11.2018 09:47:2712.11.2018 09:47:2712.11.2018 09:41:2598I_2812.11.2018 09:41:25TRUE
11123458CCfalse12.11.2018 09:41:2512.11.2018 09:41:2512.11.2018 09:41:2510101012.11.2018 09:41:25TRUE
12123458CCfalse12.11.2018 10:48:4412.11.2018 10:48:4412.11.2018 10:48:4416161112.11.2018 10:48:44TRUE
13123458DDfalse12.11.2018 10:44:5412.11.2018 10:44:5412.11.2018 10:44:5412121212.11.2018 10:44:54TRUE
14123458DDfalse12.11.2018 10:48:4412.11.2018 10:48:4412.11.2018 10:48:4418181312.11.2018 10:48:44TRUE
15123459CCfalse12.11.2018 10:48:4412.11.2018 10:48:4412.11.2018 10:48:4439391412.11.2018 10:48:44TRUE
16123459CCfalse12.11.2018 10:50:2912.11.2018 10:50:2912.11.2018 10:50:2921211512.11.2018 10:50:29TRUE
17123459DDtrue12.11.2018 10:46:0012.11.2018 10:47:0012.11.2018 10:46:001515III_11612.11.2018 10:46:00TRUE
18123459DDtrue12.11.2018 10:47:0012.11.2018 10:47:0112.11.2018 10:46:002915III_21612.11.2018 10:46:00TRUE
19123459DDtrue12.11.2018 10:47:0112.11.2018 10:51:3612.11.2018 10:46:001915III_31612.11.2018 10:46:00TRUE
20123459DDtrue12.11.2018 10:51:3612.11.2018 10:58:4112.11.2018 10:46:002215III_41612.11.2018 10:46:00TRUE
21123459DDfalse12.11.2018 10:58:4112.11.2018 10:58:4112.11.2018 10:46:002615III_51612.11.2018 10:46:00TRUE
22123459DDfalse12.11.2018 10:52:2212.11.2018 10:52:2212.11.2018 10:52:2225252112.11.2018 10:52:22TRUE
23123459DDtrue12.11.2018 11:00:3612.11.2018 11:01:4112.11.2018 11:00:362323IV_12212.11.2018 11:00:36TRUE
24123459DDfalse12.11.2018 11:01:4112.11.2018 11:01:4112.11.2018 11:00:362423IV_22212.11.2018 11:00:36TRUE
25123459DDfalse12.11.2018 11:58:4312.11.2018 11:58:4312.11.2018 11:58:4314142412.11.2018 11:58:43TRUE
26123460EEtrue12.11.2018 10:36:2212.11.2018 10:36:5612.11.2018 10:36:2234342512.11.2018 10:36:22TRUE
27123460EEfalse12.11.2018 10:50:5212.11.2018 10:50:5212.11.2018 10:50:5228282612.11.2018 10:50:52TRUE
28123460EEfalse12.11.2018 10:51:3612.11.2018 10:51:3612.11.2018 10:51:3635352712.11.2018 10:51:36TRUE
29123460FFfalse12.11.2018 10:51:3612.11.2018 10:51:3612.11.2018 10:51:3631312812.11.2018 10:51:36TRUE
30123460FFfalse12.11.2018 10:51:3612.11.2018 10:51:3612.11.2018 10:51:3632322912.11.2018 10:51:36TRUE
31123461EEtrue12.11.2018 10:51:3612.11.2018 10:51:3612.11.2018 10:51:362020II_13012.11.2018 10:51:36TRUE
32123461EEfalse12.11.2018 10:51:3612.11.2018 10:51:3612.11.2018 10:51:361320II_23012.11.2018 10:51:36TRUE
33123461GGfalse12.11.2018 10:51:4712.11.2018 10:51:4712.11.2018 10:51:4736363212.11.2018 10:51:47TRUE
34123461HHfalse13.11.2018 07:26:1813.11.2018 07:26:1813.11.2018 07:26:1838383313.11.2018 07:26:18TRUE
35123461IIfalse12.11.2018 09:15:1312.11.2018 09:15:1312.11.2018 09:15:1340403412.11.2018 09:15:13TRUE
36123461JJfalse12.11.2018 09:15:1312.11.2018 09:15:1312.11.2018 09:15:1311113512.11.2018 09:15:13TRUE
37123462AAfalse12.11.2018 09:41:2512.11.2018 09:41:2512.11.2018 09:41:2530303612.11.2018 09:41:25TRUE
38123463GGfalse12.11.2018 10:44:5412.11.2018 10:44:5412.11.2018 10:44:5417173712.11.2018 10:44:54TRUE
39123466PPfalse12.11.2018 09:41:2512.11.2018 09:41:2512.11.2018 09:41:25443812.11.2018 09:41:25TRUE
40123467KKfalse12.11.2018 10:48:4412.11.2018 10:48:4412.11.2018 10:48:4427273912.11.2018 10:48:44TRUE
41123475EEfalse12.11.2018 10:51:3612.11.2018 10:51:3612.11.2018 10:51:3641414012.11.2018 10:51:36TRUE
42123475MMfalse12.11.2018 10:51:4712.11.2018 10:51:4712.11.2018 10:51:4733334112.11.2018 10:51:47TRUE
Example2
Cell Formulas
RangeFormula
K2:K42K2=IF(SUMPRODUCT(($A$2:$A$42=$A2)*($B$2:$B$42=$B2)*($C$2:$C$42="True")*(E$2:E$42=$D2)*(ROW($C$2:$C$42)<>ROW()))<>0,INDEX($K$1:$K$42,AGGREGATE(14,6,ROW($C$2:$C$42)/(($A$2:$A$42=$A2)*($B$2:$B$42=$B2)*(C$2:$C$42="True")*($E$2:$E$42=$D2)),1)),ROW()-ROW($K$1))
L2:L42L2=INDEX($D$2:$D$42,$K2,)
M2:M42M2=L2=F2



I hope someone will be able to help me out with this!


Best regards,
Lexer
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Something like
- Group By Batch Code
- Aggregate MIN of Created
- Aggregate ALL rows ( to keep all details)
- Expand All rows (choose columns you'd need in the end result)
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Batch", Int64.Type}, {"Code", type text}, {"Created", type datetime}, {"Changed", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Batch"}, {{"First Created", each List.Min([Created]), type nullable datetime}, {"All", each _, type table [Batch=nullable number, Code=nullable text, Created=nullable datetime, Changed=nullable datetime]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Code", "Created", "Changed"}, {"Code", "Created", "Changed"})
in
    #"Expanded All"
 
Upvote 0
Hi GraH, and thanks!

Rows with the same Batch and Code doesn't belong to the same group unless the other criteria are met too. As an example, look at rows with Batch = 123459 and Code DD. Rows with these values fall into one of four groupings in this example: 1) Five rows marked yellow, 2) One stand-alone row (white), 3) Two rows marked red and 4) One stand-alone row (white). Each of these groups will have their own MIN date/timestamp.

You can see how e.g. the five rows marked in yellow belong together this way: They have the same Batch and Code values. All rows have the value "true" in Row Changed, except one single row which contains the most recent date/timestamp of this grouping, which has the value "false". Lastly, these rows can be traced/linked by comparing the date/timestamp in the Created column with the date/timestamp in the Changed column of the preceding row, as these date/timestamps will be the exact same. Thus, there will be a "zig-zag" pattern going backwards in time. If you look at the other rows which also have Batch = 123459 and Code DD, none of them can be traced/linked in this way to this grouping. (They may however belong to other groupings, as described).

Best regards,
Lexer
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top