Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

DATEADD not working in Matrix

I created a measure that shows revenue from the 12 months before the last 12 months using DATEADD function.  I put it in a MATRIX and it works fine when there are no row or column headings added to the MATRIX.  (See below)

 

However, when I try to add a dimension to rows, it gives me the following error:

 

 

Does anyone know how to fix this?  Thanks in advance!

 

  • Hi,

     

    According to your description, i create a sample to test:

    Then create a date slicer table:

    Date Slicer = DISTINCT(SELECTCOLUMNS('Table',"Date",'Table'[Date]))

    Then try this measure:

    Measure = 
    var a = SELECTEDVALUE('Date Slicer'[Date])
    return
    CALCULATE(SUM('Table'[accrual_revenue]),DATESBETWEEN('Table'[Date],DATE(YEAR(a)-2,MONTH(a),DAY(a)),DATE(YEAR(a)-1,MONTH(a),DAY(a))))

    When choosing one date in date slicer, the result shows the total revenue from the 12 months before the last 12 months based on the date selected:

    Tips: In this case, i add a defined filter to the table visual to show the result more obviously.

    Here is my test pbix file:

    pbix 

    Hope this can help.

     

    Best Regards,

    Giotto Zhi

     

6 Replies

  • Anonymous 

    For all such calculation use a date calendar and join the Date of Calendar with your date.

     

    Something like

    Last YTD Sales = CALCULATE(sum('PL lines'[PL Line.1]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))
    Last YTD complete Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"12/31"))

     

    To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
    https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
    https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
    https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

     

    if you need more help make me @

    Appreciate your Kudos.

    • Anonymous's avatar
      Anonymous
      Not applicable

      amitchandakThanks.  I do have a date calendar thats joined to my table date.  I believe your formula examples do not pull prior 12 months before the last 12 months.

       

      This formula works fine except when a row dimension is added to the matrix.

       

      Prior 12 mo Rev = Calculate(sum('TKD'[accrual_revenue]),DATESINPERIOD(TKD[Date],ENDOFMONTH(DATEADD(TKD[Date],-12,MONTH)),-12,MONTH))

       

      • amitchandak's avatar
        amitchandak
        Icon for Super User rankSuper User

        Anonymous 

        Yes when you view by few dimension not all dates world be there, Try if one of 2 works

         

        Prior 12 mo Rev = Calculate(sum('TKD'[accrual_revenue]),DATESINPERIOD(Date[Date],ENDOFMONTH(DATEADD(TKD[Date],-12,MONTH)),-12,MONTH))
        Prior 12 mo Rev = Calculate(sum('TKD'[accrual_revenue]),DATESINPERIOD(Date[Date],ENDOFMONTH(DATEADD(Date[Date],-12,MONTH)),-12,MONTH))

         

  • v-gizhi-msft's avatar
    v-gizhi-msft
    Icon for Community Support rankCommunity Support

    Hi,

     

    According to your description, i create a sample to test:

    Then create a date slicer table:

    Date Slicer = DISTINCT(SELECTCOLUMNS('Table',"Date",'Table'[Date]))

    Then try this measure:

    Measure = 
    var a = SELECTEDVALUE('Date Slicer'[Date])
    return
    CALCULATE(SUM('Table'[accrual_revenue]),DATESBETWEEN('Table'[Date],DATE(YEAR(a)-2,MONTH(a),DAY(a)),DATE(YEAR(a)-1,MONTH(a),DAY(a))))

    When choosing one date in date slicer, the result shows the total revenue from the 12 months before the last 12 months based on the date selected:

    Tips: In this case, i add a defined filter to the table visual to show the result more obviously.

    Here is my test pbix file:

    pbix 

    Hope this can help.

     

    Best Regards,

    Giotto Zhi