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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mikro87
Helper II
Helper II

Calculate sales for the previous month

Hi, 

 

I have one field with only the month (1,2,3,...) and I want to get the sum of the previous month.

So I got:

Sales = Sum(SQL[Sales])

Sales1 = Calculate([Sales]; SQL[month]=SQL[month]-1)

 

This isn't working. Can anyone h

1 ACCEPTED SOLUTION

@mikro87 ,

 

You mentioned only month so I wrote in that way. Try next one:

Sales1 =
VAR __CurrMonth = MAX ( SQL[month] )
VAR __CurrYear = MAX ( SQL[year] )
VAR __PrevMonth = IF ( __CurrMonth > 1, __CurrMonth - 1, 12 )
VAR __YearOfPrevMonth = IF ( __CurrMonth > 1, __CurrYear, __CurrYear - 1 )

RETURN
    CALCULATE ( [Sales], SQL[month] = __PrevMonth, SQL[year] = __YearOfPrevMonth )

Regards,
Ruslan Zolotukhin (zoloturu)
BI Engineer at Akvelon Inc. / Kharkiv Power BI User Group Leader / DAX & Power BI Trainer
-------------------------------------------------------------------
Did I answer your question? Mark my post as a solution!
It was useful? Press Thumbs Up!

You are from Ukraine? If yes then welcome to Power BI User Group - KhPUG website. Other country? Check and find proper one - Power BI User Groups

View solution in original post

7 REPLIES 7
amitchandak
Super User
Super User

Try

Sales1 =

var _max maxx(SQL,SQL[month])

return

Calculate([Sales]; all(SQL[month]),SQL[month]=_max-1)

 

But what will you do for year changes ?

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks. My Recent Blog -
Winner-Topper-on-Map-How-to-Color-States-on-a-Map-with-Winners , HR-Analytics-Active-Employee-Hire-and-Termination-trend
Power-BI-Working-with-Non-Standard-Time-Periods And Comparing-Data-Across-Date-Ranges

Connect on Linkedin

zoloturu
Memorable Member
Memorable Member

Hi @mikro87 ,

 

Try next code:

 

Sales1 =
VAR __CurrMonth = MAX ( SQL[month] )
VAR __PrevMonth = __CurrMonth - 1

RETURN
    CALCULATE ( [Sales]; SQL[month] = __PrevMonth )

 

Regards,
Ruslan Zolotukhin (zoloturu)
BI Engineer at Akvelon Inc. / Kharkiv Power BI User Group Leader / DAX & Power BI Trainer
-------------------------------------------------------------------
Did I answer your question? Mark my post as a solution!
It was useful? Press Thumbs Up!

You are from Ukraine? If yes then welcome to Power BI User Group - KhPUG website. Other country? Check and find proper one - Power BI User Groups 

It works so far.
But now I have year(2018, 2019, ...) too.
How can I compare the previous month also from Dec. to Jan.?

@mikro87 ,

 

You mentioned only month so I wrote in that way. Try next one:

Sales1 =
VAR __CurrMonth = MAX ( SQL[month] )
VAR __CurrYear = MAX ( SQL[year] )
VAR __PrevMonth = IF ( __CurrMonth > 1, __CurrMonth - 1, 12 )
VAR __YearOfPrevMonth = IF ( __CurrMonth > 1, __CurrYear, __CurrYear - 1 )

RETURN
    CALCULATE ( [Sales], SQL[month] = __PrevMonth, SQL[year] = __YearOfPrevMonth )

Regards,
Ruslan Zolotukhin (zoloturu)
BI Engineer at Akvelon Inc. / Kharkiv Power BI User Group Leader / DAX & Power BI Trainer
-------------------------------------------------------------------
Did I answer your question? Mark my post as a solution!
It was useful? Press Thumbs Up!

You are from Ukraine? If yes then welcome to Power BI User Group - KhPUG website. Other country? Check and find proper one - Power BI User Groups

I get a syntax error, row 4 & 5

@mikro87 ,

 

Do you use comma or semicolon at your formulas? Maybe you need to replace to proper? 

Provide a screenshot of an error and a code if that not helps.


Regards,
Ruslan Zolotukhin (zoloturu)
BI Engineer at Akvelon Inc. / Kharkiv Power BI User Group Leader / DAX & Power BI Trainer
-------------------------------------------------------------------
Did I answer your question? Mark my post as a solution!
It was useful? Press Thumbs Up!

You are from Ukraine? If yes then welcome to Power BI User Group - KhPUG website. Other country? Check and find proper one - Power BI User Groups

I needed the semicolon, thanks

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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