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
jony_gloeckner
Frequent Visitor

Present products on last visit

Hello Community,

 

I need help with one complex DAX formula.

I want to know how many locations have product marked with IsPresent=1 on last visit from sales rep.

Sales rep visits are tracked with ID and DistDate columns.

Tricky part is that formula needs to be calculated dynamically from maximum selected date on date slicer and calculate the results only for period of maximum selected date – 30 days. 

 

Here is table with data.

We have next columns:

- ID -> uniqe visit ID - this column has uniqe visit id written down in integer. This column can also be used for finding out which is the last visit

- CustomerID - this is location of product

- DistDate - date when visit happened

- UserID - Sales rep code

- ProductID - Product code

- isPresent - flag that sales rep gives to product if present on location

 

Here is table with data: 

ID

CustomerID

DistDate

UserID

ProductID

isPresent

36094

CI2995

2024-01-25

P1111011

33027

CI2995

2024-01-11

P1111010

37326

C1902356

2024-01-31

P1131150

38261

C1902357

2024-02-05

P1131151

36695

C1902357

2024-01-29

P1141151

35110

C19023572024-01-22P1121150

32237

C19023572024-01-08P1121151

38016

C1902375

2024-02-02P1121150

35814

C1902376

2024-01-24P1111151

37657

C1902377

2024-02-01P1111150

34734

C1902377

2024-01-18P1111150

35295

C1902378

2024-01-23P1111151

32548

C1902378

2024-01-10P1111151

37694

L3565025

2024-02-01P1111150

34694

L3565025

2024-01-18P1111151

37996

L3565026

2024-02-02P1111151

35729

L3565028

2024-01-24P1111151

37083

D3565029

2024-01-30P1111151

 

How would you approach this issue?

1 ACCEPTED SOLUTION
AMeyersen
Resolver I
Resolver I

Hi @jony_gloeckner ,

 

this sounds slightly complicated, so I'd make excessive use of table variables to keep track of what I'm doing.

locationsProductPresent =

-- get the correct date range
VAR _endDate =
    CALCULATE ( MAX ( 'Table'[Date] ) )
VAR _startDate = _endDate - 30
VAR _potentialVisits =
    FILTER ( 'Table', [Date] >= _startDate && [Date] <= _endDate ) 

-- get a table of last visit per location and product
VAR _lastVisitPerProduct =
    SUMMARIZE (
        _potentialVisits,
        [CustomerID],
        [ProductID],
        "@maxVisitID", MAX ( 'Table'[ID] )
    ) 

-- get a table of last visit per location and product when the product was present
VAR _lastVisitPresent =
    SUMMARIZE (
        FILTER ( _potentialVisits, [isPresent] = 1 ),
        [CustomerID],
        [ProductID],
        "@maxVisitID", MAX ( 'Table'[ID] )
    ) 

-- only keep identical rows of both tables (last visit overall AND last visit when product was present)
VAR _isPresentOnLastVisit =
    INTERSECT ( _lastVisitPerProduct, _lastVisitPresent ) 

-- get a list of distinct location which satisfy your critera
VAR _locations =
    SUMMARIZE ( _isPresentOnLastVisit, [CustomerID] ) 

-- count number of locations
RETURN
    COUNTROWS ( _locations )

 

You might have to tweak it a bit to get the correct results, but the logic should work.

This approach might not work in very large models due to performance issues. In this case you are probably forced to do some pre-calculations in PowerQuery to reduce query runtime.

View solution in original post

1 REPLY 1
AMeyersen
Resolver I
Resolver I

Hi @jony_gloeckner ,

 

this sounds slightly complicated, so I'd make excessive use of table variables to keep track of what I'm doing.

locationsProductPresent =

-- get the correct date range
VAR _endDate =
    CALCULATE ( MAX ( 'Table'[Date] ) )
VAR _startDate = _endDate - 30
VAR _potentialVisits =
    FILTER ( 'Table', [Date] >= _startDate && [Date] <= _endDate ) 

-- get a table of last visit per location and product
VAR _lastVisitPerProduct =
    SUMMARIZE (
        _potentialVisits,
        [CustomerID],
        [ProductID],
        "@maxVisitID", MAX ( 'Table'[ID] )
    ) 

-- get a table of last visit per location and product when the product was present
VAR _lastVisitPresent =
    SUMMARIZE (
        FILTER ( _potentialVisits, [isPresent] = 1 ),
        [CustomerID],
        [ProductID],
        "@maxVisitID", MAX ( 'Table'[ID] )
    ) 

-- only keep identical rows of both tables (last visit overall AND last visit when product was present)
VAR _isPresentOnLastVisit =
    INTERSECT ( _lastVisitPerProduct, _lastVisitPresent ) 

-- get a list of distinct location which satisfy your critera
VAR _locations =
    SUMMARIZE ( _isPresentOnLastVisit, [CustomerID] ) 

-- count number of locations
RETURN
    COUNTROWS ( _locations )

 

You might have to tweak it a bit to get the correct results, but the logic should work.

This approach might not work in very large models due to performance issues. In this case you are probably forced to do some pre-calculations in PowerQuery to reduce query runtime.

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.