Forum Discussion

hoggwildd's avatar
hoggwildd
Regular Visitor
9 years ago
Solved

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:

 

DateCustomer IDApt setConfirmedissuedsatResultSale Amount
2/27/2017 5:18:48 PM3975001100ROC             
2/28/2017 9:03:05 PM39750011101Leg            
2/27/2017 6:04:14 PM3975011100CTC             
2/27/2017 5:48:22 PM39750211113 
3/6/2017 7:05:05 PM3975021100ROC             
3/8/2017 2:39:46 PM3975021100Unissued       
3/9/2017 10:08:13 AM39750211113 
2/27/2017 12:57:33 PM3975031000ROC             
3/2/2017 4:09:14 PM3975031100CNC             
2/27/2017 6:00:03 PM3975041110CTC             
3/14/2017 5:14:40 PM3975041000Verif           
2/27/2017 6:15:12 PM3975051100CTC             
2/27/2017 6:01:54 PM39750610101Leg            
2/27/2017 1:07:50 PM3975071011Sale           21170.6
3/6/2017 3:57:19 PM3975081111Sale           13387
3/7/2017 12:17:35 PM3975091110NC              
3/8/2017 9:38:57 AM39750911113 
3/18/2017 12:12:22 PM3975091100CTC             
2/27/2017 5:55:42 PM3975101000ROC             
3/6/2017 2:24:53 PM3975101100COC             
2/27/2017 2:21:20 PM3975111100ROC             
3/6/2017 2:10:11 PM3975111111Sale           12887
3/9/2017 4:11:31 PM39751111113 
2/27/2017 1:26:06 PM39751211101Leg            
2/27/2017 1:27:45 PM3975131100ROC             
3/1/2017 5:22:00 PM3975131110NH              
2/27/2017 2:02:50 PM3975141100ROC             
3/1/2017 1:42:26 PM3975141100COC             
2/28/2017 8:01:00 AM39751511115 

 

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

  • Sean's avatar
    Sean
    9 years ago

    hoggwildd

    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

  • 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

     

    • hoggwildd's avatar
      hoggwildd
      Regular 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

      • chbraun's avatar
        chbraun
        Helper 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?

  • Anonymous's avatar
    Anonymous
    Not 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

    • hoggwildd's avatar
      hoggwildd
      Regular 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

      • chbraun's avatar
        chbraun
        Helper 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