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

Previous month not working

Hi,

If you could please help me out with this error:

 

I have a sales table (with sales of several years back) and the corresponding calendar table. All I need is the count of sales for the previous month. 

So,

 

I created a measure for the count of sales, like

Sales count = COUNTROWS( Sales( SalesID ) )

 

I created the measure for the sales count previous month, like the following:

Sales count PM = CALCULATE( [Sales count],
                                     DATEADD( Calendar[Date], -1, MONTH ) )

 

The result gives me the count of all sales of the table, ignoring the month filter, which is obviously incorrect.

 

Therefore, I experimented with the following alternative formula:

 

CALCULATE( [Sales count],
                YEAR( Calendar[Date] ) = YEAR( TODAY() ),
                MONTH( Calendar[Date] ) = MONTH( TODAY() ) -1 )

 

This time, the result turns out almost correct. But, the month of January does not show any value, because the formula restricts the year to the current year. BTW, I had to filter the year, because otherwise the result sums up the previous month of this year and all previous years too (for example, if the current month is October, the formula would count sales of September this year, and also from previous years).

 

Also, I experimented with the PREVIOUSMONTH function, which always returns a blank result.

 

Am I doing something wrong with the Dateadd function?

 

Thank you and regards.

1 ACCEPTED SOLUTION

Wow, I just solved it! (sorry for bothering you, ...good for me)

 

The reason why the previous month formula returned a zero value is because I needed to give it the today parameter in the filter. Forgot to mention that I displayed the value in a card (not a table). When I apply a filter to the card so as date = today, then the correct value comes up.

 

Must be very basic for the most of you, but I am so glad to have solved it!

 

 

 

View solution in original post

2 REPLIES 2
superjam
Helper II
Helper II

Sorry, I mentioned that the Previousmonth formula returned a blank result; actually the result is zero.

 

CALCULATE( [Sales count],
              PREVIOUSMONTH( Calendar[Date] ) )

Wow, I just solved it! (sorry for bothering you, ...good for me)

 

The reason why the previous month formula returned a zero value is because I needed to give it the today parameter in the filter. Forgot to mention that I displayed the value in a card (not a table). When I apply a filter to the card so as date = today, then the correct value comes up.

 

Must be very basic for the most of you, but I am so glad to have solved it!

 

 

 

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.