Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Element115
Power Participant
Power Participant

DAX interpreter BUG!!!

The following (code in red) works when run in the Power Bi Desktop version, but as soon as you replace it by the variable name, which contains the same code, Power BI generates an error.  In a nutshell, the column [UnreachableSince] is of data type TEXT containing textual representations of DATETIME values and other non-datetime values (I know and there is nothing I can do about this).


To intercept the DATETIME values, therefore an IF() is used.  It all works fine until you decide to use the variable name in place of the inline code inside the nested IF().

So this OK:

DATEDIFF(DATEVALUE([UnreachableSince]) + TIMEVALUE([UnreachableSince]), [ClosedDT], SECOND)


But this NOT OK!

DATEDIFF(__unreachable_dt, [ClosedDT], SECOND),

 

Code here (sorry but can't add colors when using Insert/Edit code sample from this editor toolbar):

 

ADDCOLUMNS(
    GROUPBY(
       ADDCOLUMNS(
            CALCULATETABLE(
                 SELECTCOLUMNS(
...
                 ),
            FILTER(
...
            )
),
"DownTime", IF ( [ErrorCategory] == "Unreachable",
                         VAR __unreachable_dt = DATEVALUE([UnreachableSince]) + TIMEVALUE([UnreachableSince])
                         VAR __unreachable_dt_in_secs = DATEDIFF(__start_date, __unreachable_dt, SECOND)
                         RETURN
                               IF( __unreachable_dt_in_secs >= 0,
                                     DATEDIFF(DATEVALUE([UnreachableSince]) + TIMEVALUE([UnreachableSince]), [ClosedDT], SECOND),
                                     BLANK() //DATEDIFF([CreationDT], [ClosedDT], SECOND)
                                ),
                          DATEDIFF([CreationDT], [ClosedDT], SECOND)
                     )
),
[AssetName],
"DownTime", SUMX(CURRENTGROUP(), [DownTime])
),
"% up", 1 - ([DownTime] / __in_service_time)
)

 
So am I imagining things, or isn't this a DAX interpreter bug?


5 REPLIES 5
Element115
Power Participant
Power Participant

PBI Desktop vs = July 2022

Hi, @Element115 

Although variables can be used anywhere, however, their result might not be always the same. Because they are evaluated in the context in which they are written. 

Please refer to this thread for more details.

Caution When Using Variables in DAX and Power BI 

 

Best Regards,
Community Support Team _ Eason

 

 

Hello, any updates?

One more thing... I read RADACAD's explanation, however it does not apply here.  Look again carefully at the code I posted:

 

 

"DownTime", IF ( [ErrorCategory] == "Unreachable",
                         VAR __unreachable_dt = DATEVALUE([UnreachableSince]) + TIMEVALUE([UnreachableSince])
                         VAR __unreachable_dt_in_secs = DATEDIFF(__start_date, __unreachable_dt, SECOND)
                         RETURN
                               IF( __unreachable_dt_in_secs >= 0,
                                     DATEDIFF(DATEVALUE([UnreachableSince]) + TIMEVALUE([UnreachableSince]), [ClosedDT], SECOND),
                                     BLANK() //DATEDIFF([CreationDT], [ClosedDT], SECOND)
                                ),

 

 

 

__unreachable_dt is declared right before the RETURN statement and when referenced in the RETURN block inside de DATEDIFF() inside the IF() as the 2nd parameter, the result is not that the computation is wrong.  

In other words, the block of code is

 

 

IF( condition,
    VAR __declaration0 = ...
    VAR __declaration1 = ...
    
    RETURN
        IF( __declaration1 >= 0,  // no issues here referencing the second variable 
            DATEDIFF( __declaration0, ... ) // issue here CAN'T FIND THE VARIABLE!!!
...

Is this really as it should be???

 

 

The result is that the interpreter complains the variable does not exist or is not defined!  Two very different things, yes?

TBH what you are saying makes no sense to me:  variables 

are evaluated in the context in which they are written.

 

Variables should be visible in the scope in which they are declared, period.  Are you saying that DAX scoping isn't following what we are used to with all major programming languages out there?  

 

In any case, all my variables are usually declared top level, ie right after the 'MeasureName = ' line.  Therefore I would expect this scope to be visible all the way down to the RETURN statement.

Isn't it so?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors