Quantcast
Channel: Power Query Archives –
Viewing all articles
Browse latest Browse all 111

Export data from Power BI using Microsoft Flow

$
0
0

In my last 2 posts I’ve described a way to automatically validate attachments from incoming E-mails. Microsoft Flow would watch for incoming E-mails that match certain criteria and move their attachments to a dedicated folder. Then it would trigger a refresh of a Power BI dataset that has been designed to check for errors in those attachments. Data driven alerts in Power BI would indicate that there are certain errors and trigger a Flow that would send an E-mail back to the sender, informing him that his attachments didn’t meet the agreed criteria.

In this article I will now explain how not just a trigger about the existence of a faulty attachment could be passed back to Flow, but also the corresponding data itself. Therefore I write a query that exports data from Power BI to Flow. But watch out: This is not suitable for very big tables. I experienced timeouts at tables with 300k rows already.

Send data from Power BI to Microsoft Flow

As Chris Webb described in this article, Power Query can create POST requests to a webservice, thereby passing values in the body of the call to the webservice. This allows to export the data from Power BI. With Flow, it is very easy to setup a webservice: Just create a Flow whose trigger is a “When a HTTP request is received” (no further inputs in that step) and add a “Compose”-action with the Body-element in it. Then save the Flow and copy the generated URL:

Setup webservice in Flow that listens for incoming data (export from Power BI)

Power BI

In Power BI, you perform the following steps:

  1. Transform your error-table into a list of records (as that is the format of a JSON-array that Flow likes):
    • row 3: Table.ToRecords(<YourTableName>)
  2. Transform that into a JSON-binary that the Web.Contents-function can digest:
    • row 4: Json.FromValue(<YourListOfRecordsFromAbove>)
  3. Make the Web-call to the copied URL (use Anonymus credentials):
    • row 5-7: Web.Contents(<PasteTheURLFromFlowHere>, [Content=<JsonFromAbove>])
  4. Parse the result and load this query to the datamodel (this is very important, because otherwise the WebCall wouldn’t be made if the dataset is refreshed!):
    • row 8: Lines.FromBinary(<WebCallFromAbove>)

Code to export a table from Power BI to Flow

Back in Flow

Modify the “Compose”-step to parse out the table from the JSON like this:

Next step is to create a csv table:

And export it to OneDrive:

Save csv to OneDrive for Business

I’ve passed the FolderPath and FileName dynamically from Power BI here, but that’s not necessary. Just make sure that the folder is different than the one that contains the original attachments. A trigger will be set on this folder that sends the e-mail back to the sender. So the saving of the original attachment mustn’t trigger this return-email back to the sender already.

Now this sub-flow is completed. In the next steps you will adjust the main flow (like described in the previous post) to attach the generated file to the email.

Adjust main Flow

Task is to attach the stored csv-file to the email that will be sent out to the sender of the attachment. There are a couple of new steps (red) and an adjustment in the “Send email”-step:

Adjustments for the return-attachment

As the query, which calls the web-service will probably be executed twice, I’ve added a wait-step. The time has to fit to the table length, so you might test this for your specific file behaviour. Then the content from the saved file has to be fetched and added in the attachment:

Adjustments in main Flow: Attach e-mail

The last 2 new steps in the flow are similar to the previous ones and simply delete the files from the “return-attachment”-folder. Of course, if you want to keep them in a folder somewhere, you can instead implement a step that moves the file over there, instead of deleting.

Outlook

As soon as Power Query will be fully integrated in Flow, these tasks don’t need any Power BI-involvement any more: You can do the validations directly in the editor there. Although: Given the current lack of support to parse csv-files in Flow (and the terrible performance of the current workaround), I’m wondering if it wouldn’t be better, if the Power Query connector in Flow would save it’s results to csv-files instead of pushing it back to Flow (a bit like the new dataflows are doing it now). This would allow for mass-data-transformations and forwarding its results, without congesting the Flow-service.

What are your thoughts on this?

Enjoy & stay queryious 😉

The post Export data from Power BI using Microsoft Flow appeared first on The BIccountant.


Viewing all articles
Browse latest Browse all 111

Trending Articles