How can we create SSAS 2008 cube partitions using Powershell?
To start creating partitions, we need to go to the Partitions tab in the cube editor. There is a default partition that will contain all the data for the measure group. I already created an aggregation design and assigned it to the partition.
Partitions are used by Microsoft SQL Server SQL Server Analysis Services to manage and store data and aggregations for a measure group in a cube. Every measure group has at least one partition; this partition is created when the measure group is defined.
This adds a partition to the Adventure Works DW 2008R2 cube (specifically the Internet Customers measure group in the Adventure Works cube):
$server_name = "localhost"
$catalog = "Adventure Works DW 2008R2"
$cube = "Adventure Works"
$measure_group = "Fact Internet Sales"
$old_partition = "Customers_2004"
$new_partition = "Customers_2009"
$old_text = "'2008"
$new_text = "'2009"
[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.AnalysisServices.DLL")
$srv = new-object Microsoft.AnalysisServices.Server
$srv.Connect("Data Source=" + $server_name)
$new_part = $srv.Databases[$catalog].Cubes[$cube].MeasureGroups[$measure_group].Partitions[$old_partition].Clone()
$new_part.ID = $new_partition
$new_part.Name = $new_partition
$new_part.Source.QueryDefinition = $new_part.Source.QueryDefinition.Replace($old_text, $new_text)
$srv.Databases[$catalog].Cubes[$cube].MeasureGroups[$measure_group].Partitions.Add($new_part)
$srv.Databases[$catalog].Cubes[$cube].MeasureGroups[$measure_group].Partitions[$new_partition].Update()
$srv.Databases[$catalog].Update()
$srv.Disconnect()
You'll have to change variables up top, and the reference to the Microsoft.AnalysisServices.dll
assembly, but other than that, this will work peachy keen.
The trick is to call Update()
on the object changed and then on the whole database itself.
If you'd like to process the new partition, as well, you can do that with the following line before $srv.Disconnect
:
$srv.Databases[$catalog].Cubes[$cube].MeasureGroups[$measure_group].Partitions[$new_partition].Process()
You can learn more about Analysis Management Objects (AMO) here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With