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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
VEOMBOTEST
Regular Visitor

Last month sales and sales of year's last month

Hi all,

 

I am a beginner with Power BI and I need help on this.

I need to calcule sales for last month and sales for the year of last month.

For example:

1. If today is 29/01/2019 => I should calculate:

         -the sales for 12/2018 (all sales month)

           AND

          - the sales for all the year 2018 (from Januar to December)

 

2. If today is 15/03/2019 => I should calculate:

         -the sales for 02/2019 (all sales month)

           AND

          - the sales for the year 2019 (from Januar to December)

 

Your help is much appreciated.

Thanks

1 ACCEPTED SOLUTION
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @VEOMBOTEST,

 

For your requiement, I have made a simple test pbix to reproduce your scenario.

 

You could refer to the measures below. 

 

last month_ =
VAR currentmonth =
    MONTH ( MAX ( 'Data'[Date] ) )
VAR lastmonth =
    IF ( currentmonth = 1, 12, currentmonth - 1 )
VAR currentyear =
    YEAR ( MAX ( 'Data'[Date] ) )
VAR the_year_of_lastmonth =
    IF ( currentmonth = 1, currentyear - 1, currentyear )
RETURN
    CALCULATE (
        SUM ( Data[Amount] ),
        FILTER (
            'Data',
            MONTH ( 'Data'[Date] ) = lastmonth
                && YEAR ( 'Data'[Date] ) = the_year_of_lastmonth
        )
    )

the year of last month =
VAR currentmont =
    MONTH ( MAX ( 'Data'[Date] ) )
VAR lastmonth =
    IF ( currentmont = 1, 12, currentmont - 1 )
VAR currentyear =
    YEAR ( MAX ( 'Data'[Date] ) )
VAR the_year_of_lastmonth =
    IF ( currentmont = 1, currentyear - 1, currentyear )
RETURN
    CALCULATE (
        SUM ( Data[Amount] ),
        FILTER ( 'Data', YEAR ( 'Data'[Date] ) = the_year_of_lastmonth )
    )

If you want to calculated the sales of last month for each month, you may need to create another date table and then create the relationship between the tables, then create the measure like to calculate the last month value.

 

last month = CALCULATE(SUM(Data[Amount]),DATEADD('Data'[Date],-1,MONTH))

More details, you could refer to my attachment.

 

Best  Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @VEOMBOTEST,

 

For your requiement, I have made a simple test pbix to reproduce your scenario.

 

You could refer to the measures below. 

 

last month_ =
VAR currentmonth =
    MONTH ( MAX ( 'Data'[Date] ) )
VAR lastmonth =
    IF ( currentmonth = 1, 12, currentmonth - 1 )
VAR currentyear =
    YEAR ( MAX ( 'Data'[Date] ) )
VAR the_year_of_lastmonth =
    IF ( currentmonth = 1, currentyear - 1, currentyear )
RETURN
    CALCULATE (
        SUM ( Data[Amount] ),
        FILTER (
            'Data',
            MONTH ( 'Data'[Date] ) = lastmonth
                && YEAR ( 'Data'[Date] ) = the_year_of_lastmonth
        )
    )

the year of last month =
VAR currentmont =
    MONTH ( MAX ( 'Data'[Date] ) )
VAR lastmonth =
    IF ( currentmont = 1, 12, currentmont - 1 )
VAR currentyear =
    YEAR ( MAX ( 'Data'[Date] ) )
VAR the_year_of_lastmonth =
    IF ( currentmont = 1, currentyear - 1, currentyear )
RETURN
    CALCULATE (
        SUM ( Data[Amount] ),
        FILTER ( 'Data', YEAR ( 'Data'[Date] ) = the_year_of_lastmonth )
    )

If you want to calculated the sales of last month for each month, you may need to create another date table and then create the relationship between the tables, then create the measure like to calculate the last month value.

 

last month = CALCULATE(SUM(Data[Amount]),DATEADD('Data'[Date],-1,MONTH))

More details, you could refer to my attachment.

 

Best  Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Greg_Deckler
Super User
Super User

See if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
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!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Kudoed Authors