Import Data from Tabular Model in Excel Using a DAX Query

F01

When you use Excel to connect to a Tabular database in Analysis Services, you get a PivotTable as a result. In this article, you learn how to import data in a table writing a DAX query than runs on Analysis Services.

A connection to Tabular in Excel is a connection to Analysis Services. The Excel wizard does not allow creating a connection including a DAX query, but with a simple change in the ODC file generated by the wizard, you can transform a connection to a cube into a DAX query that returns data in an Excel table. You start by creating a connection to Analysis Services, specifying the server name and the log on credentials. In the following example I use the TABULAR instance on the local machine, you have to write the correct serverinstance name for your database.

F01

In the next step you select a perspective. You can simply select Model, which is the default cube name of a tabular model.

F02

At this point you specify the name of the connection file. I suggest using a name that is easy to remember. For example, I renamed the default name to DaxQuery.

F03

The last step request you to import data. As you see, the Table option is disabled for a connection to Analysis Services, by default you would see PivotTable Report selected. If you do not want to create a PivotTable at this point, you select Only Create Connection option, which just create the ODC file we need.

F04

Now you have to open an Explorer window moving to the folder in which you saved the ODC connection file. This is usually the My Data Sources folder in My Documents.

F05

In this directory, identify the DaxQuery.odc file you saved before (or the name you used instead) and open it with Notepad. In the next picture you see two rows highlighted, which are the ones you will change in order to execute a DAX query.

F06

You change the CommandType parameter from Cube to Query. This is the important change because at this point the CommandText parameter contains the query and no longer the cube name. You replace Model with the DAX query you want, for example you can write EVALUATE Currency to get the content of the Currency table in Adventure Works. Please note that you might also write an MDX query instead of a DAX one.

F07

After you saved the file you changed in Notepad, go back to Excel and open Existing Connections dialog box, choosing the connection you created (DaxQuery in this example).

F08

Because this connection contains a query, you can import the result in an Excel table, choosing Table in the Import Data dialog box that appears in the next step.

F09

As a result, you copy the content of the Currency table from Analysis Services in an Excel table. The name of the columns are the same name used on the server (using the table[column] convention) and the sort order is defined on the data source. If you specify an ORDER BY clause in the DAX query, it will be respected in the imported result.

F10

You can open the connection you use in the Excel workbook by choosing the Connections button in the Data ribbon in Excel. In the Connection Properties dialog box you can change the DAX query in the Command text parameter without opening Notepad anymore. Please note that Command type now displays Default, even if internally the value Query is saved instead. You might use Default instead of Query in the ODC file, too.

F11

The Connection Properties dialog box is not the best place in which you can author a DAX query, you will probably copy and paste queries that you tested elsewhere. In order to create DAX queries you can use the DAX Studio editor, which is available as an Excel add-in, or you can use the SQL Server Management Studio that is part of Workstation Components of SQL Server.