Created
July 8, 2016 13:46
-
-
Save kitmenke/ff9398091c5fe7669c564316a6c4bdda to your computer and use it in GitHub Desktop.
Script to create SSAS partions for a table in a tabular model, refresh all of the partitions, and/or print all partitions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------------------ | |
# | |
# NAME: CreatePartitions.ps1 | |
# AUTHOR: Kit Menke | |
# DATE: 2016-05-13 | |
# | |
# COMMENTS: | |
# Create SSAS partions for a table in a tabular model. | |
# Must be run with sqlps. | |
# | |
# USAGE: | |
# -create: For every partition in the list $AS_PARTITIONS, create a new partition using query defined in $AS_QUERY. | |
# -print: List all the partitions | |
# -refresh: Find any partition with a status of NoData and schedule a Full refresh. | |
# | |
# EXAMPLE: | |
# sqlps | |
# cd wherever | |
# .\createPartitions.ps1 -print | |
# ------------------------------------------------------------------------ | |
#parameters | |
param( | |
[switch] $create, | |
[switch] $refresh, | |
[switch] $print | |
) | |
#global variables | |
# server name | |
$AS_SERVER = "MYSERVER\TABULAR" | |
# database name | |
$AS_DATABASE = "MyTabularDB" | |
# datasource name (was defined when the model was created in Visual Studio) | |
$AS_DATASOURCE = "SqlAzure example.database.windows.net DB_PRD" | |
# tabular model table name | |
$AS_TABLE = "FACT MyTable" | |
# query to use as a template for each partition | |
$AS_QUERY = @" | |
select a, b, c, d | |
from factmytable | |
where date = '{0}'; | |
"@ | |
# list of partitions which will be created in the tabular model | |
$AS_PARTITIONS = "2015-11-16","2015-11-17","2015-11-18","2015-11-19","2015-11-20","2015-11-21","2015-11-22","2015-11-23","2015-11-24","2015-11-25","2015-11-26","2015-11-27","2015-11-28","2015-11-29","2015-11-30","2015-12-01","2015-12-02","2015-12-03","2015-12-04","2015-12-05","2015-12-06","2015-12-07","2015-12-08","2015-12-09","2015-12-10","2015-12-12","2015-12-13","2015-12-14","2015-12-15","2015-12-16","2015-12-17","2015-12-18","2015-12-19","2015-12-20","2015-12-21","2015-12-22","2015-12-23","2015-12-24","2015-12-25","2015-12-26","2015-12-27","2015-12-28","2015-12-29","2015-12-30","2015-12-31","2016-01-01","2016-01-02","2016-01-03","2016-01-04","2016-01-05","2016-01-06","2016-01-07","2016-01-08","2016-01-09","2016-01-10","2016-01-11","2016-01-12","2016-01-13","2016-01-14","2016-01-15","2016-01-16","2016-01-17","2016-01-18" | |
# require sql powershell | |
#import-module sqlps | |
# Original function from https://msdn.microsoft.com/en-us/library/mt712543.aspx | |
# Modified by Kit Menke | |
# Creates an Analysis Services Tabular partition | |
function Create-ASPartition() | |
{ | |
param( | |
[Microsoft.AnalysisServices.Tabular.Table] $table, | |
[Microsoft.AnalysisServices.Tabular.DataSource] $dataSource, | |
[string] $name, | |
[string] $query) | |
$partition = New-Object -TypeName Microsoft.AnalysisServices.Tabular.Partition; | |
$partition.Source = New-Object -TypeName Microsoft.AnalysisServices.Tabular.QueryPartitionSource; | |
$partition.Source.DataSource = $dataSource; | |
$partition.Source.Query = $query; | |
$partition.Name = $name; | |
$partition.Mode = [Microsoft.AnalysisServices.Tabular.ModeType]::Default; | |
$partition.DataView = [Microsoft.AnalysisServices.Tabular.DataViewType]::Default; | |
$table.Partitions.Add($partition); | |
} | |
function App-Create() | |
{ | |
write-host "Creating partitions..." | |
foreach ($partition in $AS_PARTITIONS) | |
{ | |
$query = $AS_QUERY -f $partition | |
write-host ("Creating partition for {0} with query {1}" -f $partition,$query) | |
Create-ASPartition $table $datasource $partition $query | |
} | |
$result = $model.SaveChanges() | |
if (-Not $result.Impact.IsEmpty) | |
{ | |
$result.Impact | |
} | |
else | |
{ | |
write-host "No changes... Something went wrong..." | |
} | |
} | |
function App-Refresh() | |
{ | |
$count = 0 | |
write-host (Get-Date -format s) "Refreshing partitions..." | |
foreach ($partition in $AS_PARTITIONS) | |
{ | |
write-host ("Checking partition {0} " -f $partition) | |
$partition = $table.Partitions.Find($partition) | |
if ($partition.State -eq [Microsoft.AnalysisServices.Tabular.ObjectState]::NoData) | |
{ | |
write-host (Get-Date -format s) ("Requesting refresh for partition {0} " -f $partition) | |
$partition.RequestRefresh([Microsoft.AnalysisServices.Tabular.RefreshType]::Full) | |
$count = $count + 1 | |
} | |
if ($count -gt 6) | |
{ | |
write-host (Get-Date -format s) "Saving..." | |
$result = $model.SaveChanges() | |
$result.Impact | |
$count = 0 | |
} | |
} | |
if ($count -gt 0) | |
{ | |
write-host (Get-Date -format s) "Saving..." | |
$result = $model.SaveChanges() | |
$result.Impact | |
$count = 0 | |
} | |
} | |
function App-Print() | |
{ | |
write-host (Get-Date -format s) ("Printing {0} partitions..." -f $table.Partitions.Count) | |
$table.Partitions | Select Name,State | |
} | |
function App-Main() | |
{ | |
write-host (Get-Date -format s) ("Connecting to {0}..." -f $AS_SERVER) | |
$as = New-Object Microsoft.AnalysisServices.Server | |
$as.Connect($AS_SERVER) | |
write-host ("Database: {0}, Data Source: {1}, Table: {2}" -f $AS_SERVER,$AS_DATASOURCE,$AS_TABLE) | |
# get the database | |
$db = $as.Databases.FindByName($AS_DATABASE) | |
$model = $db.Model | |
# get the datasource | |
$dataSource = $model.DataSources.Find($AS_DATASOURCE) | |
# get the table | |
$table = $model.Tables.Find($AS_TABLE) | |
if ($create) | |
{ | |
App-Create | |
} | |
if ($refresh) | |
{ | |
App-Refresh | |
} | |
if ($print) | |
{ | |
App-Print | |
} | |
$as.Disconnect() | |
write-host (Get-Date -format s) "Complete!" | |
} | |
# Run the program | |
App-Main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment