Skip to content

Data Cop! - detecting revenue changes

A revenue dataset is suspected of having been tampered with. There is a trusted copy from before the change, and the question is what is different. Two kinds of change are suspected:

  1. Some revenue figures have been altered.
  2. Some rows have been added.

The dataset runs to nearly 100,000 rows, so checking it by eye is out of the question, and lookup formulas filled down both files would be slow and fragile. Power Query can answer both questions with a merge each, because a merge compares two tables on whichever columns you choose.

The two files are the trusted original and the suspect copy:

A folder listing Compromised Revenue File.xlsx and Original Revenue File.xlsx

  1. On the Data tab, choose Get Data, then From File, then From Workbook, and select Original Revenue File.xlsx.

The original workbook in the editor, listing Sheet1 and a table called Revenues

  1. Open the Revenues table.

The original revenue data, with Date, Color, Supplier Category, Supplier Name, Customer Category and City columns

  1. Name the query Correct, then choose Close & Load To and Only Create Connection.

Repeat the same steps for Compromised Revenue File.xlsx, and name that query Compromised.

The compromised workbook in the editor, also listing Sheet1 and a table called Revenues

The compromised revenue data, which looks the same as the original in its first rows

Every column except Revenue describes what a row is about: when, which product colour, which supplier and which customer. Together those columns identify a row, so they are the columns to match the two tables on.

  1. Select the Correct query, and on the Home tab choose Merge Queries, then Merge Queries as New.
  2. In the top table, select Date, Color, Supplier Category, Supplier Name, Customer Category and City, holding Ctrl to add each one.
  3. In the bottom table, choose Compromised and select the same columns in the same order. The small numbers beside the headers show the order, and the two tables are paired column by column on it.
  4. Set Join Kind to Inner (only matching rows), which keeps only the rows that appear in both tables.

The Merge dialog matching Correct with Compromised on Date, Color, Supplier Category, Supplier Name, Customer Category and City, numbered 1 to 6 in both tables, with an inner join that matched 96,035 of the first 96,035 rows

The result has every column of Correct, plus a Compromised column holding the matching row from the other table.

The merged query, with City and Revenue from the original and a Compromised column holding a table on each row

Click the expand button on Compromised, clear every column except Revenue, and leave Use original column name as prefix ticked. The prefix keeps the two revenue columns apart, as Revenue and Compromised.Revenue.

The two revenue columns side by side, Revenue and Compromised.Revenue, agreeing on every visible row

Click fx to add a step, and enter:

= Table.SelectRows(#"Expanded Compromised", each [Revenue] <> [Compromised.Revenue] )

Table.SelectRows keeps the rows for which the test after each is true, which here means the rows where the two revenue figures disagree. One step compares every row, however many there are.

The filtered rows, where the original revenue figures sit beside altered ones such as -1000, 0 and 10000000

Choose Close & Load to list the altered rows on a worksheet.

The worksheet of altered rows, showing each row’s date, colour, supplier, customer category and city, with the original revenue beside the compromised figure

An added row has no partner in the trusted copy, so the inner join above drops it without trace. Finding it needs a different join kind.

  1. Select Correct again and choose Merge Queries, then Merge Queries as New.
  2. Select the same six columns in both tables, in the same order as before.
  3. Set Join Kind to Right Anti (rows only in second). This keeps the rows of the second table, Compromised, that have no match in the first.

The Merge dialog matching the same six columns, this time with Right Anti (rows only in second) as the join kind

Because none of the kept rows has a match in Correct, the columns that come from Correct are all null. The added rows sit inside the Compromised column.

The result of the anti join, with null in every column from Correct and one table in the Compromised column

Right-click the Compromised column and choose Remove Other Columns.

The query reduced to the single Compromised column

Then expand it, keeping all of its columns.

The expanded rows, three rows dated 11/04/2018 whose text fields spell out a message from the intruder, two with a revenue of 0 and one with 1234567890

Choose Close & Load to list the added rows on a worksheet.

The worksheet listing the three added rows

The two merges answer the two questions asked, but they are not the only things that could have changed. Some further checks follow the same pattern:

  • Deleted rows. A Left Anti (rows only in first) join, with the same six columns, lists the rows of Correct that are missing from Compromised.
  • Altered descriptive fields. If someone changed a city or a supplier name rather than a revenue figure, the row no longer matches on the six columns, so it shows up twice: once in the right anti join as an added row, and once in the left anti join as a deleted one. Rows that appear in both lists with the same revenue are worth reading side by side.
  • Duplicate keys. The comparison assumes that the six columns identify each row once. If two rows share the same six values, the inner join pairs each with both, so it is worth grouping each table by those columns and counting the rows before trusting the results.