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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
jcarville
Skilled Sharer
Skilled Sharer

Show sales for last 'X' months and Same Period Last Year

Hi,

 

I have been looking at showing sales for the last 'X' months using the brilliant guide from SQLJason. All good so far.

 

My issue is I would like to also show the sales for the same period last year based on the month I have selected, and include the logic for last 'X' months i.e. in the example below becuase I have selected Dec 2017, only the value for Dec 2016 is showing in the table. I would like my table to show the figures as shown in red going back the same 'X' months and showing BLANK() if no data is available.

 

The file can be seen here: https://drive.google.com/open?id=1GcYElXLi54hupoFYEeNFyjBOlyvjQlAZ

 

Capture.PNG

 

Any help is appreciated.

 

Regards,

Jake

 

 

1 ACCEPTED SOLUTION

This should work

 

Sales Last Year = var S_Mno = CALCULATE(MAX(Sales[MonthYearNo]), ALL('Date'))
var S_Mno_LY = IF(NOT(ISBLANK([Sales (last n months)])), INT(INT(LEFT(S_Mno, 4))-1 & RIGHT(S_Mno, LEN(S_Mno)-4)) )
var Sal_LY = CALCULATE(SUM(Sales[Sales]), FILTER(ALL(Sales[MonthYearNo], Sales[MonthYear]), 'Sales'[MonthYearNo] = S_Mno_LY), ALL('Date'))
return Sal_LY

 

Note that I am trying to find the Month number of previous year and then finding the sales for that month number. 

View solution in original post

8 REPLIES 8
v-chuncz-msft
Community Support
Community Support

@jcarville,

 

You may try applying virtual relationship in DAX and also implement a custom DAX formula for time intelligence function.

http://community.powerbi.com/t5/Desktop/Relational-Date-Filter-By-Other-Date-How-To/td-p/298373

https://www.sqlbi.com/articles/time-intelligence-in-power-bi-desktop/

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-chuncz-msft, I appreciate the reply, but I am still struggling to find a solution by piecing these all together.

Anybody else have any suggestions?

This should work

 

Sales Last Year = var S_Mno = CALCULATE(MAX(Sales[MonthYearNo]), ALL('Date'))
var S_Mno_LY = IF(NOT(ISBLANK([Sales (last n months)])), INT(INT(LEFT(S_Mno, 4))-1 & RIGHT(S_Mno, LEN(S_Mno)-4)) )
var Sal_LY = CALCULATE(SUM(Sales[Sales]), FILTER(ALL(Sales[MonthYearNo], Sales[MonthYear]), 'Sales'[MonthYearNo] = S_Mno_LY), ALL('Date'))
return Sal_LY

 

Note that I am trying to find the Month number of previous year and then finding the sales for that month number. 

@SqlJason

I have been also looking at your guide and it work great, however I am struggling to get a chart that displays the last 12 months of Sales YTD and Sales (Rolling Total) 

Wondering if you could give me any suggestions 
Thanks in advance

Anonymous
Not applicable

Hi,

 

I'm also struggling to get a chart that displays the last 12 months of Sales YTD and Sales (Rolling Total).

 

Can you help me? Is there any solution?

 

Best regards

 

LMFC

Thanks for following up @SqlJason, that's a really creative way of getting the sales based on the month number. I appreciate your help, and am a big fan of your blog!

jcarville
Skilled Sharer
Skilled Sharer

bump...

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.