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
levcovitz
Frequent Visitor

DAX - Sales in the last 3 months by salesperson (calculated column)

Hello!

 

I have a table that looks like this:

 

Month (DD/MM/YYY)SalespersonIDSales
01/12/2022xxxxx0
01/11/2022xxxxx0
01/10/2022xxxxx0
01/09/2022xxxxx0
01/08/2022xxxxx1
01/07/2022xxxxx2
01/06/2022xxxxx1
01/05/2022xxxxx1
01/12/2022yyyyy1
01/11/2022yyyyy2
01/10/2022yyyyy0
01/09/2022yyyyy0
01/08/2022yyyyy0
01/07/2022yyyyy1
01/06/2022yyyyy1
01/05/2022yyyyy0

 

I want to create a calculated column that shows how many sales a salesperson made in the previous 3 months (not including the current month). Here's the expected result:

 

Month (DD/MM/YYY)SalespersonIDSalesSales Previous 3 Months (Expected Result)
01/12/2022xxxxx00
01/11/2022xxxxx01
01/10/2022xxxxx03
01/09/2022xxxxx04
01/08/2022xxxxx14
01/07/2022xxxxx2blank
01/06/2022xxxxx1blank
01/05/2022xxxxx1blank
01/12/2022yyyyy12
01/11/2022yyyyy20
01/10/2022yyyyy01
01/09/2022yyyyy02
01/08/2022yyyyy02
01/07/2022yyyyy1blank
01/06/2022yyyyy1blank
01/05/2022yyyyy0blank

 

When there are less than 3 months previous to the date (respecting the salesperson ID context), I don't want to show results.

 

Any ideas on how to do that in a DAX calculated column?

1 ACCEPTED SOLUTION
ppm1
Solution Sage
Solution Sage

You should probably do this as a measure, but here is a column expression that seems to work. Replace T2 with your actual table names (and update column names, if needed).

P3Mos =
VAR thisdate = T2[Month]
VAR mindate =
    CALCULATE ( MIN ( T2[Month] ), ALLEXCEPT ( T2, T2[SalespersonID] ) )
VAR prevmonths =
    DATEDIFF ( mindate, thisdate, MONTH )
VAR result =
    CALCULATE (
        SUM ( T2[Sales] ),
        ALLEXCEPT ( T2, T2[SalespersonID] ),
        T2[Month] >= EDATE ( thisdate, -3 )
            && T2[Month] <= EDATE ( thisdate, -1 )
    )
RETURN
    IF ( prevmonths >= 3, result )

 

Pat

Microsoft Employee

View solution in original post

2 REPLIES 2
ppm1
Solution Sage
Solution Sage

You should probably do this as a measure, but here is a column expression that seems to work. Replace T2 with your actual table names (and update column names, if needed).

P3Mos =
VAR thisdate = T2[Month]
VAR mindate =
    CALCULATE ( MIN ( T2[Month] ), ALLEXCEPT ( T2, T2[SalespersonID] ) )
VAR prevmonths =
    DATEDIFF ( mindate, thisdate, MONTH )
VAR result =
    CALCULATE (
        SUM ( T2[Sales] ),
        ALLEXCEPT ( T2, T2[SalespersonID] ),
        T2[Month] >= EDATE ( thisdate, -3 )
            && T2[Month] <= EDATE ( thisdate, -1 )
    )
RETURN
    IF ( prevmonths >= 3, result )

 

Pat

Microsoft Employee

Works perfectly. Thank you!

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.