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
mekaelj
Helper II
Helper II

Rolling sales data for the past x months

Hi,

 

I would like to create a measure that shows only the sales for the previous x months. I have a separate date table and a sales data table that I use.

 

I want the measure to show the past 12 months based on the InvoiceDate table in my sales data, so if my last reported sales was 2017-08-23 I want the measure to show only 2017-08-23 and 12 months back and then automatically update whenever new sales data is added.

 

Is this possible?

 

Regards,

 

Mikael

7 REPLIES 7
Anonymous
Not applicable

If the solution works Just accept it as a solution it might be useful for others....

Anonymous
Not applicable

Give it a try...

Measure =
VAR LSTDATE =
    MAX ( Sales[InvoiceDate] )
RETURN
    CALCULATE (
        [SumOfSales],
        DATESBETWEEN (
            'Date'[Date],
            DATE ( YEAR ( LSTDATE ) - 1MONTH ( LSTDATE )DAY ( LSTDATE ) ),
            DATE ( YEAR ( LSTDATE )MONTH ( LSTDATE )DAY ( LSTDATE ) )
        )
    )

Hi,

 

I can't get this formula to work. After the datesbetween part does not work for me. 

 

When you type RETURN, do you mean create a new measure? So when you later type LSTDATE, is that my VAR LSTDATE measure I'm meant to use? 

Anonymous
Not applicable

To my Knowledge,

 

DATESBETWEN doesn't work if you don't have a full date dimensions with all the dates...

Before RETURN we will declare variables(LSTDATE) with a keyword "VAR" and after we will write an expression to use the declared variable(LSTDATE).

"LSTDATE" variable pulls the max invoicedate and then we are calculating 1 year back based on the max invoice date.

You can refer this link for Variables in DAX  https://www.sqlbi.com/articles/variables-in-dax/

I tested that and it works fine for me...

 

Capture.PNG  

Hi,

 

I've gotten it to calculate something now, but it returns me an absurdly high number that I am not able to sort by dates for some reason. My calculation looks like this:

 

Rolling 12 = VAR LSTDATE = MAX(Sales[InvoiceDate]) RETURN CALCULATE([Sales in $];DATESBETWEEN('Date'[Date];DATE(YEAR(LSTDATE)-1;MONTH(LSTDATE);DAY(LSTDATE));DATE(YEAR(LSTDATE);MONTH(LSTDATE);DAY(LSTDATE))))

 

where Sales in $ is a measure that calculates 

 

Sales in $  = SUM(Sales[GrossSales]) - SUM(Sales[Discounts])

 

Where am I going wrong?

 

Regards,

Anonymous
Not applicable

Will you be able to provide sample data ...?

I can try to provide some but I'll have to create it first as all the data is confidential. 

 

Does anyone else have any suggestions on how I can make this happen?

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.