Skip to content

Instantly share code, notes, and snippets.

@otykier
Last active August 29, 2023 08:48
Show Gist options
  • Save otykier/de42cdbf30225ccbaf7f1fc67d357f3f to your computer and use it in GitHub Desktop.
Save otykier/de42cdbf30225ccbaf7f1fc67d357f3f to your computer and use it in GitHub Desktop.
Convert legacy (SQL) partitions to M (Power Query)
// This Tabular Editor script will convert all legacy (SQL) partitions on a model, to corresponding M
// (Power Query) partitions, that use the Value.NativeQuery function on the original SQL expression.
// The script can be used in both TE2 and TE3.
const string mQuery = "let\n Source = #\"{0}\",\n Data = Value.NativeQuery(Source, \"{1}\")\nin\n Data";
foreach(var table in Model.Tables.Where(t => t.SourceType == PartitionSourceType.Query))
{
table.Partitions.ConvertToPowerQuery();
foreach(MPartition partition in table.Partitions)
{
partition.Expression = string.Format(mQuery,
Model.DataSources.First().Name,
partition.Expression.Replace("\"", "\"\""));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment