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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register 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)))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

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

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
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)))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.