Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ninmonkey/b0fa4df9f2e8ef75aba7889ef708cb6c to your computer and use it in GitHub Desktop.
Save ninmonkey/b0fa4df9f2e8ef75aba7889ef708cb6c to your computer and use it in GitHub Desktop.
Get early explicit errors, prevents distinct-errrors from being applied.
/// if conditions fail, don't allow the query
// to silently complete as "successful"
let
Source = Sql.Databases("server"),
nin = Source{ [Name="instance"] }[Data],
PingLog = nin{ [Schema="dbo",Item="PingLog"] }[Data],
// optional function: prints list values as a `csv` without having to drill down
list_to_string = (text_list as list) as text =>
let
seperator = ", ",
template = "{ #{0} }",
combined_string = Text.Combine(text_list, seperator)
in
Text.Format(template, {combined_string}),
// Sorted Rows was the final step before error handling
final_table = #"Sorted Rows",
// column names used for testing whether a table is distinct
// ie: the Table's primary keys
distinct_columns = { "DatetimeUtc", "Destination", "Source" },
final_table_otherwise_fail = if
Table.IsDistinct(
final_table,
distinct_columns
)
then
final_table as table
else
error [
Reason = "ColumnsNotDistinct",
Message = "Required distinct columns found duplicate records",
Detail = "Failed PKs = " & list_to_string( distinct_columns )
]
in
final_table_otherwise_fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment