Forum Discussion
Comparing two tables columns by columns
- Anonymous1 year ago
Hi divumj ,
You can ttry the following codelet // Load Table 1 Source1 = #"Table 1", // Load Table 2 and rename columns to avoid conflict Source2 = Table.RenameColumns(#"Table 2", List.Transform(Table.ColumnNames(#"Table 2"), each {_, _ & "2"})), // Merge Tables MergedTables = Table.NestedJoin(Source1, "Unique", Source2, "Unique2", "Table2", JoinKind.Inner), // Expand the merged table ExpandedTable = Table.ExpandTableColumn(MergedTables, "Table2", Table.ColumnNames(Source2)), // Get the list of columns to compare ColumnsToCompare = List.RemoveItems(Table.ColumnNames(Source1), {"Unique"}), // Function to compare columns CompareColumns = (table as table, columns as list) as table => List.Accumulate( columns, table, (state, current) => Table.AddColumn( state, "Compare_" & current, each if Record.Field(_, current) = Record.Field(_, current & "2") then "Match" else "Mismatch" ) ), // Apply the comparison function Result = CompareColumns(ExpandedTable, ColumnsToCompare), // Remove unnecessary columns FinalResult = Table.SelectColumns(Result, List.Combine({{"Unique"}, List.Transform(ColumnsToCompare, each "Compare_" & _)})) in FinalResultFinal output
Best regards,
Albert HeIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly
It depends a bit on what you would see in case of differences.
Some approaches coming to mind:
- Do a merge on all columns full outer join. Expand the table and check for null in any field that should never have null and you have a discrepancy. Hard to diagnose where it is though.
- Or add an index column to both tables, unpivot all columns except the index column, Do an inner join on the index and attribute columns. Anywhere the 2 values are not equal you have a discrpancy. Will give a mess when you are missing a record somewhere.
- Or, same as last, but use an existing unique key instead of the index. Missing records are easier found.
I hope this gives you some ideas...
PwerQueryKees Thanks I am almost doing same thing...but problem is our main focus is to not just identify records but also to look for any unwanted data transformation in any of the column...for which I have created a calculated filed in merge table...Colum a = table2.column a.
To write this formula I have literary type columns name one by one, which is very time consuming and I have to create multiple transformation on same grounds.