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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
srl01
Helper II
Helper II

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 6
Ashish_Mathur
Super User
Super User

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.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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?

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.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish

 

Apologies - let me rephrase then as I do indeed want a calculated column formula, but not if it takes hours to calculate. (The model has now been running for 3 hours and is still not done). 

 

Is there an alternative to your suggestion above (either a calculated column, or a measure) that will be computationally efficient here? 

 

For example... first creating a calculated column in the CustomerMaster table (meaning one calc for each of 1mil customers, rather than one for each of 5-10mil transaction lines) that looks for the earliest transaction date of that customer after their very first transaction. Then the calculated column in the SalesRawData is just a simple lookup to the CustomerMaster table to see if the date matches.

 

If that concept makes sense (or any other ideas) I'll gather an extract of the appropriate data and post. Thank you!

Hi,

 

I think we should write a measure for this.  Share the link from where i can download your file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
srl01
Helper II
Helper II

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?

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.