"Have(ing) fun with DAX" and wondering why the RETURN statement is not suitable for my examples in the 'NOT works' section. Thank you in advance.
-- DEFINE can only be used once ie globals
-- EVALUATE VAR RETURN can be used multiple and nested
-- Working
EVALUATE
VAR _return = "test" -- create local variable
RETURN
{ _return }
DEFINE
VAR _return = "test" -- create global variable
EVALUATE
{ _return }
-- Not working
DEFINE
VAR _return = "test"
EVALUATE
RETURN
{ _return }
DEFINE
VAR _return = "test"
--EVALUATE
RETURN
{ _return }
Power BI Community forum post link of same question :
Like it was mentioned in the Fabric Community link, this is a rule sytax.
1st case : if you want to use the local Variable Inside EVALUATE:
This works because you are defining a local variable
_returninside anEVALUATEstatement and immediately returning it.In other words, the scope of
_returnis within theEVALUATEblock, so it isaccessible to theRETURNstatement.2nd case : Using global Variable with EVALUATE:
Here,
_returnis defined globally usingDEFINEoutside of anyEVALUATEcontext, making it accessible to anyEVALUATEstatement that follows. You are then using this variable directly inside anEVALUATEblock, so it works.Coming to the cases where it is failing :
3rd case : DEFINE with EVALUATE and RETURN:
The problem here is that
EVALUATEexpects a table expression to follow it directly. So, when you insertRETURNimmediately after it without any table expression, it will fail. Keep in mind thatRETURNis not a standalone statement in the context of DAX queries but is used withinVARto return a value.4th case : DEFINE and RETURN without EVALUATE:
Here you are trying to use
RETURNwithout an enclosingEVALUATEstatement, which is not howRETURNis designed to be used in the context of DAX queries.RETURNis used to return the result of aVARwithin anEVALUATEstatement or similar contexts, not as a standalone statement.