Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

DAX Add months to SELECTEDVALUE Date

Hi,   I have a measure in which I want to make use of the date that has been selected in an unrelated Calendar table, but the measure should return values related to two months further.     L3....
  • Anonymous's avatar
    Anonymous
    5 years ago

    It seems that I found the solution, but I don't understand why it works at all.

    I now use the following measure:

    L3.2 = 
    VAR _SelDate = 
        FORMAT(DATEADD('DIM Kalender'[Datum],2,MONTH), "YYYYMM")
    RETURN  
    CALCULATE(
        COUNT('FACT ACM'[EAN]),
            'FACT ACM'[Subonderwerp] = "L3.2",
            FORMAT('FACT ACM'[InvoiceDate],"YYYYMM") = _SelDate
    ) 

    When I make a selection in the slicer above and select August, it returns the values of October. However, this Calendar table HAS NO RELATIONSHIP with the table 'FACT ACM'. What is the reason why this works without SELECTEDVALUE?

     

    Kind regards,

    Igor

  • tex628's avatar
    tex628
    5 years ago

    Sorry I just realized, When you perform a SELECTEDVALUE() on a column you are no longer working with a column but instead with a single value. Dateadd() requires a columnstatement to work so the combination of selectedvalue() and dateadd() will never work! 

    You can work around this by simply adding months manually, along the lines if this:

    VAR NewMonth = IF( MONTH( Calendar[Date] ) = 11 , 1 , IF( MONTH( Calendar[Date] ) = 12 , 2 , MONTH( Calendar[Date]) +2 ))
    VAR NewYear = IF( MONTH( Calendar[Date] ) > 10 , YEAR( Calendar[Date] ) +1 , YEAR( Calendar[Date] ))

    This would mean that you can completly skip the dateadd, meaning that you can use selectedvalue() 

    But if I understood you correctly you already got it to work! 🙂 

    /J