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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

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
v-jayw-msft
Community Support
Community Support

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

 

 

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

8 REPLIES 8
v-jayw-msft
Community Support
Community Support

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

 

 

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.