March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe 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
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 Sale | Customer name |
2021-03-16 | Fred Flintstone |
2021-03-18 | Wilma Flintstone |
2021-03-16 | Barney Rubble |
2020-12-23 | Betty Rubble |
VisitorTable
Date of visit | Visitor name |
2020-12-28 | Fred Flintstone |
2020-06-15 | Fred Flintstone |
2020-07-18 | Wilma Flintstone |
2020-08-15 | Wilma Flintstone |
2020-09-12 | Wilma Flintstone |
2020-12-28 | Barney Rubble |
2021-03-14 | Barney Rubble |
Result should be:
Date of Sale | Customer name | Recently visited? |
2021-03-16 | Fred Flintstone | yes |
2021-03-18 | Wilma Flintstone | no |
2021-03-16 | Barney Rubble | yes |
2020-12-23 | Betty Rubble | no |
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
Solved! Go to Solution.
You can just create the following measure, check attached pbix if needed.
Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.
You can just create the following measure, check attached pbix if needed.
Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.
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
This solved the issue
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
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?
@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
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
123 | |
86 | |
73 | |
57 | |
52 |
User | Count |
---|---|
201 | |
137 | |
108 | |
73 | |
68 |