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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
D_PBI
Post Patron
Post Patron

Determine how many customers purchased different items, and the period of time between doing so?

Hi,
I have the below dataset. I would like to understand, via DAX, how to ascertain the following answers. Before I list the questions, I should say the report will contain a filter on 'Item'. Choosing the Item of, say, 'Bike' should return the customers whose first purchased item was a 'Bike'.
D_PBI_2-1675704907083.png


Questions are:

1) Of the customers who purchased a Bike as their first item, what percentage went of to purchase a Bench, Book, Car, Desk, Rod, and Watch.
2) What was the time period (in days) for those who purchased a Bike as their first item to purchase their second item, and the next different item and so on?

Answers are:
Based on the dataset shown above, the answers to my questions would be:
1)
a) Total customers who purchased a Bike as their first item: 2 (that being Dave and Paul).
b) Total customers who purchased a Bike as their first item but also purchased a Bench: 1 (just Paul).
c) Total customers who purchased a Bike as their first item but also purchased a Book:   0
d) Total customers who purchased a Bike as their first item but also purchased a Car:     1 (just Paul).
e) Total customers who purchased a Bike as their first item but also purchased a Desk:   0
f) Total customers who purchased a Bike as their first item but also purchased a Rod:     1 (just Dave).
g) Total customers who purchased a Bike as their first item but also purchased a Watch: 1 (just Paul).

2) 
a) Days between purchasing the first Bike and the first Bench:     Paul 731 days.  Dave n/a.
b) Days between purchasing the first Bike and the first Book:      Paul n/a.  Dave n/a.
c) Days between purchasing the first Bike and the first Car:         Paul 1157 days.  Dave n/a.
d) Days between purchasing the first Bike and the first Desk:      Paul n/a.  Dave n/a.
e) Days between purchasing the first Bike and the first Rod:        Paul n/a.  Dave 336 days.
f) Days between purchasing the first Bike and the first Watch:     Paul 734 days.  Dave n/a.

I appreciate this will need a number of DAX measures. The aim is to use Context Evaluation so when I bring the measure to work out, say, the days between the first item purchase and the different item purchased, then I would drop that measure on a row for Dave and the calculation is worked out.
Obviously, nothing is to be hard-code as the CustomerID, PurchaseDate, and Item will change overtime.

I hope I've explained all well. Please help as I'm stuck on this.
Thanks in advance.

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

@D_PBI Try something along the lines of:

Bikes First Measure =
  VAR __Customers = DISTINCT('Table',[CustomerID])
  VAR __Table = 
    GENERATE(
      __Customers,
      VAR __CustomerID = [CustomerID]
      VAR __FirstPurchaseDate = MINX(FILTER('Table',[CustomerID] = __CustomerID),[PurchaseDate])
      VAR __FirstPurchase = MINX(FILTER('Table',[CustomerID] = __CustomerID && [PurchaseDate] = __FirstPurchaseDate),[Item])
      RETURN ROW(
        "FirstPurchaseDate", __FirstPurchaseDate,
        "FirstPurchase", __FirstPurchase
      )
    )
  VAR __Result = COUNTROWS(FILTER(__Table,[FirstPurchase] = "Bike")) // or use CONCATENATEX to return customer id's
RETURN
  __Result
        

You can use similar measures for the rest, here is an example of finding customers who also bough Bench:

Bikes First Measure =
  VAR __Customers = DISTINCT('Table',[CustomerID])
  VAR __Table = 
    GENERATE(
      __Customers,
      VAR __CustomerID = [CustomerID]
      VAR __FirstPurchaseDate = MINX(FILTER('Table',[CustomerID] = __CustomerID),[PurchaseDate])
      VAR __FirstPurchase = MINX(FILTER('Table',[CustomerID] = __CustomerID && [PurchaseDate] = __FirstPurchaseDate),[Item])
      VAR __PurchasedBench = COUNTROWS(FILTER('Table',[CustomerID] = __CustomerID && [Item] = "Bench")) + 0
      RETURN ROW(
        "FirstPurchaseDate", __FirstPurchaseDate,
        "FirstPurchase", __FirstPurchase,
        "PurchasedBench", __PurchasedBench
      )
    )
  VAR __Result = COUNTROWS(FILTER(__Table,[FirstPurchase] = "Bike" && [PurchasedBench] > 0)) // or use CONCATENATEX to return customer id's
RETURN
  __Result

And for finding days between:

Bikes First Measure =
  VAR __Customers = DISTINCT('Table',[CustomerID])
  VAR __Table = 
    GENERATE(
      __Customers,
      VAR __CustomerID = [CustomerID]
      VAR __FirstPurchaseDate = MINX(FILTER('Table',[CustomerID] = __CustomerID),[PurchaseDate])
      VAR __FirstPurchase = MINX(FILTER('Table',[CustomerID] = __CustomerID && [PurchaseDate] = __FirstPurchaseDate),[Item])
      VAR __PurchasedBench = COUNTROWS(FILTER('Table',[CustomerID] = __CustomerID && [Item] = "Bench")) + 0
      VAR __PurchasedBenchDate = MINX(FILTER('Table',[CustomerID] = __CustomerID && [Item] = "Bench"),[PurchaseDate])
      VAR __DaysBetween = (__PurchaseBenchDate - __FirstPurchaseDate) * 1.
      RETURN ROW(
        "FirstPurchaseDate", __FirstPurchaseDate,
        "FirstPurchase", __FirstPurchase,
        "PurchasedBench", __PurchasedBench,
        "PurchasedBenchDate", __PurchasedBenchDate,
        "DaysBetween", __DaysBetween
      )
    )
  VAR __Result = AVERAGEX(FILTER(__Table,[FirstPurchase] = "Bike" && [PurchasedBench] > 0), [DaysBetween])
RETURN
  __Result

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

v-zhangti
Community Support
Community Support

Hi, @D_PBI 

 

You can try the following methods.
Measure:

First Item = 
Var _mindate=CALCULATE(MIN('Table'[PurchaseDate]),ALLEXCEPT('Table','Table'[CustomerName]))
Return
CALCULATE(MAX('Table'[Item]),FILTER(ALL('Table'),[PurchaseDate]=_mindate))

vzhangti_0-1675762305767.png

First Bike and Item % = 
VAR _N1 = CALCULATE ( DISTINCTCOUNT ( 'Table'[CustomerName] ),
        FILTER ( ALL ( 'Table' ), [First Item] = "Bike" ) )
VAR _N2 = CALCULATE ( DISTINCTCOUNT ( 'Table'[CustomerName] ),
        FILTER ( ALL ( 'Table' ),
            [First Item] = "Bike"
                && [Item] = SELECTEDVALUE ( 'Table'[Item] ) ) )
RETURN
    DIVIDE ( _N2, _N1 ) + 0

vzhangti_1-1675762386435.png

Days first Bike and the other = 
VAR _firstbikedate = CALCULATE ( MIN ( 'Table'[PurchaseDate] ),
        FILTER ( ALL ( 'Table' ),
            [First Item] = "Bike"
                && [CustomerName] = SELECTEDVALUE ( 'Table'[CustomerName] ) ) )
VAR _firstotherdate = CALCULATE ( MIN ( 'Table'[PurchaseDate] ),
        FILTER ( ALL ( 'Table' ),
            [First Item] = "Bike"
                && [Item] = SELECTEDVALUE ( 'Table'[Item] )
                && [CustomerName] = SELECTEDVALUE ( 'Table'[CustomerName] ) ) )
VAR _days = DATEDIFF ( _firstbikedate, _firstotherdate, DAY )
RETURN
    IF ( _days <> BLANK (), _days + 1, BLANK () )

vzhangti_2-1675762528004.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-zhangti
Community Support
Community Support

Hi, @D_PBI 

 

You can try the following methods.
Measure:

First Item = 
Var _mindate=CALCULATE(MIN('Table'[PurchaseDate]),ALLEXCEPT('Table','Table'[CustomerName]))
Return
CALCULATE(MAX('Table'[Item]),FILTER(ALL('Table'),[PurchaseDate]=_mindate))

vzhangti_0-1675762305767.png

First Bike and Item % = 
VAR _N1 = CALCULATE ( DISTINCTCOUNT ( 'Table'[CustomerName] ),
        FILTER ( ALL ( 'Table' ), [First Item] = "Bike" ) )
VAR _N2 = CALCULATE ( DISTINCTCOUNT ( 'Table'[CustomerName] ),
        FILTER ( ALL ( 'Table' ),
            [First Item] = "Bike"
                && [Item] = SELECTEDVALUE ( 'Table'[Item] ) ) )
RETURN
    DIVIDE ( _N2, _N1 ) + 0

vzhangti_1-1675762386435.png

Days first Bike and the other = 
VAR _firstbikedate = CALCULATE ( MIN ( 'Table'[PurchaseDate] ),
        FILTER ( ALL ( 'Table' ),
            [First Item] = "Bike"
                && [CustomerName] = SELECTEDVALUE ( 'Table'[CustomerName] ) ) )
VAR _firstotherdate = CALCULATE ( MIN ( 'Table'[PurchaseDate] ),
        FILTER ( ALL ( 'Table' ),
            [First Item] = "Bike"
                && [Item] = SELECTEDVALUE ( 'Table'[Item] )
                && [CustomerName] = SELECTEDVALUE ( 'Table'[CustomerName] ) ) )
VAR _days = DATEDIFF ( _firstbikedate, _firstotherdate, DAY )
RETURN
    IF ( _days <> BLANK (), _days + 1, BLANK () )

vzhangti_2-1675762528004.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-zhangti - thanks for your response. I haven't attempted the suggestion as Greg_Deckler responded first and I was in the process of following his guidance, which has worked, when you added yours.

Greg_Deckler
Super User
Super User

@D_PBI Try something along the lines of:

Bikes First Measure =
  VAR __Customers = DISTINCT('Table',[CustomerID])
  VAR __Table = 
    GENERATE(
      __Customers,
      VAR __CustomerID = [CustomerID]
      VAR __FirstPurchaseDate = MINX(FILTER('Table',[CustomerID] = __CustomerID),[PurchaseDate])
      VAR __FirstPurchase = MINX(FILTER('Table',[CustomerID] = __CustomerID && [PurchaseDate] = __FirstPurchaseDate),[Item])
      RETURN ROW(
        "FirstPurchaseDate", __FirstPurchaseDate,
        "FirstPurchase", __FirstPurchase
      )
    )
  VAR __Result = COUNTROWS(FILTER(__Table,[FirstPurchase] = "Bike")) // or use CONCATENATEX to return customer id's
RETURN
  __Result
        

You can use similar measures for the rest, here is an example of finding customers who also bough Bench:

Bikes First Measure =
  VAR __Customers = DISTINCT('Table',[CustomerID])
  VAR __Table = 
    GENERATE(
      __Customers,
      VAR __CustomerID = [CustomerID]
      VAR __FirstPurchaseDate = MINX(FILTER('Table',[CustomerID] = __CustomerID),[PurchaseDate])
      VAR __FirstPurchase = MINX(FILTER('Table',[CustomerID] = __CustomerID && [PurchaseDate] = __FirstPurchaseDate),[Item])
      VAR __PurchasedBench = COUNTROWS(FILTER('Table',[CustomerID] = __CustomerID && [Item] = "Bench")) + 0
      RETURN ROW(
        "FirstPurchaseDate", __FirstPurchaseDate,
        "FirstPurchase", __FirstPurchase,
        "PurchasedBench", __PurchasedBench
      )
    )
  VAR __Result = COUNTROWS(FILTER(__Table,[FirstPurchase] = "Bike" && [PurchasedBench] > 0)) // or use CONCATENATEX to return customer id's
RETURN
  __Result

And for finding days between:

Bikes First Measure =
  VAR __Customers = DISTINCT('Table',[CustomerID])
  VAR __Table = 
    GENERATE(
      __Customers,
      VAR __CustomerID = [CustomerID]
      VAR __FirstPurchaseDate = MINX(FILTER('Table',[CustomerID] = __CustomerID),[PurchaseDate])
      VAR __FirstPurchase = MINX(FILTER('Table',[CustomerID] = __CustomerID && [PurchaseDate] = __FirstPurchaseDate),[Item])
      VAR __PurchasedBench = COUNTROWS(FILTER('Table',[CustomerID] = __CustomerID && [Item] = "Bench")) + 0
      VAR __PurchasedBenchDate = MINX(FILTER('Table',[CustomerID] = __CustomerID && [Item] = "Bench"),[PurchaseDate])
      VAR __DaysBetween = (__PurchaseBenchDate - __FirstPurchaseDate) * 1.
      RETURN ROW(
        "FirstPurchaseDate", __FirstPurchaseDate,
        "FirstPurchase", __FirstPurchase,
        "PurchasedBench", __PurchasedBench,
        "PurchasedBenchDate", __PurchasedBenchDate,
        "DaysBetween", __DaysBetween
      )
    )
  VAR __Result = AVERAGEX(FILTER(__Table,[FirstPurchase] = "Bike" && [PurchasedBench] > 0), [DaysBetween])
RETURN
  __Result

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler - thanks for your prompt response. Very much appreaciated. I've got what I needed working based on your guidance. Kudos and suggestion vote given.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors