Recently I found a PowerQuery gem, trick with Java/VB Script that allows to export data from Power Query to CSV without R / DAX Studio / SMS and Registration. However, related with risk. As everything else in our life.

Kudos to user Shi Yang from Stack Overflow who replied to How to write to DATA sources with Power Query?.

Shi proposes to use following code (extended with my comments)

All great, but this method doesn’t work with default settings of Internet Explorer.

Yes… yes… IE is involved somehow.

To make it work, you need to open IE, go ‘Internet options

Tab ‘Security‘ then ‘Custom level…

Find setting ‘Initialize and script ActiveX controls not marked as safe for scripting (not secure)

Unfortunately, this is a risky option, as it potentially allows to execute any script on your workstation.

But if you really need to export something to CSV… you probably allowed to change this setting on your corporate laptop… right?

For your convenience, IE will show notification about security risk each time when you open it

You may click ‘Fix settings for me‘ to restore secure configuration.

Back to Export to CSV

Proposed code is basically a script that writes text file using CreateTextFile method

Script creates FileSystemObject needed to work with files as with objects, then creates TextFile (with overwrite option = true).

Then saves prepared Json into variable, adds line breaks and finally writes everything into text file.

Data Volume

First thing I did – tried to export something not small but not too big. It was a table about 200k rows and 15 columns. Script successfully exported data. Then I doubled volume – went well, tripled – no CSV file were created.

Obviously, limit on data volume exists. Json can’t be infinite large.

I also tried to export table with over one million of rows but got ‘Out of memory’ message.

It is clear that we will need paging if we want to export huge tables.

And when I hear ‘paging’ I think about List.Generate.


Instead of CreateTextFile we will need OpenTextFile method, that also allows to create file, but in addition allows to append to existing file.

Idea of test – take a small table and try to record it to CSV several times.

What if Power Query will be so fast that create a conflict while recording to file, e.g. if PQ create multiple threads? – i thought.

And I decided to add a delay between appending.

We can start with a function

However, I couldn’t achieve stable number of rows in resulting file.

Even if I set Delay to 3 seconds or 5, I still get my table written to file two or three or five times.

Had no time to explore this further. Share as is.

I think that in Excel iteration can be done through VBA code that can change code of PQ queries…

You can download my sample file from here.

P.S. – not a single corporate laptop was at risk during writing of this post.

Leave a Reply