Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Value.Optimize Function Guide and Usage

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Value.Optimize function in Power Query is used to improve the performance of data refreshes by reducing the number of times a specific value or expression is recalculated. It achieves this by caching the result of a function or a value, ensuring that if the same input is encountered again during the evaluation of a query, the engine uses the cached result rather than executing the logic from scratch. This is particularly useful when dealing with complex calculations, API calls, or heavy transformations that are repeated across many rows of a dataset.

What You'll Need

  • Microsoft Excel or Power BI Desktop with Power Query installed.
  • A dataset that involves repetitive, resource-intensive calculations (such as a custom function applied to a large column).
  • Access to the Advanced Editor to implement the function, as Value.Optimize is not available via the standard ribbon interface.

Step-by-Step

  1. Identify the Bottleneck: Before applying optimization, identify which part of your query is slow. Typically, this is a custom function called within a Table.AddColumn step. If you notice that the same values are being processed repeatedly, this is a prime candidate for Value.Optimize.
  2. Open the Advanced Editor: In the Power Query Editor, go to the Home tab and select Advanced Editor. This allows you to modify the M code directly. Labels and menu paths may vary slightly depending on whether you are using Excel or Power BI.
  3. Define the Value to Optimize: You must wrap the expression or function you want to cache inside the Value.Optimize function. The syntax is Value.Optimize(expression). For example, if you have a complex calculation defined as a variable, you would wrap that variable.
  4. Implement the Wrapper: Instead of calling your function directly in a table transformation, create a new step or variable that defines the optimized version of that function. Example: let OriginalFunction = (x) => /* complex logic here */, OptimizedFunction = Value.Optimize(OriginalFunction), AddedCustom = Table.AddColumn(Source, "Result", each OptimizedFunction([Column1])) in AddedCustom
  5. Apply the Optimized Function to the Column: Use the Table.AddColumn or Table.TransformColumns function, but ensure you are referencing the variable that was wrapped in Value.Optimize.
  6. Verify the Performance: Run the query. You should notice a decrease in refresh time, especially if your dataset contains many duplicate keys that trigger the same calculation.
  7. Close and Apply: Once the logic is verified in the Advanced Editor, click Done and then select Close & Apply to load the optimized data into your model.

Common Mistakes

One of the most frequent mistakes is using Value.Optimize on values that are unique for every single row. The function works by caching results based on inputs; if every input is unique, the engine still has to perform the calculation for every row, and the overhead of managing the cache may actually slow down the query slightly. Only use this function when there is a high degree of redundancy in your input data.

Another common error is placing the Value.Optimize call inside the each loop of a Table.AddColumn step. If you write Table.AddColumn(Source, "Custom", each Value.Optimize(MyFunction)([Column1])), you are essentially telling Power Query to create a new optimized function for every single row. This completely defeats the purpose. The optimization must happen outside the row-by-row iteration so that a single cache is shared across the entire operation.

Users also frequently confuse Value.Optimize with List.Buffer or Table.Buffer. While buffering loads a whole table or list into memory to prevent multiple reads from the source, Value.Optimize is specifically for the result of an expression or function. Using a buffer when you need value optimization (or vice versa) will not yield the desired performance gains.

Tips

To maximize the effectiveness of Value.Optimize, try to simplify the inputs passed to the function. If your function takes three different columns as arguments, the cache key is a combination of those three values. If you can consolidate those into a single unique key (like a concatenated string), the cache hits may increase, leading to faster performance.

Combine this function with Query Folding whenever possible. While Value.Optimize happens within the Power Query engine (the "Mashup" engine), ensuring that as much data as possible is filtered at the source (SQL server, etc.) reduces the number of rows that the optimized function needs to process in the first place.

When working with API calls, be cautious. Value.Optimize can reduce the number of requests sent to a server by caching the response for a specific parameter. This not only speeds up your query but also helps you avoid hitting API rate limits or being blocked by the server for making too many identical requests in a short window.

Troubleshooting

If the query does not seem to run any faster after applying Value.Optimize, first check your data distribution. Use a "Group By" operation on the input column to see how many unique values exist compared to the total row count. If the number of unique values is nearly equal to the total rows, Value.Optimize will not help.

If you encounter a memory error (Out of Memory), it may be because the cached results are too large. Value.Optimize stores results in RAM. If your function returns very large objects or long strings and you have thousands of unique inputs, you may exhaust your available memory. In this case, try to reduce the amount of data the function returns or filter the table more aggressively before the optimization step.

If the results of the function seem "stale" or incorrect, ensure that the function being optimized is deterministic. A deterministic function always returns the same output for the same input. If your function relies on a volatile value (like DateTime.LocalNow()), the cached result will be based on the moment the first call was made, and subsequent calls will return that same cached time instead of the current time.

Frequently Asked Questions

Does Value.Optimize work with all data types?

Yes, it can be used with any data type that the M language supports, including text, numbers, dates, and complex types like lists or records, provided the inputs can be compared for equality to trigger a cache hit.

Is Value.Optimize available in the user interface?

No, there is no button or menu option for this function in the Power Query ribbon. It must be implemented manually by editing the M code in the Advanced Editor.

How does this differ from a Merge operation?

A Merge operation joins two tables based on a key. Value.Optimize is used when you are calculating a value via a function. While you could sometimes replace a function with a lookup table and a Merge, Value.Optimize is more efficient when the value must be computed dynamically rather than retrieved from a static list.

Can I use this to speed up web scraping?

Yes, if you are scraping multiple pages but some pages are requested multiple times, wrapping the web request function in Value.Optimize will prevent the engine from downloading the same page twice.

Does it increase the size of the final data model?

No, Value.Optimize affects the processing time during the refresh. It does not change the structure or the size of the data that is eventually loaded into the Power BI or Excel data model.