Forum Discussion

srl01's avatar
srl01
Helper II
8 years ago

Flagging a customer's second visit

Hello - 

 

I have a table of raw sales transaction data (one line per item, multiple lines for a transaction, with a unique transaction code for each transaction).

 

DateTransaction codeCustomercodeStorecodeItem codeQuantity
1-Jan10010001ABC2
1-Jan10010001XYZ3
3-Jan10110012ABC2
4-Jan10210002XYZ4
5-Jan10310021ABC1
5-Jan10310021XYZ

2

 

I would like to generate a calculated column that flags whether a particular transaction is the second time that particular customer has visited. In the above, TransactionCode 102 would be flagged as True since it is the second visit from customer 1000.

 

I think I need a DistinctCount of TransactionCodes filtered for both a customercode match, and a date filter limiting to past transactions, and if that DistinctCount adds to 1 then the flag is True, otherwise False.

 

For the date filter, calendar table is setup and linked so expect I need something along the lines of;

 

FILTER (
ALL ( 'Calendar' ),
'Calendar'[Date] < MIN ( 'Calendar'[Date])
)

 

But cannot get the overall code to work with both filters.

Would appreciate assistance! Thank you.

 

6 Replies

  • Hi,

     

    Try this calculated column formula

     

    =CALCULATE(DISTINCTCOUNT(Data[Date]),FILTER(Data,Data[Customercode]=EARLIER(Data[Customercode])&&Data[Date]<EARLIER(Data[Date])))

     

    Hope this helps.

     

    • srl01's avatar
      srl01
      Helper II

      Hello Ashish - thank you!

      I've plugged that formula in but it is taking a very long time to calculate as a column formula (data is about 5 million rows, so that is a lot of filtering and counting going on). 20 minutes on an i7-7700k and it doesn't seem to be anywhere close to finished.

      Any thoughts on how to make this work with a measure instead that could be less computationally intensive?

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Super User

        Hi,

         

        In your original post, you mentioned that you wanted a calculated column formula.  Now you want a measure.  Share the link from where i can download your trimmed data (the file shize should only be a couple of MB's please) and share the expected result.

  • Update - this is what I have so far but it isn't working...

     

    Is2ndVisit = if(calculate (distinctcount([TransactionCode]),
    filter(all(CustomerMaster), CustomerMaster[CustomerCode]=min(CustomerMaster[CustomerCode])),
    FILTER (ALL ( 'Calendar' ), 'Calendar'[Date] < MIN ( 'Calendar'[Date])))=1, 1, 0)

    Anyone able to assist?