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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Need help in writing DAX query for finding customer previous product.

Hello Commuinty, 

 

Can you please help me writing following DAX code please.  I need to find the customer previous purchased. 

 

Channa_0-1639168702759.png

 

I tried with previous day since  transaction date is not contnious for customer it is not working properly. here is my code

Previous Product Test =
CALCULATE(
DISTINCT(test[Product Type]),
PREVIOUSDAY(test[date]) ,
ALLEXCEPT(test,test[Customer ID]))
 
 
I would profoundly appreciate for any given help.
Many Thanks,
C. A
Customer IDProduct TypedateExpected Outcome - Previous ProductExpected Outcome - Product Switch
12F1-Feb-21  
12F5-Feb-21FF->F
12NF3-Mar-21FF->NF
12NF4-Mar-21NFNF->NF
12F6-Jul-21NFNF->F
12F7-Jul-21FSameDay
12F7-Jul-21FSameDay
12NF8-Jul-21FSameDay
12F8-Jul-21NFSameDay
10NF9-Jul-21  
10F10-Jul-21NFNF->F
10F11-Jul-21FF->F

 

 

 

 

 

1 ACCEPTED SOLUTION
VahidDM
Super User
Super User

Hi @Anonymous 

 

If you need a measure, try this:

Previous Product = 
VAR _D =
    MAX ( 'Table'[date] )
VAR _PD =
    CALCULATE (
        MAX ( 'Table'[date] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] < _D )
    )
VAR _A =
    CALCULATE (
        MAX ( 'Table'[Product Type] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] = _PD )
    )
RETURN
    IF ( ISBLANK ( _A ), "-", _A )

 

Output:

VahidDM_0-1639200179522.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

View solution in original post

3 REPLIES 3
VahidDM
Super User
Super User

Hi @Anonymous 

 

If you need a measure, try this:

Previous Product = 
VAR _D =
    MAX ( 'Table'[date] )
VAR _PD =
    CALCULATE (
        MAX ( 'Table'[date] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] < _D )
    )
VAR _A =
    CALCULATE (
        MAX ( 'Table'[Product Type] ),
        FILTER ( ALL ( 'Table' ), 'Table'[date] = _PD )
    )
RETURN
    IF ( ISBLANK ( _A ), "-", _A )

 

Output:

VahidDM_0-1639200179522.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

Anonymous
Not applicable

Hi @VahidDM ,

I just noticed  in your solutions that measure is returning previous product on first purchased date  from another customer ID .  For example Customer ID 10  previous product  on  9th July 2021 is NF whereas this is not the case. Can You please suggest way around it. 

Also, I have multipile years' data. It is not generating accurate results when year is changing.  I am badly stuck in writing this measure  and will really appreciate if you could help  me with it.  

 

I am ok with both measure or columns. 

 

Thank you so much for your assistance.,

C. 

 
 

Capture.PNG

 

Customer IDProduct TypedatePrevious Product ( Expected Results) Product Switch ( Expected Results ) 
12F1-Feb-21  
12F5-Feb-21FF->F
12NF3-Mar-21FF->NF
12NF4-Mar-21NFNF->NF
12F6-Jul-21NFNF->F
12F6-Jul-21FF->F
12F7-Jul-21FF->F
12NF8-Jul-21FF->NF
12F8-Jul-21NFNF->F
10NF10-Feb-19  
10NF12-Dec-20NFNF->NF
10NF9-Jul-20NFNF->NF
10F9-Jul-20NFNF->F
10F10-Aug-21FF->F
10F11-Sep-21FF->F
13F16-Sep-19  
13NF25-Feb-20FF->NF
13F3-Dec-20NFNF->F
13NF1-Jan-21FF->NF
13NF31-Aug-21NFNF->NF
13F1-Sep-21NFNF->F
13F9-Sep-21FF->F

 

ValtteriN
Super User
Super User

Hi,

Your idea was very close. This seemed to work for me:
Test Data:

ValtteriN_0-1639179910193.png

 

Dax:

Previous Product C =
CALCULATE(max('Previous Purchase'[Product]),ALLEXCEPT('Previous Purchase','Previous Purchase'[CID]),'Previous Purchase'[Dates]<EARLIER('Previous Purchase'[Dates]))
 
End result:
ValtteriN_1-1639179979801.png

 

I hope this helps and if it does consider accepting this as a solution!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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