{"id":1669,"date":"2013-07-16T00:00:00","date_gmt":"2013-07-16T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/using-microsoft-excel-to-retrieve-ssas-tabular-data\/"},"modified":"2021-05-17T18:33:17","modified_gmt":"2021-05-17T18:33:17","slug":"using-microsoft-excel-to-retrieve-ssas-tabular-data","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/bi-sql-server\/using-microsoft-excel-to-retrieve-ssas-tabular-data\/","title":{"rendered":"Using Microsoft Excel to Retrieve SSAS Tabular Data"},"content":{"rendered":"<div id=\"pretty\">\n<p>Until recently, most of the talk about tabular data revolved around PowerPivot, an Excel add-in that brings powerful in-memory data crunching to the spreadsheet environment. Creating a tabular database in PowerPivot, and by extension, within Excel, is a fairly prescribed process that involves retrieving data from one or more data sources-most notably SQL Server databases-and creating one or more tables based on that data. <\/p>\n<p>Despite all the attention paid to PowerPivot and its tabular databases, there has been relatively little focus on how to retrieve tabular data directly from a SQL Server Analysis Services (SSAS) instance into Excel, whether through PowerPivot or directly into a workbook.<\/p>\n<p>In this article, we explore how to use Excel to view and retrieve data from an SSAS tabular database. This is the third article in a series about SSAS tabular data. In the last article, &#8220;<a href=\"https:\/\/www.simple-talk.com\/sql\/reporting-services\/using-dax-to-retrieve-tabular-data\/\">Using DAX to retrieve tabular data<\/a>,&#8221; we discussed how to create Data Analysis Expressions (DAX) statements to retrieve tabular data, working from within SQL Server Management Studio (SSMS) to run our queries against an SSAS tabular instance. Some of what was covered in that article will apply to retrieving data from within Excel.<\/p>\n<p>For this article, I provide a number of examples of how to import tabular data into Excel. The examples pull data from the <code>AdventureWorks<\/code> <code>Tabular<\/code> <code>Model<\/code> <code>SQL<\/code> <code>2012<\/code> database, available as a SQL Server Data Tools (SSDT) tabular project from the <a href=\"http:\/\/msftdbprodsamples.codeplex.com\/releases\/view\/55330\">AdventureWorks CodePlex site<\/a>. On my system, I&#8217;ve implemented the database on a local instance of SSAS 2012 in tabular mode.<\/p>\n<h1>Viewing Browsed Data in Excel<\/h1>\n<p>We got a taste of how to browse data from within SSMS in the first article of this series, &#8220;<a href=\"https:\/\/www.simple-talk.com\/sql\/reporting-services\/getting-started-with-the-ssas-tabular-model-\/\">Getting Started with the SSAS Tabular Model<\/a>.&#8221; In <code>Object<\/code> <code>Explorer<\/code>, right-click the database and then click <code>Browse<\/code>. When the <code>Browse<\/code> window appears, you can view a list of database objects and use those objects to retrieve data by dragging them into the main workspace. For example, Figure 1 shows data from the <code>Country<\/code> <code>Region<\/code> <code>Name<\/code> and <code>City<\/code> columns of the <code>Geography<\/code> table, as well as from the <code>Internet<\/code> <code>Total<\/code> <code>Sales<\/code> and <code>Internet<\/code> <code>Total<\/code> <code>Units<\/code> measures of the <code>Internet<\/code> <code>Sales<\/code> table. <\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image002.jpg\" width=\"624\" height=\"458\" alt=\"1835-clip_image002.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 1: Browsing tabular data in SSMS<\/b><\/p>\n<p>Notice in Figure 1 that I also created a filter that limits the amount of data returned. (You define filters in the small windows above the main workspace.) In this case, I eliminated all sales that include the value <code>Bikes<\/code> in the <code>Category<\/code> <code>Name<\/code> column of the <code>Product<\/code> table. This way, all non-bike merchandise is included in the sales figures.<\/p>\n<p>What&#8217;s shown in Figure 1 is pretty much the extent of what you can do in the <code>Browse<\/code> window in SSMS. To be able to fully drill into the data, you need to use Excel. Starting with SQL Server 2012, Excel is now the de facto tool in SSMS and SSDT for analyzing tabular data. Unfortunately, that means you need Office 2003 or later installed on the same computer where SSMS and SSDT are installed, not a practical solution for everyone. Even so, having Excel installed makes analyzing tabular data a much easier process.<\/p>\n<p>Part of that simplicity lies in the fact that the SSMS <code>Browse<\/code> window and the SSDT tabular project window both include the <code>Analyze<\/code> <code>in<\/code> <code>Excel<\/code> button on their respective menus. The button launches Excel and let&#8217;s you browse the tabular data from within in a pivot table.<\/p>\n<p>When you first click the <code>Analyze<\/code> <code>in<\/code> <code>Excel<\/code> button, you must choose which perspective to open in Excel, as shown in Figure 2. As you might recall from the first article, a perspective represents all or part of the data model. The <code>Model<\/code> perspective includes all database components.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image004.jpg\" width=\"540\" height=\"594\" alt=\"1835-clip_image004.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 2: Selecting a perspective when sending data to Excel<\/b><\/p>\n<p>Once you&#8217;ve selected a perspective, click <code>OK<\/code>. Excel is launched and opens to a new pivot table that automatically connects to the SSAS tabular data source. Figure 3 shows the pivot table before any data has been added. Notice in the right window you&#8217;ll find the <code>PivotTable<\/code> <code>Field<\/code> <code>List<\/code> pane, which contains a list of all your database objects, including tables, columns, and measures.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image006.jpg\" width=\"624\" height=\"352\" alt=\"1835-clip_image006.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 3: Importing tabular data into an Excel pivot table <\/b><\/p>\n<p>To get started with the pivot table, drag the fields and measures you want to include in your report to the <code>Column<\/code> <code>Labels<\/code>, <code>Row<\/code> <code>Labels<\/code>, and <code>Values<\/code> panes. The data will then be displayed in the pivot table. You can also create filters by dragging fields to the <code>Report<\/code> <code>Filter<\/code> pane. Figure 4 shows the pivot table after I added numerous database objects. Many of these objects are the same columns and measures that were added to the <code>Browse<\/code> window shown in Figure 1, except that now I also break down the sales by years.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image008.jpg\" width=\"624\" height=\"352\" alt=\"1835-clip_image008.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 4: Working with tabular data in an Excel pivot table<\/b><\/p>\n<p>To configure the pivot table shown in Figure 4, I added the following database objects to the specified configuration panes in the right window:<\/p>\n<ul>\n<li>The <code>Country Region Name<\/code> column from the <code>Geography<\/code> table to the <code>Row Labels<\/code> pane.  <\/li>\n<li>The <code>City<\/code> column from the <code>Geography<\/code> table to the <code>Row Labels<\/code> pane.  <\/li>\n<li>The <code>Year<\/code> column (from the Calendar hierarchy) of the <code>Date<\/code> table to the <code>Column Labels<\/code> pane.  <\/li>\n<li>The <code>Internet Total Sales<\/code> measure from the I<code>nternet Sales<\/code> table to the <code>Values<\/code> pane.  <\/li>\n<li>The <code>Internet Total Units<\/code> measure from the <code>Internet Sales<\/code> table to the <code>Values<\/code> pane.  <\/li>\n<li>The <code>Product Category Name<\/code> column from the <code>Product<\/code> table to the <code>Report Filter<\/code> pane. <\/li>\n<\/ul>\n<p>Once you&#8217;ve set up your pivot table, you can play around with the data however you want. For example, you can drill down into the year values or view only the country totals without viewing the cities. You can also find rollups for each category of data. Plus, you can filter data based on the values in the <code>Product<\/code> <code>Category<\/code> <code>Name<\/code> column. Keep in mind, however, that this article is by no means a tutorial on how to work with Excel pivot tables. For that, you need to check out the Excel documentation. SSMS and SSDT make it easy to view the data in Excel, but it&#8217;s up to you to derive meaning from what you find.<\/p>\n<h1>Importing Data into an Excel Pivot Table<\/h1>\n<p>Not everyone will have SSMS or SSDT installed on their systems, but they might still need to access an SSAS instance to retrieve tabular data. In that case, they can establish their own connection to the tabular database (assuming they have the necessary permissions) and import the data into a pivot table, just as we saw above.<\/p>\n<p>To connect to a tabular database from within Excel, go to the <code>Data<\/code> tab, click the <code>From<\/code> <code>Other<\/code> <code>Sources<\/code> button, and then click <code>From<\/code> <code>Analysis<\/code> <code>Services<\/code>. When the <code>Data<\/code> <code>Connection<\/code> <code>Wizard<\/code> appears, type the instance name and any necessary logon credentials, as shown in Figure 5. (In my case, I used Windows Authentication.)<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image009.png\" width=\"527\" height=\"371\" alt=\"1835-clip_image009.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 5: Connecting to an SSAS instance in tabular mode<\/b><\/p>\n<p>Click <code>Next<\/code> to advance to the <code>Select<\/code> <code>Database<\/code> <code>and<\/code> <code>Table<\/code> page, shown in Figure 6. Here you select the database you want to connect to and the specific perspective. Once again, I&#8217;ve selected <code>Model<\/code> in order to retrieve all the database components. (As you&#8217;ve probably noticed, Microsoft is somewhat inconsistent in its interfaces when it comes to referring to perspectives in a tabular database. Sometimes they&#8217;re referred to as cubes, sometimes perspective, and sometimes <code>Model<\/code> is referred to as a cube and the other components as perspectives. I&#8217;m not sure how <code>Table<\/code> fits into the picture.)<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image011.png\" width=\"526\" height=\"374\" alt=\"1835-clip_image011.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 6: Selecting a perspective when connecting to tabular data<\/b><\/p>\n<p>After you&#8217;ve selected your database and perspective, click <code>Next<\/code> to advance to the <code>Save<\/code> <code>Data<\/code> <code>Connection<\/code> <code>File<\/code> <code>and<\/code> <code>Finish<\/code> page, shown in Figure 7. Here you can provide a name for the data connection file (an .odc file), a description of the connection, and a friendly name for the connection.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image013.png\" width=\"527\" height=\"443\" alt=\"1835-clip_image013.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 7: Creating a data connection file in Excel<\/b><\/p>\n<p>Once you&#8217;ve supplied the necessary information, click <code>Finish<\/code>. You&#8217;ll then be presented with the <code>Import<\/code> <code>Data<\/code> dialog box, as shown in Figure 8. You can choose whether to create a pivot table, a pivot table and pivot chart, or the data connection only. You can also choose to use the existing worksheet or open a new one. <\/p>\n<div class=\"float-left\">\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image015.png\" width=\"302\" height=\"254\" alt=\"1835-clip_image015.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 8: Importing tabular data into a pivot table<\/b><\/p>\n<\/div>\n<p>You might have noticed that the <code>Table<\/code> option is grayed out. When importing tabular data in this way, Excel is very picky. Either you use a pivot table (and pivot chart, if you want) or nothing at all. You cannot import the data into a set of tables. However, there is a way to work around the default behavior, which we cover in the following section.<\/p>\n<p>But for now, stick with the default options (those shown in Figure 8), and click <code>OK<\/code>. You&#8217;ll then be presented with a new pivot table and the <code>PivotTable<\/code> <code>Field<\/code> <code>List<\/code> pane, with all the tables and their columns and measures listed, similar to what is shown in Figure 3. You can then work with that data to create a report just like we did earlier.<\/p>\n<h1>Using DAX to Import Data into Excel<\/h1>\n<p>To import tabular data into a regular Excel table, rather than a pivot table, we can create a DAX expression that pulls in exactly the data we need. We start by creating a data connection, just as we did in the last section. Once again, launch the <code>Data<\/code> <code>Connection<\/code> <code>Wizard<\/code> and follow the prompts. When you get to the <code>Save<\/code> <code>Data<\/code> <code>Connection<\/code> <code>File<\/code> <code>and<\/code> <code>Finish<\/code> page, provide a file name, description, and friendly name. But keep all the names simple because we&#8217;ll be referencing them later in the process and we want to make sure everything is easy to find. Figure 9 shows the names I used to set up the data connection on my system.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image013.png\" width=\"527\" height=\"443\" alt=\"1835-clip_image013.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 9: Creating a data connection file in Excel<\/b><\/p>\n<p>When you click <code>Finish<\/code>, you&#8217;ll again be presented with the <code>Import<\/code> <code>Data<\/code> dialog box. This time around, select the option <code>Only<\/code> <code>Create<\/code> <code>Connection<\/code>, as shown in Figure 10. We don&#8217;t want to pull in any data just yet. We just want to establish how we&#8217;ll be connecting to the tabular database. Click <code>OK<\/code> to finish creating the connection.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image019.png\" width=\"302\" height=\"254\" alt=\"1835-clip_image019.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 10: Creating a data connection without creating a pivot table<\/b><\/p>\n<p>The next step is to locate the data connection file we just created so we can edit it. On my system, I had named the file <code>aw_query.odc<\/code>, and I found it in the <code>My<\/code> <code>Documents\/My<\/code> <code>Data<\/code> <code>Sources<\/code> folder associated with my user account. <\/p>\n<p>When editing the connection file, which we can do in Notepad, we want to modify the default settings so we can use a DAX query to retrieve the data. To achieve this, we need to modify two lines, the <code>CommandType<\/code> and <code>CommandText<\/code> elements, which are highlighted in the following HTML:<\/p>\n<pre class=\"mark:20,21\">&lt;head&gt;\n&lt;meta http-equiv=Content-Type content=\"text\/x-ms-odc; charset=utf-8\"&gt;\n&lt;meta id=\"ProgId\" content=ODC.Cube&gt;\n&lt;meta id=\"SourceType\" content=OLEDB&gt;\n&lt;meta id=\"Catalog\" content=\"AdventureWorks Tabular Model SQL 2012\"&gt;\n&lt;meta id=\"Table\" content=Model&gt;\n&lt;title&gt;AdventureWorks query&lt;\/title&gt;\n&lt;xml id=\"docprops&gt;&lt;o:DocumentProperties\"&gt;\n&#160; xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n&#160; xmlns=\"http:\/\/www.w3.org\/TR\/REC-html40\"&gt;\n&#160; &lt;o:Description&gt;Generate DAX statement to retrieve table from AdventureWorks database.&lt;\/o:Description&gt;\n&#160; &lt;o:Name&gt;AdventureWorks query&lt;\/o:Name&gt;\n&#160;&lt;\/o:DocumentProperties&gt;\n&lt;\/xml&gt;\n&lt;xml id=\"msodc&gt;&lt;odc:OfficeDataConnection\"&gt;\n&#160; xmlns:odc=\"urn:schemas-microsoft-com:office:odc\"\n&#160; xmlns=\"http:\/\/www.w3.org\/TR\/REC-html40\"&gt;\n&#160; &lt;odc:Connection odc:Type=\"OLEDB\"&gt;\n&#160;&#160; &lt;odc:ConnectionString&gt;Provider=MSOLAP.4;Integrated Security=SSPI;Persist Security Info=True;Data Source=win7vm\\ssas2012tab;Initial Catalog=AdventureWorks Tabular Model SQL 2012&lt;\/odc:ConnectionString&gt;\n&#160;&#160; &lt;odc:CommandType&gt;Cube&lt;\/odc:CommandType&gt;\n&#160;&#160; &lt;odc:CommandText&gt;Model&lt;\/odc:CommandText&gt;\n&#160; &lt;\/odc:Connection&gt;\n&#160;&lt;\/odc:OfficeDataConnection&gt;\n&lt;\/xml&gt;\n&lt;style&gt;\n&lt;!--\n&#160;&#160;&#160; .ODCDataSource\n&#160;&#160;&#160; {\n&#160;&#160;&#160; behavior: url(dataconn.htc);\n&#160;&#160;&#160; }\n--&gt;\n&lt;\/style&gt;\n&lt;\/head&gt;\n\n<\/pre>\n<p>The HTML shown here is the <code>head<\/code> section of the data connection file. For the <code>CommandType<\/code> element, replace the value <code>Cube<\/code> with the value <code>query<\/code>. This tells Excel to let us pass a query in through our data connection, rather than having to identify a specific perspective.<\/p>\n<p>For the <code>CommandText<\/code> element, replace the value <code>Model<\/code> with a DAX statement that retrieves data into a single result set. For this example, I pulled the following query from the last article in this series (so return to that article for an explanation):<\/p>\n<pre>evaluate\n(\n&#160; filter\n&#160; (\n&#160;&#160;&#160; addcolumns\n&#160;&#160;&#160; (\n&#160;&#160;&#160; &#160;&#160;summarize\n&#160;&#160;&#160;&#160;&#160; (\n&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Internet Sales',\n&#160;&#160; &#160;&#160;&#160;&#160;&#160;'Product'[Product Name],\n&#160;&#160;&#160; &#160;&#160;&#160;&#160;'Product Subcategory'[Product Subcategory Name],\n&#160;&#160;&#160; &#160;&#160;&#160;&#160;'Product Category'[Product Category Name],\n&#160;&#160;&#160; &#160;&#160;&#160;&#160;'Date'[Calendar Year]\n&#160;&#160;&#160;&#160;&#160; ),\n&#160;&#160; &#160;&#160;&#160;\"Total Sales Amount\", calculate(sum('Internet Sales'[Sales Amount])),\n&#160;&#160; &#160;&#160;&#160;\"Total Cost\", calculate(sum('Internet Sales'[Total Product Cost]))\n&#160;&#160;&#160; ),\n&#160;&#160;&#160; 'Date'[Calendar Year] &gt; 2006\n&#160; )\n)\norder by \n&#160; 'Product'[Product Name],\n&#160; 'Date'[Calendar Year]\n\n<\/pre>\n<p>You can use a different query, of course, whatever serves your needs. The goal is to retrieve the data into a single table that you can view in an Excel worksheet, just like any other worksheet. After you modify the HTML in the data connection file, the <code>head<\/code> section should look similar to the following:<\/p>\n<pre class=\"mark:20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45\">&lt;head&gt;\n&lt;meta http-equiv=Content-Type content=\"text\/x-ms-odc; charset=utf-8\"&gt;\n&lt;meta id=\"ProgId\" content=ODC.Cube&gt;\n&lt;meta id=\"SourceType\" content=OLEDB&gt;\n&lt;meta id=\"Catalog\" content=\"AdventureWorks Tabular Model SQL 2012\"&gt;\n&lt;meta id=\"Table\" content=Model&gt;\n&lt;title&gt;AdventureWorks query&lt;\/title&gt;\n&lt;xml id=\"docprops&gt;&lt;o:DocumentProperties\"&gt;\n&#160; xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n&#160; xmlns=\"http:\/\/www.w3.org\/TR\/REC-html40\"&gt;\n&#160; &lt;o:Description&gt;Generate DAX statement to retrieve table from AdventureWorks database.&lt;\/o:Description&gt;\n&#160; &lt;o:Name&gt;AdventureWorks query&lt;\/o:Name&gt;\n&#160;&lt;\/o:DocumentProperties&gt;\n&lt;\/xml&gt;\n&lt;xml id=\"msodc&gt;&lt;odc:OfficeDataConnection\"&gt;\n&#160; xmlns:odc=\"urn:schemas-microsoft-com:office:odc\"\n&#160; xmlns=\"http:\/\/www.w3.org\/TR\/REC-html40\"&gt;\n&#160; &lt;odc:Connection odc:Type=\"OLEDB\"&gt;\n&#160;&#160; &lt;odc:ConnectionString&gt;Provider=MSOLAP.4;Integrated Security=SSPI;Persist Security Info=True;Data Source=win7vm\\ssas2012tab;Initial Catalog=AdventureWorks Tabular Model SQL 2012&lt;\/odc:ConnectionString&gt;\n&#160;&#160; &lt;odc:CommandType&gt;query&lt;\/odc:CommandType&gt;\n&#160;&#160; &lt;odc:CommandText&gt;\n&#160;&#160;&#160;&#160; evaluate\n&#160;&#160;&#160;&#160; (\n&#160;&#160;&#160;&#160;&#160;&#160; filter\n&#160;&#160;&#160;&#160;&#160;&#160; (\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; addcolumns\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; summarize\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Internet Sales',\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Product'[Product Name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Product Subcategory'[Product Subcategory Name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Product Category'[Product Category Name],\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Date'[Calendar Year]\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ),\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \"Total Sales Amount\", calculate(sum('Internet Sales'[Sales Amount])),\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \"Total Cost\", calculate(sum('Internet Sales'[Total Product Cost]))\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ),\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'Date'[Calendar Year] &gt; 2006\n&#160;&#160;&#160;&#160;&#160;&#160; )\n&#160;&#160;&#160;&#160; )\n&#160;&#160;&#160;&#160; order by \n&#160;&#160;&#160;&#160;&#160;&#160; 'Product'[Product Name],\n&#160;&#160;&#160;&#160;&#160;&#160; 'Date'[Calendar Year]\n&#160;&#160; &lt;\/odc:CommandText&gt;\n&#160; &lt;\/odc:Connection&gt;\n&#160;&lt;\/odc:OfficeDataConnection&gt;\n&lt;\/xml&gt;\n&lt;style&gt;\n&lt;!--\n&#160;&#160;&#160; .ODCDataSource\n&#160;&#160;&#160; {\n&#160;&#160;&#160; behavior: url(dataconn.htc);\n&#160;&#160;&#160; }\n--&gt;\n&lt;\/style&gt;\n&lt;\/head&gt;\n\n<\/pre>\n<p>I highlighted the new code to make it easier to pick out. I also laid out the DAX statement so it&#8217;s more readable. However, you don&#8217;t need to break it apart in this way to add it into the file. (The parser ignores the extra whitespace and line breaks.)<\/p>\n<p>After you save and close the data connection file, it&#8217;s time to verify what you&#8217;ve done. In Excel, go to the <code>Data<\/code> tab and click <code>Existing<\/code> <code>Connections<\/code>. When the <code>Existing<\/code> <code>Connections<\/code> dialog box appears, double-click the connection you just edited. It will be listed under its friendly name, not the file name. On my system, the connection&#8217;s friendly name is <code>AdventureWorks<\/code> <code>query<\/code>. This launches the <code>Import<\/code> <code>Data<\/code> dialog box, shown in Figure 11. Notice that this time around, the <code>Table<\/code> option is available and selected.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image021.png\" width=\"301\" height=\"253\" alt=\"1835-clip_image021.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 11: Importing tabular data into an Excel table<\/b><\/p>\n<p>Click <code>OK<\/code> to close the <code>Import<\/code> <code>Data<\/code> dialog box. A table will appear, populated with the data retrieved through the DAX statement, as shown in Figure 11. You can then sort through the data and create any necessary filters. <\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image024.jpg\" width=\"624\" height=\"322\" alt=\"1835-clip_image024.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 12: Working with tabular data in an Excel table<\/b><\/p>\n<p>When you&#8217;re working in a table based on tabular data, you can modify the connection associated with the current workbook. Doing so, however, severs its relationship to the original .odc data connection file. Instead, your changes are saved to the connection specifically associated with that workbook. The .odc file remains unchanged. Updating the connection in this way lets you modify the DAX statement, change the connection string, or configure other properties.<\/p>\n<p>To modify the workbook&#8217;s connection, go to the <code>Data<\/code> tab and click <code>Connections<\/code>. This launches the <code>Workbook<\/code> <code>Connections<\/code> dialog box, shown in Figure 13. Here you should find the connection you created earlier. On my system, this is <code>AdventureWorks<\/code> <code>query<\/code>. <\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image025.png\" width=\"570\" height=\"394\" alt=\"1835-clip_image025.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 13: Viewing the data connections associated with an Excel workbook<\/b><\/p>\n<p>Next, we want to access the connection&#8217;s properties. Make sure the connection is selected (in case you have more than one connection listed), and then click <code>Properties<\/code>. This launches the <code>Connection<\/code> <code>Properties<\/code> dialog box. Go to the <code>Definition<\/code> tab and expand the dialog box as necessary to easily view the DAX statement. Figure 14 shows what the dialog box looks like on my system.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image027.png\" width=\"566\" height=\"1026\" alt=\"1835-clip_image027.png\" \/><\/p>\n<p class=\"caption\">Figure 14: Viewing the properties of an Excel data connection<\/p>\n<p>Chances are, when you open the dialog box, the DAX statement will be difficult to read. The statement shown in Figure 14 is what it looked like after I organized things a bit for readability. The changes I made have no impact on the statement itself.<\/p>\n<p>You can modify the DAX statement or connection string as necessary (or any other properties, for that matter). To save you changes, click <code>OK<\/code>. You&#8217;ll then be presented with an Excel message box warning you that you are about to sever ties with the original data connection file, as shown in Figure 15. That&#8217;s not a problem, though, because your updated connection information will be saved with the workbook when you save it.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image030.jpg\" width=\"623\" height=\"98\" alt=\"1835-clip_image030.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 15: Severing ties between a data connection and its source file<\/b><\/p>\n<p>You can now update you connection as often as you like. You should not receive this warning going forward. You can also use the existing .odc connection to easily add other tables to your workbook. To do so, add a new worksheet, connect to the data source via the .odc connection, and then modify the connection properties by adding a different DAX statement. (You can also rename the connection as necessary.) That way you can create a workbook that contains numerous worksheets that each includes an imported table from a tabular data source.<\/p>\n<h1>Using DAX to Import Data into PowerPivot<\/h1>\n<p>Up to this point, I&#8217;ve shied away from discussing PowerPivot in too great of detail, mostly because it&#8217;s already received so much press. But one issue that&#8217;s received little attention is how you retrieve data from an SSAS tabular instance into PowerPivot.<\/p>\n<p>Surprisingly, the functionality is somewhat limited. You would expect-or at least I would expect-that I could import all the tables, along with their columns and measures, in a single connection, just as I can do with an Excel pivot table or a SQL Server database in PowerPivot. But that doesn&#8217;t appear to be the case. The only option I can find for importing tabular data directly into PowerPivot is one table at a time, just like we saw with Excel tables. However, doing so in PowerPivot is a bit easier.<\/p>\n<p>In the PowerPivot window, click the <code>From<\/code> <code>Database<\/code> button on the <code>Home<\/code> tab and then click <code>From<\/code> <code>Analysis<\/code> <code>Services<\/code> <code>or<\/code> <code>PowerPivot<\/code>. When the <code>Table<\/code> <code>Import<\/code> <code>Wizard<\/code> appears, provide the name of the SSAS tabular instance, the necessary logon information, and the name of the tabular database, as shown in Figure 16.<\/p>\n<p class=\"illustration\">\n<p class=\"caption\"><b>Figure 16: Defining a data connection to an SSAS tabular database<\/b><\/p>\n<p>Don&#8217;t forget, you can also provide a friendly connection name. And it&#8217;s always a good idea to test your connection. When you&#8217;re finished, click <code>Next<\/code>.<\/p>\n<p>The next page in the wizard is <code>Specify<\/code> <code>a<\/code> <code>MDX<\/code> <code>Query<\/code>. As with SSMS or your Excel data connections, you can specify a DAX query rather than a Multidimensional Expressions (MDX) query. We&#8217;ll go with DAX. Figure 17 shows the page with the same query we used in the previous section. You can use the <code>Validate<\/code> option to check your statement, which seems to work with DAX, unlike the <code>Design<\/code> option, which seems happier sticking with MDX.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image033.png\" width=\"547\" height=\"582\" alt=\"1835-clip_image033.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 17: Defining a DAX query to retrieve tabular data from SSAS<\/b><\/p>\n<p>After you entered your DAX query (and provided a name for the query, if you like), click <code>Finish<\/code> to import the data. During this process, you&#8217;ll advance to the wizard&#8217;s <code>Importing<\/code> page. If the data is imported with no problems, you should receive a <code>Success<\/code> message, as shown in Figure 18.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image035.png\" width=\"548\" height=\"582\" alt=\"1835-clip_image035.png\" \/><\/p>\n<p class=\"caption\"><b>Figure 18: Successfully importing SSAS tabular data into PowerPivot for Excel<\/b><\/p>\n<p>Click <code>Close<\/code>. This will open a new table in the PowerPivot window, with the imported data displayed. Figure 19 shows part of the data returned by the query. If the data is not exactly what you need, you can refine your query as necessary.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1835-clip_image038.jpg\" width=\"624\" height=\"321\" alt=\"1835-clip_image038.jpg\" \/><\/p>\n<p class=\"caption\"><b>Figure 19: Working with tabular data in PowerPivot for Excel<\/b><\/p>\n<p>You can import data into as many tables as you need. You can then create relationships between those tables, assuming you bring in the columns necessary to define those relationships. Not surprisingly, it would be handier to go directly to the source of the data, in this case, the <code>AdventureWorksDW2012<\/code> database, so you can retrieve all the tables at once, along with their defined relationships. But you might not have access to that data and have no choice but to be retrieve it directly from the SSAS instance. Or there might be other circumstances that require you to retrieve the data directly from SSAS. Whatever the reason, you now know how it&#8217;s done, and once you&#8217;ve tried it out, you&#8217;ll find it&#8217;s a fairly simple operation.<\/p>\n<h1>The Excel Connection<\/h1>\n<p>As you&#8217;ve seen in this article, you have several options for retrieving SSAS tabular data into Excel. You can launch Excel from within SSMS or SSDT. You can import data directly into a pivot table. And you can generate DAX queries to retrieve data into individual Excel and PowerPivot tables. Or instead of DAX, you can generate MDX queries, unless the tabular database has been set up in DirectQuery mode. Regardless, you have a number of options for importing tabular data into Excel. And once you get it there, you have a rich environment for manipulating and analyzing the data, allowing you to create precise and detailed reports in the format you need when you need them, without having to be concerned about SSAS and how the tabular model works.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Although it is well-known how to create a tabular database in PowerPivot, it is less obvious that there are several useful options for retrieving SSAS tabular data into Excel. This provides an easy way of manipulating, visualizing and analyzing the data without needing to know the details of SSAS and the tabular model. &hellip;<\/p>\n","protected":false},"author":221841,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143528],"tags":[4168,5871,4379,4150],"coauthors":[],"class_list":["post-1669","post","type-post","status-publish","format-standard","hentry","category-bi-sql-server","tag-database","tag-powerpivot","tag-reporting-services","tag-sql"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221841"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1669"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1669\/revisions"}],"predecessor-version":[{"id":91016,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1669\/revisions\/91016"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1669"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}