Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago

Fetching a related max date for comparison

Hi! I tried searching around but I couldn't find a good answer to this problem.

 

I have two F tables, Product and Activity. What I want to do is count up all the customers who have a bought a product and was contacted during the 30 days prior to purchasing. My code looks like this:

Contacted customers with product =
VAR END_DATE =
MAX('F Product'[Date])
VAR START_DATE =
END_DATE - 30

RETURN
CALCULATE(
DISTINCTCOUNT('F Product'[Customer])
,'F Product'[Customer] IN (VALUES('F Activity'[Customer]))
,'F Activity'[Date] > START_DATE
,'F Activity'[Date] < END_DATE

However, this code gives me the absolute largest date value from Product and uses that as a comparison. What I need is the largest date value per customer when that particular customer is evaluated. Any help would be greatly appreciated.
 
Thanks!

8 Replies

  • Contacted customers with product =
    VAR END_DATE =
    MAX('F Product'[Date])
    VAR START_DATE =
    END_DATE - 30

     

    RETURN
    CALCULATE(
    DISTINCTCOUNT('F Product'[Customer])
    ,'F Product'[Customer] IN (VALUES('F Activity'[Customer]))
    ,'F Activity'[Date] > START_DATE && 'F Activity'[Date] < END_DATE
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi! I think you misunderstood me, this provides the same answer. What I need is for START_DATE and END_DATE to be the max date value from F Product for the specific customer being evaluated in the final expression.

      • Mahesh0016's avatar
        Mahesh0016
        Icon for Super User rankSuper User

        Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

  • You need to use Row context in Dax, when u have Max(date) out of calculate loop it doesnt work on Row context . take Max (date) inside the calculate and replace it with END_DATE and see if it works .

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, thank you for response. This did not work as expected, and I believe the reason is that I do not have some form of relationship between the two F tables. Thus, the formula doesn't fetch the correct max date value from the activity table.

      • Hosseinbrz's avatar
        Hosseinbrz
        New Member

        Hei.

        I have tried with DAX Studio to have better overview . probabely your customer table have duplicate cusotmer ID as a cusotmer may buy lots of products , so the first step is to GROUP BY by your customer and MAX date of purchasing product. Now you have a table of each customer with their latest purchase date. Just to visually see what happens , you can use ADDCOLUMN to your new table , this column is condition column , where you take each customer_ID and Maxdate and filter the activity table if there has been an calling on last 30 days for that cutomer and count the rows .

         

        Hope it works for you as well.

         

         

        Customer 

        IDDate
        114/03/2022
        227/08/2022
        310/03/2022
        127/06/2022
        114/10/2022
        216/10/2022

         

        Activity:

         

        IDDate
        122/02/2022
        228/08/2022
        318/02/2022
        128/06/2022
        115/10/2022
        217/10/2022