Skip to content

Comparing database records

This page compares each record in a table with the record before it. The technique is to add two index columns, one starting at 1 and one at 0, and then merge the table with itself on those columns, so that every row is joined to the row above it.

The example is a list of service desk tickets for several customers. The question is whether a customer opened a new ticket within 7 days of closing their previous one.

An Excel table of six tickets with ID, Customer, Open Date and Close Date columns, for two customers in May 2020

  • Click inside the table, and on Excel’s Data tab click From Table/Range. The data opens in the Power Query Editor. Check that Open Date and Close Date have the Date type.

The tickets in the Power Query Editor, with ID as a whole number, Customer as text, and Open Date and Close Date as dates

  • Sort the table by Customer, and then by Open Date, both ascending. Each customer’s tickets are then together, in the order they were opened.

The tickets sorted by customer and then by open date, with sort arrows numbered 1 and 2 in the Customer and Open Date headers

  • On the Add Column tab, open the Index Column list and choose From 1.
  • Add a second index column, choosing From 0 this time.

On every row, the second index is one less than the first, so it holds the first index of the row above. This is what links a record to the previous one.

The sorted tickets with an Index column running from 1 to 6 and an Index.1 column running from 0 to 5

  • Rename the second index column Previous.
  • On the Home tab, click Merge Queries. Choose the current query again as the second table, select Previous in the upper table and Index in the lower table, and click OK.

The Merge dialog joining Table1 to itself, with Previous selected in the upper table and Index in the lower, using a Left Outer join that matches 5 of 6 rows

The merge adds a column of tables, named Renamed Columns after the step it merged with. Each table holds the row above.

The query with Index, Previous and a new Renamed Columns column whose cells each contain the word Table

  • Remove the Index and Previous columns, which are no longer needed.
  • Click the expand icon in the Renamed Columns header, tick only Customer and Close Date, and click OK.

The expand menu for the Renamed Columns column, with only Customer and Close Date ticked

  • Rename the expanded Close Date column Previous Close Date. The expanded customer column is called Customer.1.

Each row now shows the customer and close date of the ticket above it. The first row has nulls, because no ticket comes before it.

The query with Customer.1 and Previous Close Date columns holding the previous ticket’s customer and close date, with nulls on the first row

The test is whether the previous ticket belongs to the same customer and closed no more than 7 days before this ticket opened. Comparing the customers matters, because at the boundary between two customers the row above belongs to someone else.

  • On the Add Column tab, click Custom Column, and enter this formula:
= if [Customer] = [Customer.1] and Duration.Days([Open Date] - [Previous Close Date]) <= 7
then "Repeat Ticket" else null

Duration.Days turns the difference between the two dates into a whole number of days.

The Custom Column dialog with a column named Custom and the formula testing for the same customer and a gap of no more than 7 days, returning Repeat Ticket or null

  • Click OK. The new column shows Repeat Ticket where the rule is met, and null elsewhere.

The query with a Custom column showing Repeat Ticket for ticket 2 and ticket 6, and null for the others

  • Remove the Customer.1 and Previous Close Date columns, and click Close & Load to load the result to a table.

The final table marks the two tickets that Cust 1 opened within 7 days of closing an earlier one.

The final query with ID, Customer, Open Date, Close Date and Custom columns, where tickets 2 and 6 are marked Repeat Ticket