Forum Discussion
Help With finding the earliest Date
I have a database that I need to be able to count only the first time a row of data appears. Below is part of the database:
| Date | Customer ID | Apt set | Confirmed | issued | sat | Result | Sale Amount |
| 2/27/2017 5:18:48 PM | 397500 | 1 | 1 | 0 | 0 | ROC | |
| 2/28/2017 9:03:05 PM | 397500 | 1 | 1 | 1 | 0 | 1Leg | |
| 2/27/2017 6:04:14 PM | 397501 | 1 | 1 | 0 | 0 | CTC | |
| 2/27/2017 5:48:22 PM | 397502 | 1 | 1 | 1 | 1 | 3 | |
| 3/6/2017 7:05:05 PM | 397502 | 1 | 1 | 0 | 0 | ROC | |
| 3/8/2017 2:39:46 PM | 397502 | 1 | 1 | 0 | 0 | Unissued | |
| 3/9/2017 10:08:13 AM | 397502 | 1 | 1 | 1 | 1 | 3 | |
| 2/27/2017 12:57:33 PM | 397503 | 1 | 0 | 0 | 0 | ROC | |
| 3/2/2017 4:09:14 PM | 397503 | 1 | 1 | 0 | 0 | CNC | |
| 2/27/2017 6:00:03 PM | 397504 | 1 | 1 | 1 | 0 | CTC | |
| 3/14/2017 5:14:40 PM | 397504 | 1 | 0 | 0 | 0 | Verif | |
| 2/27/2017 6:15:12 PM | 397505 | 1 | 1 | 0 | 0 | CTC | |
| 2/27/2017 6:01:54 PM | 397506 | 1 | 0 | 1 | 0 | 1Leg | |
| 2/27/2017 1:07:50 PM | 397507 | 1 | 0 | 1 | 1 | Sale | 21170.6 |
| 3/6/2017 3:57:19 PM | 397508 | 1 | 1 | 1 | 1 | Sale | 13387 |
| 3/7/2017 12:17:35 PM | 397509 | 1 | 1 | 1 | 0 | NC | |
| 3/8/2017 9:38:57 AM | 397509 | 1 | 1 | 1 | 1 | 3 | |
| 3/18/2017 12:12:22 PM | 397509 | 1 | 1 | 0 | 0 | CTC | |
| 2/27/2017 5:55:42 PM | 397510 | 1 | 0 | 0 | 0 | ROC | |
| 3/6/2017 2:24:53 PM | 397510 | 1 | 1 | 0 | 0 | COC | |
| 2/27/2017 2:21:20 PM | 397511 | 1 | 1 | 0 | 0 | ROC | |
| 3/6/2017 2:10:11 PM | 397511 | 1 | 1 | 1 | 1 | Sale | 12887 |
| 3/9/2017 4:11:31 PM | 397511 | 1 | 1 | 1 | 1 | 3 | |
| 2/27/2017 1:26:06 PM | 397512 | 1 | 1 | 1 | 0 | 1Leg | |
| 2/27/2017 1:27:45 PM | 397513 | 1 | 1 | 0 | 0 | ROC | |
| 3/1/2017 5:22:00 PM | 397513 | 1 | 1 | 1 | 0 | NH | |
| 2/27/2017 2:02:50 PM | 397514 | 1 | 1 | 0 | 0 | ROC | |
| 3/1/2017 1:42:26 PM | 397514 | 1 | 1 | 0 | 0 | COC | |
| 2/28/2017 8:01:00 AM | 397515 | 1 | 1 | 1 | 1 | 5 |
So, if I want to know how many customer's (customer ID) contacted us in a month I would not want to count everytime we talked to them, only the first time. I.E. customer ID 397500 we spoke to on 2/27 and again on 2/28. Since it is the same customer I would only want to count it one time, for the 27th. Any help, suggestions or ideas are greatly appreciated!
HW
If you don't want to use DAX - you can get the same result in the Query Editor using Group By
1) Duplicate your Table
2) then Group By - Customer ID and the new Column "First Contact" you are creating based on the MIN date for each Customer ID
3) Close & Apply
4) Create a Matrix - drag First Contact to the Rows and Customer ID to the Values
(change to Distinct -although the values are already distinct because we did the Group BY)
Follow the picture below...
OPTION 2
You can actually achieve the same result with a simple DAX Column in your current Table
First Contact Column = CALCULATE ( FIRSTNONBLANK('Table'[Date],1), ALLEXCEPT('Table', 'Table'[Customer ID]) )Then Create a Matrix HOWEVER
1) use the First Contact Column in the Rows (keep only Year and Month from the Hierarchy)
2) drag First Contact Column again but this time to the Values
AND this time you have to change the default earliest to distinct count
Hope this helps! :smileyhappy:
Let me know if you have any questions!
16 Replies
- chbraunHelper I
If you simply want the number of unique customers per calendar month, you can have a measure like this:
unique customers = DISTINCTCOUNT(CustomerID)
And then you use the month name as context in your visualizations - that way, you will see unqiue customers for January, February etc. For convenience, I would add the month name as calculated column ot your table using the DAX function MONTH.
Typically, however, instead of looking at calendar months, monthly metrics are based on a rolling window of 28 days - something like this:
unique customers moving 28 days =
CALCULATE(
[unique customers],
DATESINPERIOD(
'Date'[Date],
LASTDATE( 'Date'[Date]),
-28, DAY
)
)This way you are normalizing for different month lengths and you also get a valid and complete monthly metric every day.
Hope this helps!
Christian
- hoggwilddRegular Visitor
Christian,,
Thank you for the time and the reply
We are a sales organization and we contact customers repeatedly, month in and month out. If we use the distinctcount would it count the same customer next month (or 6 months from now) again the first time we contacted them in THAT month?
As for the month period, sometimes we look at quarters, years, etc. I have a timeline built in that allows us to set the parameters as desired. The month was not important and I should have omitted it. Sorry for the confusion.
HW
- chbraunHelper I
Yes, it would - it all depends on the context, though.
Without context, the measure using DISTINCTCOUNT will count every customer in your table only once. But the true magic of DAX is that it is context-sensitive: if you provide a context the measure will be evaluated in that context.
For example, if you set up a bar chart showing customer counts per month (months on x-axis, measure on y-axis), the context is the list of available months and the measure will be evaluated per month, i.e. customers are counted separately per month.
Make sense?
- AnonymousNot applicable
Hi hoggwildd,
Based on your description, you want to get the monthly distinct customer count, right?
If this is a case, you can refer to below steps to achieve your requirement:
1. Add calculate column "year&month" to store the value which used to group.
Year&Month = [Date].[Year]*100+[Date].[MonthNo]
2. Create a calculated table to show the grouped records.
Summary Table = SUMMARIZE(Sheet3,Sheet3[Year&Month], "Distinct Count",DISTINCTCOUNT(Sheet3[Customer ID]), "Count",COUNT(Sheet3[Customer ID]), "Detail Customer(Distinct)", CONCATENATEX(DISTINCT(SELECTCOLUMNS(FILTER(ALL(Sheet3),[Year&Month]=EARLIER(Sheet3[Year&Month])),"Custom ID",[Customer ID])),[Custom ID],","))3. Create a table visual to show the result.
Regards,
Xiaoxin Sheng
- hoggwilddRegular Visitor
Thank you both for your advice, time and help. I realize this is way over my head! I have now spent nearly 3 days on this and I am more confused then when I started. It seems so simple in my head! LOL
All this time I thought I was getting pretty good at this too. This just shows me I really need to take a course.
Thanks again,
HW
- chbraunHelper I
Sorry to hear that - don't give up, DAX is an awesome tool once you got the hang of it!
I copied your data into a pbix and created what I think you need. Unfortunately, I don't know how I can share that file with you ... Maybe the following screenshot will be enough? It shows the statement you need to create a customers table that holds dates for the first contact for each customer and all the data that is in that summarized table. The bar chart shows counts of customers per month - note how it only shows 2 for March.
The DAX for the Month column looks like this:
Month = MONTH(Customers[First contact])
Hope this helps!
Cheers,
Christian