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
Anonymous
Not applicable

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)

 

1.PNG

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

 

 

2.PNG

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

 

1 ACCEPTED SOLUTION
v-gizhi-msft
Community Support
Community Support

Hi,

 

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

10.PNG

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.

11.PNG

Here is my test pbix file:

pbix 

Hope this can help.

 

Best Regards,

Giotto Zhi

 

View solution in original post

6 REPLIES 6
v-gizhi-msft
Community Support
Community Support

Hi,

 

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

10.PNG

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.

11.PNG

Here is my test pbix file:

pbix 

Hope this can help.

 

Best Regards,

Giotto Zhi

 

amitchandak
Super User
Super User

@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
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))

 

3.PNG

@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))

 

Anonymous
Not applicable

@amitchandakUnfortunately, they didn't work.  I think it's the dateadd piece that breaks it. 😞

@Anonymous 

I have one doubt, your calendar min date is less than the min date of the TKD table. Just check.

If not so, Create a sample where you can reproduce and share.

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.