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


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.