Skip to content

Dealing with slowly changing Datasets

A query is written against the data as it looks today, but the source keeps changing after that. Columns are added, renamed, dropped or moved, and new values appear in columns that used to hold a fixed set. A dataset whose structure drifts like this is a slowly changing dataset, and a query built on it can break, or worse, keep running and quietly return the wrong answer.

Power Query makes the problem easy to fall into, because the steps it writes from the interface usually name every column they touch. The habits below make a query depend on as little of the source’s current shape as possible.

The cheapest protection is not technical. Agree with whoever produces the data what its structure is, which columns it has and what they are called, and ask to be told before it changes. A short written agreement of this kind, sometimes called a data contract, turns a surprise failure into a planned change.

When you tick items in a column’s filter list, Power Query decides how to write the condition. Depending on what you ticked, it may keep the selected values, with =, or exclude the unselected ones, with <>. On today’s data both give the same rows, but they behave differently when a new value appears.

Suppose a column holds only Red and Yellow, and you want the red rows:

  • [Colour] = "Red" keeps red rows only, whatever new colours arrive later.
  • [Colour] <> "Yellow" also keeps red rows today, but it will let Blue through as soon as blue rows appear.

Check the condition in the formula bar after setting a filter, and ask which way it should treat values that do not exist yet.

Dragging a column to a new position writes a Table.ReorderColumns step that lists every column in the table. If any of those columns is later renamed or removed in the source, the step fails, even if the report never used that column.

Two habits help:

  • Keep only the columns the report needs first, and reorder afterwards, so that the step lists fewer columns and there is less to break.
  • Where only a few columns need a set position, move just those and leave the rest in whatever order they arrive.

The code below moves Customers and Employees to the second and third positions, however many other columns there are and whatever they are called:

Table.ReorderColumns (
#"TableStep",
List.InsertRange (
List.Difference(
Table.ColumnNames(#"TableStep"),
{"Customers", "Employees"}
),
1,
{"Customers", "Employees"}
)
)

It works in three parts:

  1. List.Difference lists all the table’s columns except the two to be moved.
  2. List.InsertRange puts the two back into that list at position 1, which is the second position because positions count from 0.
  3. Table.ReorderColumns applies the new order.

The same logic can be written as a function, so that any table can be reordered with one call:

fnReorderSubsetOfColumns =
(tbl as table, reorderedcolumns as list, offset as number) as table =>
Table.ReorderColumns (
tbl,
List.InsertRange (
List.Difference(
Table.ColumnNames(tbl),
reorderedcolumns
),
offset,
reorderedcolumns
)
)

Called on another table, it moves Suppliers and Stores to the second and third positions:

= fnReorderSubsetOfColumns(#"TableStepNew", {"Suppliers", "Stores"}, 1)

Choose columns by what you keep, or by position

Section titled “Choose columns by what you keep, or by position”

A step that removes named columns fails if the source has already dropped one of them. A step that keeps named columns is safer, because the columns it lists are the ones the report depends on anyway. So, where possible, use Choose Columns or Remove Other Columns rather than Remove Columns.

Where the names themselves are unstable, select columns by position or by a pattern in their names instead. Each of the snippets below works out its list of columns from Table.ColumnNames when the query runs.

List.FirstN takes the first items of a list:

Table.RemoveColumns(Source, List.FirstN(Table.ColumnNames(Source), 2))

List.Last takes the final item of a list:

Table.RemoveColumns(Source, List.Last(Table.ColumnNames(Source), 1))

The second argument of List.Last is the value to return when the list is empty, not a count, so the 1 here does nothing and can be left out.

List.Range takes a list, a starting position counted from 0, and the number of items to return:

Table.SelectColumns(Source, List.Range(Table.ColumnNames(Source), 1, 2))

Keep two columns that are not next to each other

Section titled “Keep two columns that are not next to each other”

Positions in a list are read with {n}, counting from 0, so this keeps the third and sixth columns:

Table.SelectColumns(Source, {Table.ColumnNames(Source){2}, Table.ColumnNames(Source){5}})

Remove columns whose names match a pattern

Section titled “Remove columns whose names match a pattern”

List.Select keeps the items of a list that pass a test. This removes every column whose header contains Random:

= Table.RemoveColumns(#"Source", List.Select(Table.ColumnNames(#"Source"), each Text.Contains (_, "Random")))

The same result comes from keeping the columns that fail the test:

= Table.SelectColumns(#"Source", List.Select(Table.ColumnNames(#"Source"), each not Text.Contains (_, "Random")))

Renaming makes a report easier to read, but a rename step written from the interface names the old columns, so it fails as soon as one of them is renamed or removed at the source:

Table.RenameColumns(#"Source", {{"Random Column 1", "New Name 1"}, {"Random Column 2", "New Name 2"}})

Where the positions are more stable than the names, a function can rename columns by position instead:

fnRenameColumnByIndices =
let
Source = (Source as table, ColumnNamesNew as list, Indices as list) =>
let
ColumnNamesOld = List.Transform(Indices, each Table.ColumnNames(Source){_} ),
ZippedList = List.Zip( { ColumnNamesOld, ColumnNamesNew } ),
#"Renamed Columns" = Table.RenameColumns(Source, ZippedList)
in
#"Renamed Columns"
in
Source

The function takes three arguments:

  • Source, the table whose columns are renamed
  • ColumnNamesNew, the list of new names
  • Indices, the positions of the columns to rename, counting from 0

It looks up the current name at each position, pairs each old name with its new one using List.Zip, and passes the pairs to Table.RenameColumns.

The lists passed in can be generated rather than typed. This call renames the columns at positions 6 to 12 to New Column Name 1 to New Column Name 7:

= fnRenameColumnByIndices(#"TableStep", List.Transform({1..7}, each "New Column Name " & Text.From(_)), {6..12})

Written out in full, that call is:

= fnRenameColumnByIndices(#"TableStep", {"New Column Name 1", "New Column Name 2", "New Column Name 3", "New Column Name 4", "New Column Name 5", "New Column Name 6", "New Column Name 7"}, {6..12})

Split Column by Delimiter into columns fixes the number of new columns from the data it sees when the step is written. If a later row holds more items than that, the extra items are dropped without any error. For example, if a column was split into two columns on rows like green, yellow, a later row holding red, blue, green loses green.

The step that splits into a fixed set of columns looks like this, where Products is the column being split:

#"Split Column by Delimiter" = Table.SplitColumn(#"PreviousStep", "Products", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Product.1", "Product.2"}),

Splitting into rows instead keeps every item, however many there are. In the Split Column by Delimiter dialog, open Advanced options and choose Rows, which writes a step like this:

#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"PreviousStep", {{"Products", Splitter.SplitTextByDelimiter(", ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Products"),

Each row is repeated once for every item in its Products value, with one item on each copy. The two examples use different delimiters, a comma and a comma followed by a space, so match the delimiter to your own data.

Merging has the same weakness as reordering, because the step names the columns it merges. Passing the names in as a list, here called ColumnsToMerge, keeps them out of the step itself:

#"Merged Columns" = Table.CombineColumns (
Table.TransformColumnTypes (
Source,
List.Transform (
ColumnsToMerge,
each {_, type text}
),
"en-US"
),
ColumnsToMerge,
Combiner.CombineTextByDelimiter(":", QuoteStyle.None),
"Merged")

The inner Table.TransformColumnTypes turns every listed column into text first, because the combiner joins text and would fail on numbers or dates. List.Transform builds the pairs of column name and type that it needs. As in the earlier examples, the curly quotation marks need replacing with straight ones.

Power Query often adds a Changed Type step by itself, near the start of a new query. Like the other generated steps, it names every column, so it is the step most likely to fail when a column changes.

Data types are still worth setting, because a typed column catches bad data on refresh, such as a date that is not a valid date or text in a number column. That check would be hard to make any other way. The answer is to keep the step but move it:

  • Delete any Changed Type steps that Power Query adds early on.
  • Set types once, as near the end of the query as possible, after the columns the report does not need have been removed.
  • ETL best practices, a set of habits for designing and building queries
  • Reordering columns and the pages that follow it, which cover renaming, reordering, removing, splitting and merging columns from the interface