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

using dax formula to calculate the previous month total

My question sounds simple, but i have not been able to calculate the prior/previous month total using a dax formula. I have a formula that calulates the current/latest month total and here is the formula for the latest month: 

ar_0_30 = CALCULATE(sum(AR_Data[M_BALN_POST]),filter( AR_Data,AR_Data[Report Date] = MAX(AR_Data[Report Date])),FILter(AR_Data,(AR_Data[Age Group - Discharge] = " 0-30")))
 
When i try and add previous or '-1' in the report date filter, it would fail. The format of the AR_Data[Report Date] is mm/dd/yyyy
 
Help will be greatly appriciated.
 
Thank you in advance
 
 
1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi  @Anonymous ,

I created some data:

vyangliumsft_0-1635309227419.png

Here are the steps you can follow:

1. Create measure.

ar_0_30 =
CALCULATE(SUM('Table'[M_BALN_POST]),FILTER(ALLSELECTED('Table'),
MAX('Table'[Report Date])>=DATE(YEAR(TODAY()),MONTH(TODAY())-1,DAY(TODAY()))&&
MAX('Table'[Report Date])<=TODAY()
&&'Table'[Age Group - Discharge]="0-30"))

2. Click on the blank space of the page and place [Report Date] in the Filter to filter the entire page.

vyangliumsft_1-1635309227422.png

3. Result:

Total in the most recent month:

Filter type – Relative Date, Is in the last , 1, Month。

The results are displayed for the most recent month:

vyangliumsft_2-1635309227425.png

Total last month:

Filter type – Relative Date, Is in the last , 1, calendar months.

The results are shown as last month:

vyangliumsft_3-1635309227428.png

 

Best Regards,

Liu Yang

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

18 REPLIES 18
TheoC
Super User
Super User

Hi @Anonymous 

 

There looks to be a lot of comments and potential solutions already provided, so my apologies if I am just adding to the confusion, however, can you try the below:

 

PriorMonthToDate =

VAR _CurMth = MONTH ( TODAY() )
VAR _CurYr = YEAR ( TODAY() )
VAR _CurDay = DAY ( MAX ( AR_Data[Report Date] ) )
VAR _LastDay = DATE ( _CurYr , _CurMth -1, _CurDay )
VAR _FirstDay = DATE ( _CurYr, _CurMth -1, 1 )

RETURN

CALCULATE ( SUM ( AR_Data[M_BALN_POST] ) , FILTER ( 'AR_Data' , AR_Data[Report Date] >= _FirstDay && AR_Data[Report Date] <= _LastDay ) , FILTER ( 'AR_Data' , AR_Data[Age Group - Discharge] = " 0-30" ) )

 

All the best! 

 

Theo 🙂

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

v-yangliu-msft
Community Support
Community Support

Hi  @Anonymous ,

I created some data:

vyangliumsft_0-1635309227419.png

Here are the steps you can follow:

1. Create measure.

ar_0_30 =
CALCULATE(SUM('Table'[M_BALN_POST]),FILTER(ALLSELECTED('Table'),
MAX('Table'[Report Date])>=DATE(YEAR(TODAY()),MONTH(TODAY())-1,DAY(TODAY()))&&
MAX('Table'[Report Date])<=TODAY()
&&'Table'[Age Group - Discharge]="0-30"))

2. Click on the blank space of the page and place [Report Date] in the Filter to filter the entire page.

vyangliumsft_1-1635309227422.png

3. Result:

Total in the most recent month:

Filter type – Relative Date, Is in the last , 1, Month。

The results are displayed for the most recent month:

vyangliumsft_2-1635309227425.png

Total last month:

Filter type – Relative Date, Is in the last , 1, calendar months.

The results are shown as last month:

vyangliumsft_3-1635309227428.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Anonymous
Not applicable

Capture.PNG

form at of the report date. I dont think i can share the pbxi file.

Samarth_18
Community Champion
Community Champion

Hi @Anonymous 

You can try below code:-

PreviousMonth =
CALCULATE (
    SUM ( AR_Data[M_BALN_POST] ),
    FILTER (
        AR_Data,
        AR_Data[Report Date]
            = MAX (
                DATE ( YEAR ( AR_Data[Report Date] ), MONTH ( AR_Data[Report Date] ) - 1, DAY ( AR_Data[Report Date] ) )
            )
    ),
    FILTER ( AR_Data, ( AR_Data[Age Group - Discharge] = " 0-30" ) ) //change this filter condition according to your requirement
)

 

Thanks,

Samarth

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Anonymous
Not applicable

thank you for that reply, it did not seem to work.

this is the error i am getting now

The MAX function only accepts a column reference as an argument.

Can you try below code:-

 

PreviousMonth =
CALCULATE (
    SUM ( AR_Data[M_BALN_POST] ),
    FILTER (
        AR_Data,
        AR_Data[Report Date]
            = DATE ( MAX ( YEAR ( AR_Data[Report Date] ) ), MONTH ( MAX ( AR_Data[Report Date] ) ) - 1, DAY ( MAX ( AR_Data[Report Date] ) ) )
    ),
    FILTER ( AR_Data, ( AR_Data[Age Group - Discharge] = " 0-30" ) )
)

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Anonymous
Not applicable

same error

The MAX function only accepts a column reference as an argument.

Sorry my bad. This would work for sure 🙂

PreviousMonth =
CALCULATE (
    SUM ( AR_Data[M_BALN_POST] ),
    FILTER (
        AR_Data,
        AR_Data[Report Date]
            = DATE ( YEAR ( MAX ( AR_Data[Report Date] ) ), MONTH ( MAX ( AR_Data[Report Date] ) ) - 1, DAY ( MAX ( AR_Data[Report Date] ) ) )
    ),
    FILTER ( AR_Data, ( AR_Data[Age Group - Discharge] = " 0-30" ) )
)

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Anonymous
Not applicable

There is no error, but the the ans shows blank. The formula has no errors.

Have you updated below condition in the formula as per your need?

FILTER ( AR_Data, ( AR_Data[Age Group - Discharge] = " 0-30" )

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Anonymous
Not applicable

yes, i left the formula just the way it is, since i need that filter

Hope you have verified, you have data for previous month specific date?

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Anonymous
Not applicable

Capture.PNG

yes, when i checked i do have data for the previous/prior month

 

Is it possible for you to share your PBIX file after removing confidential data?

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Anonymous
Not applicable

how do i share the pbix file

You can upload it in a drive and share the link.

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Anonymous
Not applicable

I also have a field that is in the format of YYYYMM, Is there anyway that would work better

 

Anonymous
Not applicable

I dont think i can share that, it i going to take a lot of work to get rid of the confidential info.😟

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.