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
RobX
Advocate I
Advocate I

Measure for Prior Month to Date

I'm trying to create a measure that calculates the prior month to date based on the dates selected in a filter pane.  If I select 1/1/2023 through 1/9/2023, I want the prior month results to be for 12/1/2022 through 12/9/2022.

I found another thread that suggested this DAX measure:

SpendMTD = CALCULATE(
    SUM(Spend[Spend]),
    DATEADD(Calendar[Date], -1, MONTH)
)

But that seems to provide the sum for the full previous month, not just through the 9th.
1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @RobX  ,

I create a sample pbix file(see the attachment), please check if that is what you want. You can create a measure as below to get it:

Previous month spends of selected date range = 
VAR _mindate =  MIN ( 'Calendar'[Date] )
VAR _maxate = MAX ( 'Calendar'[Date] )
VAR _pminedate = EOMONTH ( _mindate, -1 )
VAR _pmaxedate = EOMONTH ( _maxate, -1 )
VAR _minday = DAY ( _mindate )
VAR _maxday = DAY ( _maxate )
VAR _pmindate =
    IF (
        _minday > DAY ( _pminedate ),
        _pminedate,
        DATE ( YEAR ( _pminedate ), MONTH ( _pminedate ), _minday )
    )
VAR _pmaxdate =
    IF (
        _maxday > DAY ( _pmaxedate ),
        _pmaxedate,
        DATE ( YEAR ( _pmaxedate ), MONTH ( _pmaxedate ), _maxday )
    )
RETURN
    CALCULATE (
        SUM ( 'Spend'[Spend] ),
        FILTER ( 'Spend', 'Spend'[Date] >= _pmindate && 'Spend'[Date] <= _pmaxdate )
    )

yingyinr_0-1673336824211.png

 

If the above one can't help you, could you please provide more raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

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

View solution in original post

3 REPLIES 3
v-yiruan-msft
Community Support
Community Support

Hi @RobX  ,

I create a sample pbix file(see the attachment), please check if that is what you want. You can create a measure as below to get it:

Previous month spends of selected date range = 
VAR _mindate =  MIN ( 'Calendar'[Date] )
VAR _maxate = MAX ( 'Calendar'[Date] )
VAR _pminedate = EOMONTH ( _mindate, -1 )
VAR _pmaxedate = EOMONTH ( _maxate, -1 )
VAR _minday = DAY ( _mindate )
VAR _maxday = DAY ( _maxate )
VAR _pmindate =
    IF (
        _minday > DAY ( _pminedate ),
        _pminedate,
        DATE ( YEAR ( _pminedate ), MONTH ( _pminedate ), _minday )
    )
VAR _pmaxdate =
    IF (
        _maxday > DAY ( _pmaxedate ),
        _pmaxedate,
        DATE ( YEAR ( _pmaxedate ), MONTH ( _pmaxedate ), _maxday )
    )
RETURN
    CALCULATE (
        SUM ( 'Spend'[Spend] ),
        FILTER ( 'Spend', 'Spend'[Date] >= _pmindate && 'Spend'[Date] <= _pmaxdate )
    )

yingyinr_0-1673336824211.png

 

If the above one can't help you, could you please provide more raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

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

Thank you.  This solved the issue I was having.

Greg_Deckler
Super User
Super User

@RobX Try:

SpendMTD = 
  VAR __CMBegin = MIN('Calendar'[Date])
  VAR __CMEnd = MAX('Calendar'[Date])
  VAR __PMBegin = EDATE(__CMBegin, -1)
  VAR __PMEnd = EDATE(__CMEnd, -1)
  VAR __Result = CALCULATE(SUM(Spend[Spend]), Calendar[Date]>=__PMBegin && Calendar[Date]<= __PMEnd)
RETURN
  __Result


or

SpendMTD = 
  VAR __CMBegin = MIN('Calendar'[Date])
  VAR __CMEnd = MAX('Calendar'[Date])
  VAR __PMBegin = EDATE(__CMBegin, -1)
  VAR __PMEnd = EDATE(__CMEnd, -1)
  VAR __Result = SUMX(FILTER(Spend, [Date]>=__PMBegin && [Date]<= __PMEnd),[Spend])
RETURN
  __Result

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