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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Anonymous
Not applicable

Return measure based on previous time period

Hello community.

I have created a formula that should return the measure based on previous date. The formula is as follows.

VAR PeriodEnd = FIRSTDATE(Dates[Date])-1
VAR PeriodStart =
    PeriodEnd -
    DATEDIFF (
        FIRSTDATE(Dates[Date]),
        LASTDATE(Dates[Date]),
        DAY
    )
VAR Result = 
    CALCULATE(
        [Dækningsgrad nuværende],
        DATESBETWEEN(
            Dates[Date],
            PeriodStart,PeriodEnd
        )
)        
RETURN
    Result

 In my mind that should return my measure [Dækningsgrad nuværende] based on my Dates table. -1 period. And if i change (-1) with (-2) that should return my measure minus two periods. However it is not working.

 

If i set my dates as the following. Last period show 99,02%, but if i change to last period which in this instance would be 01-01-2020 - 31-12-2020

tobkres_0-1656312219604.png

 

tobkres_2-1656312546077.png

 

tobkres_3-1656312693269.png

 

tobkres_4-1656312729786.png

In the above example my measure "Dækningsgrad lastperiod" in 2020 should have returned as "Dækningsgrad 2nd last period" in 2021. Whereas second last period should be 2019. 

Dækningsgrad 2nd last period = 
VAR PeriodEnd = FIRSTDATE(Dates[Date])-2
VAR PeriodStart =
    PeriodEnd -
    DATEDIFF (
        FIRSTDATE(Dates[Date]),
        LASTDATE(Dates[Date]),
        DAY
    )
VAR Result = 
    CALCULATE(
        [Dækningsgrad nuværende],
        DATESBETWEEN(
            Dates[Date],
            PeriodStart,PeriodEnd
        )
)        
RETURN
    Result

Can you see what could be wrong?
Thanks in advance.

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous , refer if these can help

 

same period based on date range
Last Period =
var _max =maxx(allseleceted(date),date[date])
var _min =maxx(allseleceted(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)))


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

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

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @Anonymous,

Did the above suggestions help with your scenario? if that is the case, you can consider Kudo or accept the helpful suggestions to help others who faced similar requirements.

If these also don't help, please share more detailed information to help us clarify your scenario to test.

How to Get Your Question Answered Quickly 

Regards,

Xiaoxin Sheng

amitchandak
Super User
Super User

@Anonymous , refer if these can help

 

same period based on date range
Last Period =
var _max =maxx(allseleceted(date),date[date])
var _min =maxx(allseleceted(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)))


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

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

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

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.