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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
PBIBeginner2022
Helper III
Helper III

DAX : Sum previous month automatically

Hello,

 

I would like to translate this into DAX language:

 

I have a column calculated with the value of each item sold and a column with the date of sale of each item.
I would like to create a measure that gives me the total value sold for the month before last. So the current month minus -1.

 

For example, we are in June, I would like to know the total value sold in May. When we are in July, I would like my measure to automatically calculate the total value sold in June.

 

Thanks in advance

 

1 ACCEPTED SOLUTION

Hi @PBIBeginner2022 ,

 

This formula should work.

Pre_sales =
CALCULATE (
    SUM ( 'Table'[Price] ),
    FILTER (
        ALLSELECTED ( 'Table' ),
        FORMAT ( 'Table'[Date], "YYYYMM" ) = FORMAT ( EDATE ( TODAY (), -1 ), "YYYYMM" )
    )
)

 

Best Regards,

Jay

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

View solution in original post

11 REPLIES 11
v-jayw-msft
Community Support
Community Support

Hi @PBIBeginner2022 ,

 

Please try these formulas.

Column:

Pre_sales =
SUMX (
    FILTER (
        ALLSELECTED ( 'Table' ),
        'Table'[date] = EDATE ( EARLIER ( 'Table'[date] ), -1 )
    ),
    'Table'[value]
)

Measure:

Pre_sales =
SUMX (
    FILTER (
        ALLSELECTED ( 'Table' ),
        'Table'[date] = EDATE ( SELECTEDVALUE ( 'Table'[date] ), -1 )
    ),
    'Table'[value]
)

If it doesn't work, please share some sample data and expected result so that we could test the formula.

 

Best Regards,

Jay 

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

Hi,

 

I tried your measurement, but I still have the same problem, no value is displayed. Here is a data set:

 

I want the sum of the actual month -1. So we are in June, I want the sum of price in May.

 

ArticlePriceDate
ART00014311/06/2022
ART000223519/05/2022
ART00034312/06/2022
ART000498723/05/2022
ART0005413/06/2022
ART00065402/06/2022
ART0007711/08/2022
ART00086534208/06/2022
ART00098713/05/2022
ART001034209/05/2022
ART0011007/06/2022
ART0012728/05/2022
ART00133430/04/2022
ART00146518/05/2022
ART00153213/07/2022
ART0016914/05/2022
ART0017210/08/2022
ART00184305/06/2022
ART001923519/04/2022
ART00204325/08/2022
ART002198711/05/2022
ART0022412/07/2022
ART00235410/05/2022
ART0024724/08/2022
ART00256534230/06/2022
ART00268706/05/2022
ART002734224/04/2022
ART0028021/06/2022
ART0029704/05/2022
ART00303405/05/2022
ART00316517/05/2022
ART00329605/07/2022
ART003312726/05/2022
ART003415825/08/2022
ART003518931/07/2022
ART003622026/08/2022
ART003725103/07/2022
ART003828219/07/2022
ART003931318/05/2022
ART004034414/06/2022

Hi @PBIBeginner2022 ,

 

Thank you for the data.
Please try this formula:

Pre_sales =
CALCULATE (
    SUM ( 'Table'[Price] ),
    FILTER (
        ALLSELECTED ( 'Table' ),
        'Table'[Date].[MonthNo]
            = SELECTEDVALUE ( 'Table'[Date].[MonthNo] ) - 1
    )
)

vjaywmsft_0-1655114659809.png

 

Best Regards,

Jay

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

Thanks for your answer @v-jayw-msft  but I can't write in my measure 'TABLE'[date].[MonthNo]. I can write 'TABLE'[date] but I can't add .[MonthNo] in my measure. 

 

Are they other possibilities to write this measure please ?

 

Best regards,

Hi @PBIBeginner2022 ,

 

What's the format of the date column?

Make sure it's date format otherwise you will need to create another Month column.

Pre_sales = 
CALCULATE (
    SUM ( 'Table'[Price] ),
    FILTER (
        ALLSELECTED ( 'Table' ),
        MONTH ( 'Table'[Date] )
            = MONTH ( MAX ( 'Table'[Date] ) ) - 1
    )
)

vjaywmsft_0-1655171530776.png

 

Best Regards,

Jay

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

@v-jayw-msft my problem isn't resolved. I want use this visual to see only the sum of my month minus one sale.

 

PBIBeginner2022_0-1655190167065.png

 

However when I take the column of all my sales of the year into this visual and I filter with column date then relative date then "is in the last month", I have the value that I search. But I need it with a measure to automate this.

Hi @PBIBeginner2022 ,

 

So you want a card visual to dynamically display the previous month's value according to current month?

Measure = CALCULATE(SUM('Table'[Price]),FILTER(ALLSELECTED('Table'),MONTH('Table'[Date]) = MONTH(TODAY())-1))

vjaywmsft_0-1655193751169.png

 

Best Regards,

Jay

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

@v-jayw-msft I don't have the same value between my display card and the month of may. I think it is missing something in the measure. I think in the measure we filter on all the month of may of every years. So I think we need to add something filter on the month -1 of the current year and not of all the year. I don't know if your understand me. In reality I have more than 50 years in my column and I want only the current month minus one of the current year.

 

Please, could you help me for this.

Hi @PBIBeginner2022 ,

 

This formula should work.

Pre_sales =
CALCULATE (
    SUM ( 'Table'[Price] ),
    FILTER (
        ALLSELECTED ( 'Table' ),
        FORMAT ( 'Table'[Date], "YYYYMM" ) = FORMAT ( EDATE ( TODAY (), -1 ), "YYYYMM" )
    )
)

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
Hariharan_R
Solution Sage
Solution Sage

Hi,

Try below measure.

Previous Month Sales = CALCULATE(SUM(Sales[Sales]),PREVIOUSMONTH('Date'[Date]))
 
Hariharan_R_0-1654616392281.png

 

Hi @Hariharan_R 

 

When I try this, it's wrote "vide" in French (void I Think).

CALCULATE(SUM(......) is good but after to select only the month minus  1 it's complicate because I have date with this format "01/04/2022" for exemple between  year 1988 and 2024 ....

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.