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
Uzi2019
Super User
Super User

previous month calculation

Hi Experts,

I want to calcuate previous month cost in a Matrix visual in which I need to show this previous month cost against the Date and Brand Column. Everything is working fine but if brand of one particular date is missing for previous month then it is showing error of continous date (can't display the visual).

Is there any work around of such situation where we can handle missing brand of previous month?


I used below DAX:

Previous Month Cost =
VAR PRev = CALCULATE(SUM(A[Cost]),DATEADD(A[Date],-1,MONTH),ALLEXCEPT(A,A[Brand],A[Date]))
VAR Curr = CALCULATE(sum(A[Cost]))
RETURN IFERROR((Curr-PRev)/PRev,"-")

 

Sample Data:

BrandDateCost
AAA1-Jan-20100
AAA2-Jan-20200
AAA3-Jan-20300
BBB1-Jan-20400
BBB2-Jan-20100
AAA1-Feb-20300
AAA2-Feb-20500
AAA3-Feb-20100
BBB1-Feb-20150
BBB3-Feb-20250

 

I urge you to please help me to get this done.

 

Don't forget to give thumbs up and accept this as a solution if it helped you!!!
1 ACCEPTED SOLUTION
v-jayw-msft
Community Support
Community Support

Hi @Uzi2019 ,

 

You could use EDATE() function instead.

Modify the formula as below.

Previous Month Cost = 
VAR PRev = CALCULATE(SUM('Table'[Cost]),EDATE('Table'[Date],-1),ALLEXCEPT('Table','Table'[Brand],'Table'[Date]))
VAR Curr = CALCULATE(sum('Table'[Cost]))
RETURN IFERROR((Curr-PRev)/PRev,"-")

1.PNG 

If the result is not what you want, please show the expected result to us.

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

View solution in original post

5 REPLIES 5
v-jayw-msft
Community Support
Community Support

Hi @Uzi2019 ,

 

You could use EDATE() function instead.

Modify the formula as below.

Previous Month Cost = 
VAR PRev = CALCULATE(SUM('Table'[Cost]),EDATE('Table'[Date],-1),ALLEXCEPT('Table','Table'[Brand],'Table'[Date]))
VAR Curr = CALCULATE(sum('Table'[Cost]))
RETURN IFERROR((Curr-PRev)/PRev,"-")

1.PNG 

If the result is not what you want, please show the expected result to us.

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
amitchandak
Super User
Super User

@Uzi2019 , Please use date table for that. Join date of date table with date of your table

 

Previous Month Cost =
VAR PRev = CALCULATE(SUM(A[Cost]),DATEADD(Date[Date],-1,MONTH),ALLEXCEPT(A,A[Brand],A[Date]))
VAR Curr = CALCULATE(sum(A[Cost]))
RETURN IFERROR((Curr-PRev)/PRev,"-")

 

refer my video, why time intelligence fails - https://www.youtube.com/watch?v=OBf0rjpp5Hw

 

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 :radacad sqlbi My Video Series Appreciate your Kudos.

 

@amitchandak  - there is no issue with Date. The Date has continuous data but channel is missing against some dates. One more thing, with my shared measure all work perfect until I add channel column in that matrix.

 

I can not Calendar table for my scenario. I'll to take everything from same table. 

So do you have any workaround without using Calendar table?

 

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

@Uzi2019 , check if this can work

Previous Month Cost =
var _date = date(year(A[Date]),month(A[Date])-1,date(A[Date]))
VAR PRev = CALCULATE(SUM(A[Cost]),_date,ALLEXCEPT(A,A[Brand],A[Date]))
VAR Curr = CALCULATE(sum(A[Cost]))
RETURN IFERROR((Curr-PRev)/PRev,"-")

 

of use a filter clause

filter(A, A[Date] =_date)

Not getting your solution. Can you please share the pbix file? I have shared some sample data also in my first post.

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

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.