Closed edhans closed 5 months ago
Hi Ed, The current behavior is actually by design, but we're certainly open for discussion on this. We treat a variable as being unused only if there are no references to that variable anywhere in the code. If a variable is indirectly unused, such as in your example, then we still regard it as being in use - chances are that you're currently writing a DAX expression that you just haven't completely finished. On the other hand, if you are cleaning up some old code, we still show you that at least one variable is unused, and when you delete that, any leftover unused variables should then light up.
Btw. please note that we are changing this feature slightly in 3.13.0, so only the variable name and not the full expression gets highlighted, if the variable is unused (as per #1173).
Thanks. Makes sense. I like the new way to only highlight the variable name vs all of the code.
I'm ok with just showing variables that have no references. When cleaning up code, I'd need to work backwards to get them all, but without this, they would all show up as I was writing code, so no perfect answer here.
This is super useful. I've already found a bug in my code because of this - code I wrote that should have been referenced was not. Some yearly calcs were referencing a quarterly table variable. 😩
@otykier Hi Daniel, I love the new warning. Is helping us a lot!
Is there a way to, programmatically with a C# script, identify all the measures with unused variables?
Thanks!!
This script works, but it uses the undocumented GetCachedSemantics()
method, which is not available in TE2 and which may change in the future.
using Dax.Analyzer.Ast;
var output = "";
foreach (var measure in Model.AllMeasures)
{
var messages = measure.GetCachedSemantics().Messages;
if(messages.FirstOrDefault(m => m.Type == MessageType.Warning && m.Message.StartsWith("Variable") && m.Message.EndsWith("is declared but never used.")) is DocumentMessage message)
{
output += measure.DaxObjectFullName + ": " + message.Message + Environment.NewLine;
}
}
Info(output);
@otykier Thank you so much!
Description
This might be by design, but not sure. Here is my measure. As you can tell there is a bunch of code I abandoned and will now comment out so this new feature doesn't report it, but it is only telling me the Result variable is unused, when in fact all of them are unused on lines 21-26.
Tabular Editor 3 Version
3.12.1
Screenshots
No response
Steps to Reproduce
No response
Expected behavior
No response
Crash Report
No response
Windows Version
windows 11