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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
stochasticKL
Frequent Visitor

Display last N months and prior year

Hi,

I have a date table connected to a fact table. I was able to create a what-if parameter that I can adjust to display how many periods of data I want to display in a line chart and table (I followed SQLjason's guide here Display Last N Months & Selected Month using Single Date Dimension in Power BI – Some Random Thought...

 

I am able to display each months data using this measure: 

 

Sales (last n months) =
CALCULATE (
    SUM ( Sales[Sales] ),
    DATESINPERIOD ( ‘Date'[Date], MAX ( ‘Date'[Date] ), – [N Value], MONTH )
)

 

 

Usually, I am able to display last month's data in the current period by just using the parallelperiod function like this: 

 

Sales last period = 
CALCULATE (
     [Sales (last n months)] ,
     PARALLELPERIOD (
          'Date'[Date] ,
          -1 ,
          MONTH
     )
)

 

 

However, when I use this measure, assuming I selected N = 5 periods, I get this table: 

 Jan 2021Feb 2021Mar 2021Apr 2021May 2021
Sales (last n months)12345
Sales last period1234 

 

Instead of this table: 

 Jan 2021Feb 2021Mar 2021Apr 2021May 2021
Sales (last n months)12345
Sales last period 1234

 

Note that I followed, in-essence, the linked guide above.


Thank you.

2 REPLIES 2
amitchandak
Super User
Super User

@stochasticKL , Try like this for n months last year

 

Sales (last n months) last year =
CALCULATE (
SUM ( Sales[Sales] ),
DATESINPERIOD ( 'Date'[Date], MAXX ( 'Date', dateadd('Date'[Date], -1,year) ), – [N Value], MONTH )
)

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

Hi @amitchandak @ Thank you for your answer. It's doing what you said - n months last year. Which shows something like this:

 

+-----------------+------+------+------+------+------+------+------+------+------+------+
|                 | Jan  | Feb  | Mar  | Apr  | May  | Jan  | Feb  | Mar  | Apr  | May  |
|                 | 2020 | 2020 | 2020 | 2020 | 2020 | 2021 | 2021 | 2020 | 2020 | 2020 |
+-----------------+------+------+------+------+------+------+------+------+------+------+
| Sales           |      |      |      |      |      | 1    | 2    | 3    | 4    | 5    |
| (last n months) |      |      |      |      |      |      |      |      |      |      |
+-----------------+------+------+------+------+------+------+------+------+------+------+
| Sales           | x    | x    | x    | x    | x    |      |      |      |      |      |
| last month      |      |      |      |      |      |      |      |      |      |      |
+-----------------+------+------+------+------+------+------+------+------+------+------+

 Unfortunately what I would like to do is simply return the last month's value in the current month like this:

+-----------------+------+-----------+-----------+-----------+-----------+
|                 | Jan  | Feb       | Mar       | Apr       | May       |
|                 | 2021 | 2021      | 2020      | 2020      | 2020      |
+-----------------+------+-----------+-----------+-----------+-----------+
| Sales           | 1    | 2         | 3         | 4         | 5         |
| (last n months) |      |           |           |           |           |
+-----------------+------+-----------+-----------+-----------+-----------+
| Sales           |      | 1         | 2         | 3         | 4         |
| last month      |      | (Jan 2021 | (Feb 2021 | (Mar 2021 | (Apr 2021 |
|                 |      | value)    | value)    | value)    | value)    |
+-----------------+------+-----------+-----------+-----------+-----------+

 

Sorry to have confused you with the terms period and month. I'm also getting errors about post flooding so I wasn't able to reply immediately.

 

Thank you!

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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