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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
July 2024 Power BI Update

Power BI Monthly Update - July 2024

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

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.