We’ve all met them: the users whose idea of a ‘report’ is a large
table of data dumped into Excel. However many beautiful data
visualisations they see, however many times you try to convince them of
the benefits of using Power BI to build reports, they just want to know
where the “Export to Excel” button is so they can carry on analysing
data in the same old way. Sometimes there’s a valid reason for doing
this, sometimes not, but all too often internal politics means that you
have to accommodate them.
If you’re using Power BI the obvious way to do this is to use the “Export Data” button in Power BI. However, as the documentation notes, there are some limits on the amount of data that can be exported:
There is another way to get data from Power BI into Excel though: using the Analyze in Excel feature. Rather than using Analyze in Excel to create a PivotTable, which may suffer from the subtotals issue described here if you aren’t using the click-to-run version of Excel 2016 and as a result may be very slow when dealing with large amounts of data, and which will be awkward to get data out of because you have to use Excel functions like GetPivotData(), in this post I’ll show you how to get data from Power BI into an Excel table instead.
First of all you need to create a PivotTable in Excel on your desktop that is linked to a dataset published to Power BI. You can do this either by clicking the Analyze in Excel option from inside the Power BI service in the browser (see here for how to do this), or by using the Connect to Data option in the Power BI Publisher for Excel add-in.
Next, drag any measure into your PivotTable (you will need to have at least one measure defined in your dataset to do this) like so:
Then double-click inside a cell containing values, such as the selected cell in the screenshot above, or right-click on the cell and select Show Details:
When you do this a new worksheet will appear with a table in it containing rows of data from a table in the underlying dataset, but the exact data is irrelevant here. The real point is that you now have an Excel table (not a PivotTable) with a connection back to your Power BI dataset and you can edit the query that it uses to return data from Power BI – a variation on an old trick known to SSAS and Power Pivot users. You can now delete the PivotTable you created because you will no longer need it.
On the new worksheet with the table on it, right-click in a cell and select Table then Edit Query:
A dialog will pop up, and you’ll see an MDX Drillthrough statement in the Command Text box as shown below:
You can replace this statement with any MDX or DAX query you want – I recommend using DAX queries here because they are likely to be easier to write if you already know how to write DAX calculations, and unlike MDX queries they return a tabular resultset. I have a fairly old series of posts on DAX queries here that will help get you going. It will be a good idea to install DAX Studio and write your queries there, then copy them into Excel when you are happy with them.
Once you have a working DAX query in the Command Text box:
You can click OK, the query will run and you’ll see the output of the query in the Excel table:
One minor annoyance is that the column names will be in DAX ‘Table Name’[Column Name] format, and although you can use the SELECTCOLUMNS() DAX function to alias your column names and thereby remove the table names, the square brackets around the column names will always be there.
There are several good things about this approach to getting data into Excel:
If you’re using Power BI the obvious way to do this is to use the “Export Data” button in Power BI. However, as the documentation notes, there are some limits on the amount of data that can be exported:
- The maximum number of rows that can be exported from Power BI Desktop and Power BI service to .csv is 30,000
- The maximum number of rows that can be exported to .xlsx is 150,000
There is another way to get data from Power BI into Excel though: using the Analyze in Excel feature. Rather than using Analyze in Excel to create a PivotTable, which may suffer from the subtotals issue described here if you aren’t using the click-to-run version of Excel 2016 and as a result may be very slow when dealing with large amounts of data, and which will be awkward to get data out of because you have to use Excel functions like GetPivotData(), in this post I’ll show you how to get data from Power BI into an Excel table instead.
First of all you need to create a PivotTable in Excel on your desktop that is linked to a dataset published to Power BI. You can do this either by clicking the Analyze in Excel option from inside the Power BI service in the browser (see here for how to do this), or by using the Connect to Data option in the Power BI Publisher for Excel add-in.
Next, drag any measure into your PivotTable (you will need to have at least one measure defined in your dataset to do this) like so:
Then double-click inside a cell containing values, such as the selected cell in the screenshot above, or right-click on the cell and select Show Details:
When you do this a new worksheet will appear with a table in it containing rows of data from a table in the underlying dataset, but the exact data is irrelevant here. The real point is that you now have an Excel table (not a PivotTable) with a connection back to your Power BI dataset and you can edit the query that it uses to return data from Power BI – a variation on an old trick known to SSAS and Power Pivot users. You can now delete the PivotTable you created because you will no longer need it.
On the new worksheet with the table on it, right-click in a cell and select Table then Edit Query:
A dialog will pop up, and you’ll see an MDX Drillthrough statement in the Command Text box as shown below:
You can replace this statement with any MDX or DAX query you want – I recommend using DAX queries here because they are likely to be easier to write if you already know how to write DAX calculations, and unlike MDX queries they return a tabular resultset. I have a fairly old series of posts on DAX queries here that will help get you going. It will be a good idea to install DAX Studio and write your queries there, then copy them into Excel when you are happy with them.
Once you have a working DAX query in the Command Text box:
You can click OK, the query will run and you’ll see the output of the query in the Excel table:
One minor annoyance is that the column names will be in DAX ‘Table Name’[Column Name] format, and although you can use the SELECTCOLUMNS() DAX function to alias your column names and thereby remove the table names, the square brackets around the column names will always be there.
There are several good things about this approach to getting data into Excel:
- It allows you to exceed the 150,000 row limit of the native Power BI Export option mentioned above
- The queries are very quick to run if you aren’t doing anything complex in them
- It avoids manual exporting – you just use Excel’s native Refresh functionality to run the query whenever you want to download new data into Excel
- There’s no easy way to pass parameters to the queries you use, and so allow the user to choose what data is retrieved from Power BI. I guess it would be possible with some VBA, though – I haven’t tried – and it may also be possible to connect Power Query/Get&Transform to the Power BI Service and then use some of the tricks I show in this video for building reporting solutions in Excel. You can of course use Excel’s own native table filtering functionality to filter the data that is downloaded.
- If your DAX query is slow to run your users may get frustrated. To counter this you could create calculated tables
in your Power BI dataset using the same DAX as your query, and because
calculated tables are created when the Power BI dataset is refreshed
rather than when your user refreshes their Excel worksheet this should
speed things up. However it will increase the size of your Power BI
dataset and make your dataset refresh take longer. In Excel your DAX
query would simply be something like:
EVALUATE ‘My Calculated Table Name’ - Writing DAX can be complex, whether it’s a DAX query or a calculated table, so another option would be to use the Power BI Query Editor to create the table you need for your report, load that into your dataset and then load the table into Excel.
- You can’t publish the Excel workbook up to Power BI and have it refresh automatically, because of course Power BI does not support connections from published Excel workbooks back to Power BI datasets. Hopefully this will change in the future – it marked as planned on the Ideas forum.
Comments
Post a Comment