Adjust Power Query Table Style and Properties

How to adjust the Default Table Style and the Autofit Column Width Property manually and automatically for all Query Tables in a Workbook

960 words, ~5 minutes read

Power Query Table Properties - IntroVery often, you get and transform data using Power Query and then load it into a table on a worksheet of your Excel workbook. When loading it for the first time, Power Query makes some decisions for you. Regarding the properties of the query table, but also regarding the used table style.

Most of theses decisions are fine. Especially two of them can become a pain in the neck, though.

First one is autofitting the column widths of the sheet to fit to the size of the header and the data. This is great, if you have only the table itself on the sheet, but it can be annoying, if you have also texts or calculations above the query table.

Second issue is the table design. Power Query uses the built-in Green Table Style Medium 7, not the default Table Style of the workbook.

I do not know of a way in Power Query to change these settings, so you always have to adjust your query table after the first load.

Today’s post describes how to do these adjustments manually for one query table and also provides a ready-to-use VBA code snippet to change properties and styles for all tables in a workbook in one go.

Issue 1 – Default Table Style used by Power Query

If you load the data of a query to a table on a worksheet in Excel, Power Query does not use the default table style set for this workbook. For whatever reason, it uses the built in Green Table Style Medium 7, even if you explicitly selected another style as default:

Power Query Default Table Style - Green Table Style Medium 7There is nothing wrong with this table style, but very often, I want to have another one. The manual fix is easy: select a cell inside the table, go to the [Table Design] tab of the Ribbon and select the style you want in the section Table Styles.

The annoyance: if you create another query and load it, you have the same issue again and there is no way to define the default Table Style in Power Query.

Issue 2 – Autofit the Column Widths

Per default, Power Query automatically adjusts the column widths of the table after loading it into a worksheet. This is well-meant, because it is helpful especially when you have only the table on a worksheet and nothing above it like some descriptive texts or cells with a calculation.

Here is one example: it may be just me, but I prefer to add SUBTOTAL calculations above the table to see aggregations on the filtered data:

Subtotal Aggregation above the Query TableI know of the option to add a total row to the table in the Table Style Options, but truth be told, I am not the biggest fan of this feature, because the total row is at the end of the table and to see the results, you always have to jump to the end. I prefer having SUBTOTAL calculations above the table and freeze the panes to always show the first 2 rows. By this, I can always see the results of the aggregation on the filtered data.

This turns into a problem, if you refresh the query, because when autofitting the column width, Power Query calculates the column widths only based upon the content in the table, i.e. the column header and the data. It does not care about the required width of the SUBTOTAL cell above the table. What you get is this:

Issue - Autofit Column Width obscures the Subtotal resultYou can manually resize the column width, of course, but you will have to do this after every refresh of the query. One work-around would be to insert the total row at the end of the table and use the same aggregation function as you do in the SUBTOTAL. Since the total row belongs to the table, the column width will be wide enough to show the number.

An even better option is to tell Power Query not to autofit the column widths anymore:

Select a cell inside the table, right click, click on |Table| and |External Data Properties|:

External Data PropertiesIn the upcoming window, uncheck the box left to |Adjust column width|:

Uncheck Adjust Column WidthThis solves the problem, but just as with the Table Style, the setting applies only to this specific query table. If you build another query and load it to a table, you have the same issue again.

Change the Table Style and the Adjust Column Width Property for all Query Tables

The issues described above count for little, if you have only a few query tables in your workbook, because the manual adjustments are quickly made.

However, if you have an Excel workbook using 20, 30 or even more queries loading to tables in worksheets (and I do have this situation in my project more often than you would think), the manual adjustments can become a pain in the neck.

To make things a bit easier, I wrote a little VBA routine which loops through all worksheets of the active workbook and sets the table style and the properties of all query tables as desired. If you are interested, here is a text file containing the code snippet:

Download Adjust Query Table Properties VBA (text file, 4K)

Download the file, open it with a text editor and copy the entire code. Next, open your Excel workbook, go to the VBE (ALT-F11), insert a module (ALT-I-M), paste the code in and run the sub (F5).

This is a one-off solution, i.e. once you ran the sub and double checked that it has done the job, you can delete the module in the VBE.

Stay tuned.

Comments

10 responses to “Adjust Power Query Table Style and Properties”

  1. Microsoft Excel Recalc or Die Avatar

    “Small little things” like this one (a macro to adjust Column Width Property for all Query Tables…) have great impact, and also can change or alter the state of humor for someone 🙂
    thanks for sharing it! danke!!!

  2. Tali Avatar
    Tali

    Thanks! This is very clear and helpful timesaver.

  3. Mark Avatar
    Mark

    I was beginning to think I was the only one finding this annoying! Not that there is anything wrong with it, but I just don’t care for the green table style. The column autofit is a real pain!
    This is perfect, thanks very much!

  4. CVRAMANA Avatar
    CVRAMANA

    thanks for such a wonderfull help

  5. Kenny Avatar
    Kenny

    Ironically the default style falls foul of the Accessibility Inspection function in Excel 2019 due to “hard to read Text Contrast”!
    I could add some VBA to the worksheet’s Worksheet_TableUpdate event to address the formatting but Power Query has made VBA redundant in most of my worksheets and it’s just something else to maintain.

  6. erichx Avatar
    erichx

    Thank you. Very nice web page!

  7. BathindaHelper Avatar

    Great!!
    One thing though, why can’t I drag and change the width of column headers inside the query editor?
    I need to resize column widths in QEditor, say, just for vision sake?

  8. Robert Avatar

    BathindaHelper,
    simply click on the right border (right to the filter arrow down) of the column header in the Power Query Editor and drag to the left or right to decrease or increase the column width.

  9. William Avatar
    William

    Another way to do this is wrap text on the whole top row of headers and also center the column headings instead of left or right.. resize column headings as well, shrink to fit, this is easier than VBA or macros…

  10. Robert Avatar

    William,
    I disagree.
    First of all, the code provided in the workbook above simply loops through all tables in the workbook, disables the automatic adjustment of the column widths and sets the table style to the workbook’s default table style. You can do this manually, too, without using the VBA code.
    The issue I am seeing with the procedure you are suggesting is the fact that you have to repeat the manual steps you are describing every time after you refreshed the query. If you disable the automatic adjustment of the column width in the table settings (either manually or by VBA), you do not have to change the column widths after a query refresh anymore.
    So, I agree, you do not necessarily have to use the VBA code, but deactivating the Adjust Column Width setting of the table saves a lot of time compared to your suggestion.

Leave a Reply to erichx Cancel reply

Your email address will not be published. Required fields are marked *