Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Rosh89
Helper I
Helper I

DAX for calculating previous month values

Hi,

 

I have a table as shown below:

MonthPeriodCustomerBonusPrevious bonus
Jan-221A100 
Feb-222A200100
Mar-223A150200
Jan-221B300 
Feb-222B50300
Mar-223B20050

 

Rosh89_0-1668446202648.png

 

I want to calculate the previous bonus using DAX. I can only create a calendar table with month and not date for this table. Can someone suggest a suitable DAX to get the previous bonus? 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Rosh89 ,

 

You could add a date column like below:

date = DATEVALUE("1-"&'Table'[Month])

vjaywmsft_1-1669963734511.png

Then create a measure:

Measure = CALCULATE(SUM('Table'[Bonus]),FILTER(ALLEXCEPT('Table','Table'[Customer]),'Table'[date]=EDATE(SELECTEDVALUE('Table'[date]),-1)))

vjaywmsft_2-1669964066125.png

 

 

View solution in original post

8 REPLIES 8
Anonymous
Not applicable

Hi @Rosh89 ,

 

You could add a date column like below:

date = DATEVALUE("1-"&'Table'[Month])

vjaywmsft_1-1669963734511.png

Then create a measure:

Measure = CALCULATE(SUM('Table'[Bonus]),FILTER(ALLEXCEPT('Table','Table'[Customer]),'Table'[date]=EDATE(SELECTEDVALUE('Table'[date]),-1)))

vjaywmsft_2-1669964066125.png

 

 

bsheffer
Continued Contributor
Continued Contributor

why not create a date in your calendar table using date(yyyy, mmm, 01) as First_of_month for each month and then you can use date functions to get the prior month?

 

is the month a text string?

@bsheffer  In this table month is a text. I tried what you mentioned about using date as first of every month but it does not work that way because you need a calendar table marked as a date table with continous dates without any gap. Without a calendar table containing continous dates the date functions dont work. 

bsheffer
Continued Contributor
Continued Contributor

if you want to use the calendar table (which doesn't have to be marked as a date table), you can use many available code snippets to create it. 

Then you have to create a column in your fact table that converts your month, year field [Month] into a date (which is why I suggested date(yyyy, mm, 01) where yyyy is like right('fact'[month], 4) and mm is probably a switch statement). 

That date can be linked to the calender table and now you can use all the date functions like adddate() on that table to get your monthly totals for whatever month range you want.

Shaurya
Memorable Member
Memorable Member

Hi @Rosh89,

 

You can use:

 

Previous Bonus = SUMX('Table',IF('Table'[Month].[MonthNo]=EARLIER('Table'[Month].[MonthNo])-1 && 'Table'[Customer]=EARLIER('Table'[Customer]),'Table'[Bonus],0))

 

Works for you? Mark this post as a solution if it does!
Check out this blog of mine: How to Export Telemetry Data from Azure IoT Central into Power BI

Hi @Shaurya 

The Dax works for this table but it doesnt run on my data model which is around 500K rows. There is no error and it just keeps working. I think it is not able to calculate for so many rows. Perhaps a measure is best for this scenario.

Hi @Rosh89,

 

It keeps working because the EARLIER function compares the expression for every row in the table with all other rows. Give it some time, it should give you the right result.

Greg_Deckler
Super User
Super User

@Rosh89 You need something to define "before", either a Date or an Index and then it's a simple matter. See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.