<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.adatis.co.uk/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'Master Data Management', 'Analysis Services', 'MDX', 'MDS', and 'MDM'</title><link>http://blogs.adatis.co.uk/search/SearchResults.aspx?o=DateDescending&amp;tag=Master+Data+Management,Analysis+Services,MDX,MDS,MDM&amp;orTags=0</link><description>Search results matching tags 'Master Data Management', 'Analysis Services', 'MDX', 'MDS', and 'MDM'</description><dc:language>en-US</dc:language><generator>CommunityServer 2007 SP2 (Build: 20611.960)</generator><item><title>Managing SSAS Named Sets with Master Data Services Collections</title><link>http://blogs.adatis.co.uk/blogs/jeremykashel/archive/2011/11/07/managing-ssas-named-sets-with-master-data-services-collections.aspx</link><pubDate>Mon, 07 Nov 2011 17:04:00 GMT</pubDate><guid isPermaLink="false">8d7d37f8-4a66-4c95-9fba-293fa87607dc:10006</guid><dc:creator>Jeremy Kashel</dc:creator><description>&lt;p&gt;Master Data Services &lt;a href="http://msdn.microsoft.com/en-us/library/ee633733.aspx" target="_blank"&gt;Collections&lt;/a&gt; are probably not the first feature that come to mind when you think of the MDS product. The hierarchies and member grids tend to be the core functionality, but as this post will hopefully show, MDS Collections are useful also.&lt;/p&gt;  &lt;p&gt;Collections are essentially managed lists of members that can come from multiple different MDS explicit hierarchies, or also from another collection. The idea is that this “subset of members” can be maintained in MDS by a business user and then fed to external applications for reporting or other purposes.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Analysis Services Named Sets&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;One example of how collections can be used is to maintain Analysis Services named sets. Some named sets, such as a Top 50 Customers, don&amp;#39;t require any maintenance, as it’s just the top 50 of all customers, based on a particular measure. On the other hand, sometimes named sets can be static lists, e.g. a list of core products that the user wants to see for reporting.&lt;/p&gt;  &lt;p&gt;In the latter example, if a user wants the definition of a named set to change, they have to get IT to change the MDX script. MDS collections can help by allowing the user to control the named set definition, reducing the burden on IT.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example Scenario&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;Here’s an example of how this is done. First of all, the end game is that I have a named set in Analysis Services that is currently just for 3 products. Therefore, a user can easily drop this set into an Excel report to get quick access to the products that are important to them:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.adatis.co.uk/blogs/jeremykashel/image_3AD09CE1.png"&gt;&lt;img style="border:0px currentColor;padding-top:0px;padding-right:0px;padding-left:0px;display:inline;background-image:none;" title="image" border="0" alt="image" src="http://blogs.adatis.co.uk/blogs/jeremykashel/image_thumb_6132802C.png" width="343" height="114" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;So the challenge is that we need to find some way of extending this named set without doing it manually. This is where MDS starts to come in, so using the sample Product model that comes with MDS, I’ve created a new collection called Favourite Products, as shown in the image below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.adatis.co.uk/blogs/jeremykashel/image_0008F40B.png"&gt;&lt;img style="border:0px currentColor;padding-top:0px;padding-right:0px;padding-left:0px;display:inline;background-image:none;" title="image" border="0" alt="image" src="http://blogs.adatis.co.uk/blogs/jeremykashel/image_thumb_033ADBF3.png" width="454" height="173" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;If I go and edit the Favourite Products collection in MDS, then I can drag and drop more products into this collection, or remove some existing members. In this case, I’ve chosen to add two new products:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.adatis.co.uk/blogs/jeremykashel/image_01F64314.png"&gt;&lt;img style="border:0px currentColor;padding-top:0px;padding-right:0px;padding-left:0px;display:inline;background-image:none;" title="image" border="0" alt="image" src="http://blogs.adatis.co.uk/blogs/jeremykashel/image_thumb_1D2E9C15.png" width="380" height="172" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;C#, AMO and the MDS API&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;So the named set represents the target that we need to get to, whereas the MDS collection that’s shown is the source. To get the contents of the MDS collection to update the named set, one way of doing it is to use the MDS API to pick up the collection members, and then to use AMO in order to write the named set into the MDX script. I’m just doing this in a C# windows application, but you could do it via a batch process, such as SSIS. For this post I’m just going to show the code, so here goes:&lt;/p&gt;  &lt;p&gt;This post is already starting to feel too long so I’m not going to show the basics of the MDS API. For that, take a look at a good posting by the MDS Team blog &lt;a href="http://sqlblog.com/blogs/mds_team/archive/2010/01/12/getting-started-with-the-web-services-api-in-sql-server-2008-r2-master-data-services.aspx" target="_blank"&gt;here&lt;/a&gt;. Also, as anyone who knows me will no doubt agree, I don’t tend to get involved in doing C#, so don’t consider this to be production ready! It should give you an idea of the basics though. Anyway, assuming that we’re now connected to the MDS Web Service, I’ve created the following method that will return the members from the collection:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span&gt;HierarchyMembers &lt;/span&gt;ObtainHierarchyMembers(&lt;span style="color:blue;"&gt;string &lt;/span&gt;entityId, &lt;span style="color:blue;"&gt;string &lt;/span&gt;hierarchyId, &lt;span style="color:blue;"&gt;string &lt;/span&gt;modelId, &lt;span style="color:blue;"&gt;string &lt;/span&gt;versionId)
{
    &lt;span&gt;HierarchyMembersGetRequest &lt;/span&gt;request = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;HierarchyMembersGetRequest&lt;/span&gt;();
    &lt;span&gt;HierarchyMembersGetResponse &lt;/span&gt;response = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;HierarchyMembersGetResponse&lt;/span&gt;();
    request.HierarchyMembersGetCriteria = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;HierarchyMembersGetCriteria&lt;/span&gt;();

    &lt;span style="color:green;"&gt;//State that our hierarhcy type is a collection and that we want collection members
    &lt;/span&gt;request.HierarchyMembersGetCriteria.HierarchyType = &lt;span&gt;HierarchyType&lt;/span&gt;.Collection;
    &lt;span style="color:green;"&gt;//Pass in the key search criteria to identify the correct collection in MDS
    &lt;/span&gt;request.HierarchyMembersGetCriteria.ParentEntityId = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;Identifier &lt;/span&gt;{ Name = entityId };
    request.HierarchyMembersGetCriteria.HierarchyId = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;Identifier &lt;/span&gt;{ Name = hierarchyId };
    request.HierarchyMembersGetCriteria.ModelId = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;Identifier &lt;/span&gt;{ Name = modelId };
    request.HierarchyMembersGetCriteria.VersionId = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;Identifier &lt;/span&gt;{ Name = versionId };

    request.HierarchyMembersGetCriteria.RowLimit = 50;
    request.International = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;International&lt;/span&gt;();
    &lt;span&gt;OperationResult &lt;/span&gt;result = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;OperationResult&lt;/span&gt;();
    &lt;span style="color:green;"&gt;//Return the hierarchy members from the service
    &lt;/span&gt;&lt;span style="color:blue;"&gt;return &lt;/span&gt;service.HierarchyMembersGet(request.International, request.HierarchyMembersGetCriteria, &lt;span style="color:blue;"&gt;out &lt;/span&gt;result);
}&lt;/pre&gt;

&lt;p&gt;Before we use the above method, we need to connect to SSAS using AMO. That can be done quite easily with the following code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span&gt;Server &lt;/span&gt;server = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;Server&lt;/span&gt;();
&lt;span style="color:blue;"&gt;string &lt;/span&gt;connection = &lt;span&gt;&amp;quot;Data Source=.;Catalog=Adventure Works DW 2008R2;&amp;quot;&lt;/span&gt;;
server.Connect(connection);&lt;/pre&gt;

&lt;p&gt;After we’ve done all the usual error handling associated with connecting to a database, we need to pick up the SSAS database and cube:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span&gt;Database &lt;/span&gt;database = server.Databases[&lt;span&gt;&amp;quot;Adventure Works DW 2008R2&amp;quot;&lt;/span&gt;];
&lt;span&gt;Cube &lt;/span&gt;cube = database.Cubes[&lt;span&gt;&amp;quot;Adventure Works&amp;quot;&lt;/span&gt;];&lt;/pre&gt;

&lt;p&gt;Now we’re ready to call our ObtainHierarchyMembers method, before looping through it to build the named set:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span&gt;StringBuilder &lt;/span&gt;mdx = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;StringBuilder&lt;/span&gt;(&lt;span&gt;&amp;quot;\n//Auto generated named set at &amp;quot; &lt;/span&gt;+ &lt;span&gt;DateTime&lt;/span&gt;.Now.ToString() + 
    &lt;span&gt;&amp;quot;\nCREATE SET CurrentCube.[Favourite Products] AS {&amp;quot;&lt;/span&gt;);
&lt;span style="color:blue;"&gt;int &lt;/span&gt;count = 1;
&lt;span style="color:green;"&gt;//Loop through the collection to build the mdx
&lt;/span&gt;&lt;span style="color:blue;"&gt;foreach &lt;/span&gt;(&lt;span&gt;ParentChild &lt;/span&gt;pc &lt;span style="color:blue;"&gt;in &lt;/span&gt;hm.Members)
{
    &lt;span style="color:green;"&gt;//Add the members to the MDX string
    //This references the member by name
    //It would be possible to reference by member key, but would require more work
    &lt;/span&gt;mdx.Append(&lt;span&gt;&amp;quot;[Product].[Product].[&amp;quot; &lt;/span&gt;+ pc.Child.Name + &lt;span&gt;&amp;quot;]&amp;quot;&lt;/span&gt;);
    &lt;span style="color:blue;"&gt;if &lt;/span&gt;(count &amp;lt; hm.Members.Count())
    {
        mdx.Append(&lt;span&gt;&amp;quot;, &amp;quot;&lt;/span&gt;);
    }
    count++;
}
mdx.Append(&lt;span&gt;&amp;quot;};&amp;quot;&lt;/span&gt;);&lt;/pre&gt;


&lt;p&gt;Now we need to insert the named set in the correct place within the existing MDX script, bearing in mind it could already exist:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;string &lt;/span&gt;currentScript = cube.MdxScripts[0].Commands[0].Text;
&lt;span style="color:green;"&gt;//Find the correct place to insert the named set within the MDX script:
&lt;/span&gt;&lt;span style="color:blue;"&gt;int &lt;/span&gt;start = currentScript.IndexOf(&lt;span&gt;&amp;quot;\n//Auto generated named set at&amp;quot;&lt;/span&gt;);
&lt;span style="color:blue;"&gt;int &lt;/span&gt;end = 0;
&lt;span&gt;StringBuilder &lt;/span&gt;newScript = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span&gt;StringBuilder&lt;/span&gt;(currentScript);
&lt;span style="color:blue;"&gt;if &lt;/span&gt;(start != -1)
{
    end = currentScript.IndexOf(&lt;span&gt;&amp;quot;;&amp;quot;&lt;/span&gt;, start);
    &lt;span style="color:green;"&gt;//If the named set already exists, remove it
    &lt;/span&gt;newScript.Remove(start, end - start + 1);
}
&lt;span style="color:blue;"&gt;else
&lt;/span&gt;{
    start = currentScript.Length;
}
&lt;span style="color:green;"&gt;//Insert the named set in the correct place
&lt;/span&gt;newScript.Insert(start, mdx.ToString());
&lt;span style="color:green;"&gt;//Update the cube&amp;#39;s MDX script
&lt;/span&gt;cube.MdxScripts[0].Commands[0].Text = newScript.ToString();&lt;/pre&gt;

&lt;p&gt;Finally we just need to update the cube in order to write the MDX back to the cube:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:green;"&gt;//Call the update methods to update the cube
&lt;/span&gt;cube.MdxScripts[0].Update();
cube.Update();&lt;/pre&gt;




&lt;p&gt;&lt;strong&gt;User Reports&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that the cube has been updated, if the Excel report is refreshed, then the two new products will appear in the list:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.adatis.co.uk/blogs/jeremykashel/image_06F880C3.png"&gt;&lt;img style="border:0px currentColor;padding-top:0px;padding-right:0px;padding-left:0px;display:inline;background-image:none;" title="image" border="0" alt="image" src="http://blogs.adatis.co.uk/blogs/jeremykashel/image_thumb_145E93C9.png" width="348" height="150" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Controlling SSAS named sets like this won’t be for everybody. I’ve certainly worked in a few clients where strict process has to be followed to update this sort of thing, but I can think of other companies that I know where this would be really useful.&lt;/p&gt;

&lt;p&gt;Managing Analysis Services named sets is just one use for collections. Another example might be managing default multi-select parameters for SSRS reports. As collections are just lists of members that can be extracted easily, what you do with them is up to you!&lt;/p&gt;</description></item></channel></rss>