Power Query Value.Versions Function: Usage and Guide
In this article
The Value.Versions function in Power Query is used to retrieve a list of all available versions of a specific value or object that supports versioning. This is primarily used when interacting with data sources that maintain a history of changes, such as certain SharePoint lists, database snapshots, or specific API endpoints that provide versioned records. By using this function, you can transform a single static record into a historical table, allowing you to compare changes over time or revert to a previous state of a data object.
What You'll Need
- Microsoft Excel or Power BI Desktop with Power Query installed.
- A data source that explicitly supports versioning (e.g., a SharePoint document library or a specific versioned API). Standard flat files like CSVs or basic SQL tables do not support this function.
- Basic familiarity with the Power Query Advanced Editor and the M language.
- Appropriate permissions to access the version history of the source data.
Step-by-Step
- Connect to your Versioned Source: Open Power Query (Data > Get Data in Excel, or Transform Data in Power BI). Connect to your source, such as a SharePoint Online site. Ensure you are connecting to an object that maintains a version history, such as a specific file or a list item.
- Identify the Target Value: Before applying the function, you must have a reference to the value you want to version. For example, if you have a step that isolates a single record or a specific file object from a folder, that step serves as the input for
Value.Versions. - Open the Advanced Editor: Since
Value.Versionsis not available as a button in the ribbon interface, you must enter the M code manually. Go to the Home tab and select Advanced Editor. - Insert the Function: Create a new step in your M code. The syntax is
Value.Versions(value). Replacevaluewith the name of the previous step that contains the object you are analyzing. For example, if your previous step was named"Source", your code would look like:Versions = Value.Versions(Source). - Expand the Resulting List: The function returns a list of versions. In the Power Query editor, you will see a "List" object. Click the "To Table" button in the Transform ribbon to convert this list into a table format.
- Extract Version Details: Once converted to a table, the versions are often stored as records. Click the expand icon (two diverging arrows) in the column header to select the specific metadata you want to see, such as the version number, the timestamp of the change, and the modified-by user.
- Filter for Specific Versions: Use the filter arrows on the column headers to isolate the most recent version or a specific historical date. This allows you to compare the current state of the data against a known previous state.
- Apply Transformations: Now that you have a table of versions, you can perform standard Power Query operations, such as calculating the time difference between versions or identifying which users made the most frequent changes.
Common Mistakes
One of the most frequent errors is attempting to use Value.Versions on a standard table or a basic Excel range. This function does not create a version history for your data within Power Query; rather, it reads a version history that already exists at the source. If the source does not provide versioning metadata, the function will return an error or an empty list.
Another common mistake is forgetting to convert the resulting list to a table. Because Value.Versions returns a List type, users often get confused when they don't see their data immediately. You must explicitly use the "To Table" transformation to make the version records accessible for expansion.
Users also frequently confuse this with "Version Control" in a coding sense. Power Query's Value.Versions is about data state history, not about tracking changes to the M code itself. To track changes to your query logic, you would need to use an external version control system like Git or save different versions of your .pbix or .xlsx files.
Tips
To optimize performance, filter your data before calling Value.Versions. If you apply the function to a large set of objects, Power Query may attempt to fetch the version history for every single item, which can lead to significant slow-downs or API throttling from the server side.
Combine Value.Versions with a custom function if you need to extract the latest version for a large list of files. You can create a function that takes a file object, calls Value.Versions, and then uses List.Last to grab only the most recent entry, avoiding the need to expand every version for every file.
When working with SharePoint, ensure that "Version History" is actually enabled in the site settings. If a site administrator has disabled versioning for a library, Value.Versions will have no data to retrieve, regardless of how correctly the M code is written.
Troubleshooting
If the function returns an error stating that the value does not support versioning, first verify your data source. Try to manually check the version history of the file or item through the source's native interface (e.g., right-click a file in SharePoint and select Version History). If you cannot see versions there, the function will not work.
If the query is timing out or taking too long to load, check the number of versions being retrieved. Some objects may have hundreds of versions. Use a List.FirstN or List.LastN wrapper around the Value.Versions function to limit the number of historical records being pulled into the memory.
If you see a "Formula.Error: The name 'Value.Versions' wasn't recognized," check your capitalization. Power Query (M) is case-sensitive. Ensure the 'V' in Value and the 'V' in Versions are both capitalized exactly as shown. If the error persists, ensure you are using a modern version of Power BI or Excel, as very old versions of the Mashup engine may not support this specific function.
Frequently Asked Questions
Does Value.Versions work with local CSV files?
No. Local flat files like CSV or TXT do not have built-in versioning metadata that Power Query can access. This function requires a source that manages its own versioning, such as SharePoint or certain cloud-based databases.
Can I use this to see who changed a cell in Excel?
Not directly. Value.Versions works on the object level (like a whole file or a record). It cannot track changes to individual cells within a spreadsheet unless that spreadsheet is stored in a versioned environment and you are tracking versions of the entire file object.
Is there a limit to how many versions I can retrieve?
The limit is determined by the data source, not Power Query. For example, SharePoint has a maximum number of versions it stores based on the site settings. Power Query will simply retrieve whatever the source provides.
How is this different from the "Previous Version" feature in Windows?
Windows "Previous Versions" relies on Volume Shadow Copy or File History at the OS level. Value.Versions interacts with the API of the data source itself to retrieve metadata, making it a programmatic way to access history rather than a manual OS restore.
Can I use this to create an audit trail?
Yes, this is one of the primary use cases. By expanding the version list and extracting the "Modified By" and "Modified Date" fields, you can build a comprehensive audit log of how a specific record has evolved over time.