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
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
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.

Top Solution Authors