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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
mcornfield
Helper III
Helper III

DAX Calculation for Prior Period Date Range

Period Column is broken up into 2 month increments. I need to compare values vs the previous period and am not sure the DAX Formula. Below is what I have in PBI and then what I am looking to create. After this I need to show the Variance. (period is based of a Date Column)

mcornfield_0-1638892186699.png

mcornfield_2-1638892313586.png

 

 

13 REPLIES 13
v-kkf-msft
Community Support
Community Support

Hi @mcornfield ,

 

Please try the following formula:

 

Measure = 
var pre_date = 
    FILTER(
        ALL('Table'),
        'Table'[QuestionEndDate] < max('Table'[QuestionEndDate])
    )
var lastPeriod = MAXX( pre_date, 'Table'[QuestionEndDate] )
return
    CALCULATE(
        SUM('Table'[1. # Achieved]),
        filter(
            all('Table'),
            'Table'[QuestionEndDate] = lastPeriod 
        )
    )

vkkfmsft_0-1639103057767.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

It works in the one view but when i try to expand by other dimensions it just repeats the total.

Please see image for reference. In my live file I have other dimensions I have to bring in.

 

mcornfield_1-1639359034849.png

 

Hi @mcornfield ,

 

After adding other dimensions, if you want to calculate the value for the same day 2 months ago, then try the following formula. For example when [2. Answer Date] column is "2021-8-30", then calculate the [1. # Achieved] from "2021-6-30".

The same day 2 months ago = 
var _end = EDATE ( MAX ( 'Table'[QuestionEndDate] ), -2 )
var _start = EDATE ( MAX ( 'Table'[QuestionEndDate] ), -4 )
var PreviousPeriod = EDATE ( MAX ( 'Table'[2. Answer Date] ), -2 )
var sub =
    CALCULATE (
        SUM ( 'Table'[1. # Achieved] ), 
        filter ( 
            ALL ( 'Table' ),
            'Table'[2. Answer Date].[Date] = PreviousPeriod
                && 'Table'[2. Answer Date] <> BLANK()
        )   
    )
var total = 
    CALCULATE (
        SUM ( 'Table'[1. # Achieved] ), 
        filter ( 
            ALL ( 'Table' ),
            'Table'[QuestionEndDate] > _start
                && 'Table'[QuestionEndDate] <= _end
        )    
    )
return 
    IF (
        ISFILTERED ( 'Table'[2. Answer Date] ),
        sub,
        total
    )

vkkfmsft_0-1639554577868.png

 

If you want to roll back [1. # Achieved] from the previous 2 months, then try the following formula. For example when [2. Answer Date] is "2021-7-29", then calculate [1. # Achieved] from "2021-5-1" to "2021-5-29".

 

Rollback two months = 
var _start = EDATE ( MAX ( 'Table'[QuestionEndDate] ), -4 )
var PreviousPeriod = EDATE ( MAX ( 'Table'[2. Answer Date] ), -2 )
return 
    CALCULATE (
        SUM ( 'Table'[1. # Achieved] ), 
        filter ( 
            ALL ( 'Table' ),
            'Table'[QuestionEndDate] > _start
                && 'Table'[2. Answer Date].[Date] <= PreviousPeriod
        )    
    )

vkkfmsft_1-1639555316585.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

 

 

amitchandak
Super User
Super User

@mcornfield , if you have date range preferred from a joined date table

 

same period based on date range
Last Period =
var _max =maxx(date,date[date])
var _min =maxx(date,date[date])
var datediff1 = datediff(_min,_max,day)
var _maxX = _max-datediff1
var _minX = _min -datediff1
return
CALCULATE(SUM(Sales[Sales Amount]),filter(all(date),date[date]<=_maxX &&date[date]>=_minX))

 

 

or refer

How to use two Date/Period slicers :https://www.youtube.com/watch?v=WSeZr_-MiTg

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks, So Everything is in one table. the columns I have are Date, Year, Month Period, Period Number, and # Acheived.   SO I am not sure how to implement your suggestion. 

@mcornfield , prefer to have date table, "all" does not go fine when what do display row level data.

 

You can replace the date table with your table and check

 

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.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thank you, the numbers look way high. I am wondering if it's not adhering to the slicers I have.

Any thoughts would be appreciated 🙂

mcornfield_0-1639000993577.png

 

Hi,

Share the link from where i can download your PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi,

You should ideally have a seperate Calendar Table with the Year and period columns appearing in the Calendar Table.  We should then create a relationship between the Date column in your Table with the Date column in the Calendar Table.  However the confusion at this stage would be that which Date column in your Table should be used for creating the relationship - QuestionEndDate or 2. Answer Date?  Please clarify.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Period is based off of PeriodEndDate Thanks!

Hi,

That does not answer my question.  I am sure someone else will help you with this.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors