You ask, I answer.
Amira Bedhiafi
Data Witch | Microsoft MVP Data Platform | Microsoft MVP Power BI | Power BI Super User | C# Corner MVP | Full Stack Business Intelligence Engineer
The OP is trying the understand why the RETURN statement is not suitable for the showing examples ?in the 'NOT working' section.
The structure implies the use of DEFINE strictly as a singular instance for global declarations, while EVALUATE and VAR RETURN can be applied repetitively and in nested scenarios.
- Working scenario:
```
EVALUATE
??? VAR _return = "test" // Initializes a local variable
RETURN
??? { _return }
DEFINE
??? VAR _return = "test" // Establishes a global variable
EVALUATE
??? { _return }
```
- Non-working senario:
```
DEFINE
??? VAR _return = "test"
EVALUATE
RETURN
??? { _return }
DEFINE
??? VAR _return = "test"
//EVALUATE
RETURN
??? { _return }
```
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 isaccessible 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.