Improve refresh time by batching Web API calls – Part 2

Following my last post here, Imke Feldmann, author of THE BICCOUNTANT, proposed a refined version for the solution. While my original solution is simpler for users who are reluctant of manipulating Power Query (M) expressions, Imke’s solution is simpler for advanced users. It contains less steps (It’s shorter by 8 UI step, and 10 transformation steps in M) and it’s more robust.

Here is the original expression we created through the Query Editor in Part 1:

While that solution was simple to follow and was 100% driven by the user interface (didn’t require any tweaks on M), it has a major disadvantage. If you will run it on a table of less than 100 rows, the query will break.

Let’s demonstrate our edge case:

Running the query on 80 symbols (In my case I just added a third step and kept the first 80 rows by clicking Home –> Keep Rows –> Keep Top Rows) will fail with an error.

In the highlighted step below, you can see that we have a single row following the Pivot step, with columns 0,1,2,…79.

Improving web queries in Power Query by using batching

Then in the next step, Power Query will fail to merge the columns:

Improving web queries in Power Query by using batching. The solution from Part 1 will fail in this edge case.

The reason for the failure is that the Query Editor created for us the following line in the M expression:

As you can see, all the column names are hard-coded in the expression above. Power Query expects to merge 100 columns, but columns 80, 81, 82, … , 99 are missing, so the transformation step fails.

While the immediate solution to fix this step is to rewrite it, as I recommended here, Imke’s approach is even better.

Instead of the pivoting, and the merging of columns that we used in Part 1, we will group the rows into a sub-tables of 100 rows each, convert each of the 100 symbols in the sub-tables into a list, and apply Text.Combine to convert the list into text.

Before I show you the complete solution step-by-step, here is the improved version.

For those of you who wish to implement the solution step by step, here is the walkthrough:

The Walkthrough

Download this Excel workbook to start the tutorial.

Select any cell in table Symbols and click From Table in the section Get & Transform of Data ribbon (Note: if you are not on Excel 2016, you will find From Table in Power Query ribbon after you install the add-in. You can also follow this tutorial with Power BI Desktop).

In Create Table select My table has headers and click OK.

In the Query Editor window, select the column Symbol and click Add Index Column in Add Column tab.

Select the column Index, and in Transform tab, select Standard and click Integer-Divide.

In Integer-Divide set 100 as Value, and click OK.

Select column Index, and click Group By.

Improving web queries in Power Query by using batching. Use Group By.

Set Symbols as New column name and select All Rows as Operation. When you are done click OK.

Improving web queries in Power Query by using batching. Use Group By.

Following the last step we now have sub-tables of 100 symbols on each row (as shown in the screenshot below). Now it’s the time to tweak the formula.

Improving web queries in Power Query by using batching.

In the formula bar add [Symbol] after the underscore (_) and change the type from table to list as shown below. Once you press Enter, you will see that the table objects are now list objects. Each list contains 100 symbols.

Improving web queries in Power Query by using batching. Use Group By.

And now the final adjustment of the formula:

Wrap the _[Symbol] part with a Text.Combine as shown below. Make sure the second argument is “+” and the type is changed from list to text.

Improving web queries in Power Query by using batching. Use Group By.

From here you can proceed with the steps of Part 1, and add the function, invoke it and expand the results.

Thank you Imke for directing me to Text.Combine 🙂

I think it is a great training material.

2 comments

  1. MarcReply

    Exactly what I was looking for and its surprisingly easy, if you know how. My thoughts for a possible solution were way to complicated.

    Thanks a LOT!

Leave a Reply