When working in the Power Query Editor in Power BI or Excel, especially when you’re editing M code, you may run into the following error:
Expression.Error: A cyclic reference was encountered during evaluation.
What does this message mean and what causes this error? In this blog post I’ll explain.
The first step to understanding what’s going on here is to understand how Power Query evaluates the steps in a query. I wrote a blog post on let expressions in M some time ago, which you can read here, and which goes into a lot of detail on this subject but the basics are quite easy to understand. Conside the following M query:
let Step1 = 1, Step2 = Step1 + 5, Step3 = Step2*2 in Step3
If you paste this into a new blank query in the Power Query Editor you’ll see it results in three steps in the Applied Steps on the right hand side of the screen:
The query itself, as you would expect, returns the value 12:
The important thing is to know that Power Query does not evaluate the steps in the order that they appear here – so it does not evaluate Step1 first, then Step2 and then Step3. What it does is evaluate Step3 first, which is needed for the output of the query, and then because it needs the value of Step2 to do this it evaluates Step2 next, and then because it needs the value of Step1 to evaluate Step2 it evaluates Step1 last. You can visualise the chain of dependencies between these steps like so:
Now consider the following M query:
let Step1 = Step3, Step2 = Step1 + 5, Step3 = Step2*2 in Step3
The change is that Step1 is now set to the value of Step3 instead of the value 1. This query gives you the “A cyclic reference was encountered during evaluation” error and you can probably see why: the query returns the value of Step3, which needs the value of Step2, which needs the value of Step1, which needs the value of Step3 – so there’s no way of calculating the output of the query. There’s a loop in the dependencies between the steps:
This is the cyclic reference (sometimes known as a “circular reference”) from the error message. As you can imagine it’s very easy to create cyclic references when you’re editing M code manually and changing step names.
It’s also possible to run into this error when using referenced queries, that’s to say when one Power Query query uses the output of another Power Query query as its data source. For example, let’s say you had a query called Query1 with the following M code:
let Source = Query2 in Source
…and a query called Query2 with the following M code:
let Source = Query1 in Source
Once again there is a cyclic reference: Query1 refers to the output of Query2 and Query2 refers to the output of Query1. The error message returned for Query2 is:
An error occurred in the ‘Query1’ query. Expression.Error: A cyclic reference was encountered during evaluation.
An extra problem in this case is that if you try to delete just one of these queries, you’ll get the following error message:
The query “Query1” cannot be deleted because it’s being referenced by another query: “Query2”.
You have to select both queries and then delete them, or of course edit one of the queries to break the cyclic reference.
One last thing to mention: recursion is possible in M, meaning that you can write a custom function that calls itself (Miguel Escobar has a detailed post on this here) but it can be very hard to get it working and it’s not always the most efficient way to solve a problem.