Table.Buffer

D

T

The M Code Behind the Power Query M function Table.Buffer

In this article, we will dive into the M code behind the Table.Buffer function, exploring how it works and how to use it effectively in your Power Query workflows.

Understanding Table.Buffer

Before we dive into the M code behind Table.Buffer, let’s briefly touch on what this function does and why it’s useful.

When you create a query in Power Query, it’s executed each time you refresh your data. This means that if you have multiple queries that use the same data source or that perform similar transformations, your data is being loaded and processed multiple times. This can slow down your query performance and increase the load on your data source.

Table.Buffer allows you to store the results of a query or transformation in memory, essentially creating a cache that can be used by subsequent queries and transformations. This means that if you have multiple queries that use the same data source or perform similar transformations, they can all reference the cached data, improving performance and reducing the load on your data source.

The M Code Behind Table.Buffer

The M code behind Table.Buffer is relatively simple. When you apply the Table.Buffer function to a table, it creates a reference to that table that is stored in memory. Any subsequent queries or transformations that reference that table will use the cached data instead of re-executing the original query or transformation.

Here’s an example of how you might use Table.Buffer in a simple Power Query workflow:


let

Source = Excel.Workbook(File.Contents("C:data.xlsx"), null, true),

Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],

#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),

#"Filtered Rows" = Table.SelectRows(#"Promoted Headers", each ([Column1] <> null)),

Buffer = Table.Buffer(#"Filtered Rows")

in

Buffer


In this example, we start by loading an Excel workbook and selecting the data from the Sheet1 worksheet. We then promote the headers and filter out any rows where Column1 is null. Finally, we apply the Table.Buffer function to the resulting table.

The M code generated by the Table.Buffer function looks like this:


Table.Buffer()


This line of code creates a reference to the Filtered Rows table and stores it in memory. Any subsequent queries or transformations that reference this table will use the cached data.

Best Practices for Using Table.Buffer

While Table.Buffer can be a powerful tool for improving query performance, it’s important to use it judiciously. Here are a few best practices to keep in mind:

– Only use Table.Buffer on tables that are relatively small or that will be used multiple times in your query workflow. Storing large tables in memory can quickly consume resources and slow down your queries.

– Avoid using Table.Buffer on tables that are already stored in memory, such as those that have been loaded into the Power Pivot data model. In these cases, using Table.Buffer can actually slow down your queries by duplicating the data in memory.

– If you’re working with a large dataset that can’t be stored in memory, consider using other Power Query functions like Table.Combine or Merge to reduce the amount of data that needs to be loaded at once.

– Finally, always test your queries and measure their performance when using Table.Buffer or any other optimization technique. What works well for one dataset or query may not work well for another.

Table.Buffer is a powerful function in Power Query that can help improve query performance and reduce the load on your data source. By understanding the M code behind this function and following best practices for its use, you can take full advantage of its benefits in your Power Query workflows.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)