Forum Discussion

RvdHeijden's avatar
RvdHeijden
Icon for Post Prodigy rankPost Prodigy
9 years ago
Solved

Need help with my formula

This is a strange one because my formula did work but now it returns an error.   This is my formula:   Doorlooptijd = IF ( RELATED ( 'Date'[IsWorkday] ) = 1; IF ( schades[binnengekomen] = Sc...
  • AlbertoFerrari's avatar
    AlbertoFerrari
    9 years ago

    Well, if you need to compute the formula for the number of working days between open and close, then your code is not correct. What you are computing is the number of days between the two dates, blanking it in the very special case when the date on which you base the relationship is not a working day. Thus, sorry to say that, but your code looks wrong.

     

    With that said, the error message seems to indicate that there is something wrong with the data, a refresh of the model might fix it (never seen it, but this is what the error message says), the formula - although semantically wrong - looks fine.

    If you need to compute the difference, in working days, between the two dates, you can easily build a calculated column like this (I wrote the code using Contoso, so it might be different in your specific case)

     

    Delta in Working  Days = 
    
    VAR StartDate = Sales[Order Date]
    VAR EndDate = Sales[Delivery Date]
    
    RETURN
    
    CALCULATE ( 
        COUNTROWS ( 'Date' ),
        DATESBETWEEN( 'Date'[Date], StartDate, EndDate ),
        'Date'[Working Day] = "Workday"  
    )

     

    Worth to note that DATESBETWEEN ignores the relationship, so you can (and should) keep it active, otherwise the report will become wrong, because of the missing relationship.


    Have fun with DAX!

    Alberto Ferrari
    http://www.sqlbi.com

  • AlbertoFerrari's avatar
    AlbertoFerrari
    9 years ago

    Just replace EndDate with this:

    VAR EndDate = IF ( ISBLANK ( Sales[Delivery Date] ), TODAY (), Sales[Delivery Date] )

     

    I created the two variables for this purpose, then I forgot to use them :)