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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
bBI
Frequent Visitor

[DAX] Check if a value from table 1 is present in table 2 (with table 2 filtered by date)

Been wreacking my brain on this one. I manage to check with COUNTROWS and LOOKUPVALUE but as soon as I add date filters it breaks down. Hoping to get some help from the community.

 

The idea is to add to the sales table a calculated column which shows whether the customer visited in the 5 months before their sale our facebook page.

 

Below two anonymized tables by way of an example:

SalesTable

Date of SaleCustomer name
2021-03-16Fred Flintstone
2021-03-18Wilma Flintstone
2021-03-16Barney Rubble
2020-12-23Betty Rubble

 

VisitorTable

Date of visitVisitor name
2020-12-28Fred Flintstone
2020-06-15Fred Flintstone
2020-07-18Wilma Flintstone
2020-08-15Wilma Flintstone
2020-09-12Wilma Flintstone
2020-12-28Barney Rubble
2021-03-14Barney Rubble

 

Result should be:

Date of SaleCustomer nameRecently visited?
2021-03-16Fred Flintstoneyes
2021-03-18Wilma Flintstoneno
2021-03-16Barney Rubbleyes
2020-12-23Betty Rubbleno

 

Fred visited 4 months before purchase ==> yes

Wilma visited 6 months before purchase ==> no

Barney visited 2 days before purchase ==> yes

Betty never visited ==> no

 

Any pointers as how to achieve such result with dax would be very much appreciated

1 ACCEPTED SOLUTION
V-pazhen-msft
Community Support
Community Support

@bBI 

 

You can just create the following measure, check attached pbix if needed.

 

Measure = IF(MAX([Customer name])in VALUES('Table (2)'[Visitor name]) && MAX([Date of Sale])<=EDATE(MAX('Table (2)'[Date of visit]),5),"Yes","No")
 
V-pazhen-msft_1-1616483030964.png

 


Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
V-pazhen-msft
Community Support
Community Support

@bBI 

 

You can just create the following measure, check attached pbix if needed.

 

Measure = IF(MAX([Customer name])in VALUES('Table (2)'[Visitor name]) && MAX([Date of Sale])<=EDATE(MAX('Table (2)'[Date of visit]),5),"Yes","No")
 
V-pazhen-msft_1-1616483030964.png

 


Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.

OwenAuger
Super User
Super User

Hi again @bBI 

We do have to be careful calling CALCULATE in a calculated column, as it causes "context transition" where the current row of the table (and related tables on the 1-side of many-1 relationships) is added to the filter context.

This can interfere with what we are trying to calculate.

 

Here is a version that should work. It uses REMOVEFILTERS to remove filters from Visitor that have resulted from context transition, for the purpose of this calculation.

Recently visited? = 
VAR CurrentDateOfSale = 'Sales'[Date of Sale]
-- Check if Customer visited between these dates
VAR DateRangeStart = EDATE ( CurrentDateOfSale, -4 )
VAR DateRangeEnd = CurrentDateOfSale
-- Do any rows exist for current customer in the 4 month date range?
VAR RecentlyVisited =
    CALCULATE ( 
        NOT ISEMPTY ( Visitor ),
        REMOVEFILTERS ( Visitor ),
        Visitor[Date of visit] >= DateRangeStart,
        Visitor[Date of visit] <= DateRangeEnd,
        TREATAS ( { 'Sales'[Customer name] }, Visitor[Visitor name] )
    )
RETURN
    IF ( RecentlyVisited, "yes", "no" )

 If it's still not working, could you post a relationships diagram?

 

All the best,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn
bBI
Frequent Visitor

This solved the issue

OwenAuger
Super User
Super User

Hi @bBI

You could create a column like this (this doesn't assume any relationships between tables or that you have a date table):

Recently visited? = 
VAR CurrentDateOfSale = 'Sales'[Date of Sale]
-- Check if Customer visited between these dates
VAR DateRangeStart = EDATE ( CurrentDateOfSale, -4 )
VAR DateRangeEnd = CurrentDateOfSale
-- Do any rows exist for current customer in the 4 month date range?
VAR RecentlyVisited =
    CALCULATE ( 
        NOT ISEMPTY ( Visitor ),
        Visitor[Date of visit] >= DateRangeStart,
        Visitor[Date of visit] <= DateRangeEnd,
        TREATAS ( { 'Sales'[Customer name] }, Visitor[Visitor name] )
    )
RETURN
    IF ( RecentlyVisited, "yes", "no" )

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn
bBI
Frequent Visitor

Hello Owen,

 

 

Thanks for the code. I was unaware of the TREATAS() function.

 

Just pasted it into my report with the sample data above.

 

 

Everyone is coming up as "no".

 

I dialed up the months to -200 and the result remains "no" for all customers.

 

Do you know what could be the issue?

bBI
Frequent Visitor

@OwenAuger ok, very weird but when I remove the relationships each table had with my Date table the DAX works.

 

However I do need those date relations for other measures and visuals.

 

Any idea how to solve this? Your input would be much appreciated

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.