"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 :
to RETURN, or not to RETURN
Like it was mentioned in the Fabric Community link, this is a rule syntax.
1st case: if you want to use the local variable inside EVALUATE:
EVALUATE
VAR _return = "test"
RETURN
{ _return }
This works because you are defining a local variable _return inside an EVALUATE statement and immediately returning it.
In other words, the scope of _return is within the EVALUATE block, so it inaccessible to the RETURN statement.
2nd case: using global variable with EVALUATE:
DEFINE
VAR _return = "test"
EVALUATE
{ _return }
Here, _return is defined globally using DEFINE outside of any EVALUATE context, making it accessible to any EVALUATE statement that follows. You are then using this variable directly inside an EVALUATE block, so it works.
Coming to the cases where it is failing:
3rd case: DEFINE with EVALUATE and RETURN:
DEFINE
VAR _return = "test"
EVALUATE
RETURN
{ _return }
The problem here is that EVALUATE expects a table expression to follow it directly. So, when you insert RETURN immediately after it without any table expression, it will fail. Keep in mind thatRETURN is not a standalone statement in the context of DAX queries but is used within VAR to return a value.
4th case: DEFINE and RETURN without EVALUATE:
DEFINE
VAR _return = "test"
--EVALUATE
RETURN
{ _return }
Here you are trying to use RETURN without an enclosing EVALUATE statement, which is not how RETURN is designed to be used in the context of DAX queries. RETURN is used to return the result of a VAR within an EVALUATE statement or similar contexts, not as a standalone statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With