Forum Discussion
Optimize DAX Code
Hello everyone,
This fourmula count unique number of stores in time window of 42 days backward from the selected date in slicer.
However formula runs pretty slow when i change the name of product.
Could you please help me with optimizing my formula?
Thank you in advance!
Best regards
---------------------------------------------------------------------------------
11 Replies
- ppm1Solution Sage
Based on your text description, why not just do a distinctcount of stores with a time intelligence expression to go back 42 days? Something like this.
ND Daily = VAR selDateEnd = SELECTEDVALUE ( 'Working Days'[WorkingDate] ) VAR selDateStart = selDateEnd - 42 RETURN CALCULATE ( DISTINCTCOUNT ( WP_FactInput[Code] ), WP_FactInput[VisitTime] >= selDateStart && WP_FactInput[VisitTime] <= selDateEnd )If more complex than that, can you can post a link to a mock version of your pbix with enough data to see slowness (e.g., 1 sec or more)? The community will likely provide alternate faster DAX.
Pat
- AnonymousNot applicable
ppm1 , first of all, thank you for the quick response. I am attaching a PBI file with Mock data.
I will try to describe the problem in more detail: My Fact table in the model contains transactions that represent visits of employees to various stores. My goal is to calculate a numerical distribution (number of stores where the product selected in the slicer is available) depending on the working day in the month (value in the columns of the matrix visual) with a time window of 42 days backwards from the selected working day. I only need the LAST VISIT TO THE STORE in this context based on the criteria Working Day.
Mock Data PBI Link : https://www.dropbox.com/s/w9mmurd4xypmg7g/Mock%20PBI.pbix?dl=0https://www.dropbox.com/scl/fo/fdnqycmyseujmbekpab27/h?dl=0&rlkey=gld45txq515jujztu0s1l6a7c
- ppm1Solution Sage
I looked at this for a bit and ran out of time for tonight. Not sure I fully understand the logic of your existing measure. The measure below is much more performant (76 ms vs 3600 ms) and does what I think you are looking for but does not match all of your current values. If you know your current values are correct, perhaps this approach will give you some ideas to tweak it.
ND daily 2 = VAR selDateEnd = SELECTEDVALUE ( tSelectedDates[WorkingDate] ) VAR selDateStart = selDateEnd - 42 VAR Summarized = CALCULATETABLE ( SUMMARIZE ( FactND, FactND[StoreID], "cMaxDate", MAX ( FactND[VisitTime] ) ), FactND[VisitTime] <= selDateEnd && FactND[VisitTime] >= selDateStart ) RETURN CALCULATE ( [BaseModelCount], TREATAS ( Summarized, FactND[StoreID], FactND[VisitTime] ) )Pat
- tamerj1Community Champion
Hi Anonymous
please try
ND Daily =
VAR selDateEnd =
SELECTEDVALUE ( 'Working Days'[WorkingDate] )
VAR selDateStart = selDateEnd - 42
VAR Summarized =
ALL ( WP_FactInput[Code], WP_FactInput[VisitTime] )
VAR tUpper =
FILTER (
Summarized,
WP_FactInput[VisitTime] <= selDateEnd
&& WP_FactInput[VisitTime] >= selDateStart
)
VAR tLower =
GROUPBY (
tUpper,
[Code],
"VisitTime", MAXX ( CURRENTGROUP (), WP_FactInput[VisitTime] )
)
VAR Final =
INTERSECT ( tUpper, tLower )
RETURN
COUNTROWS ( Final )- AnonymousNot applicable
Hi tamerj1 , I tried this formula. Unfortunately, it's not working, filters from the dimensional tables don't propagate through the measure.
Here's a link to mock data in the attachment : https://www.dropbox.com/s/w9mmurd4xypmg7g/Mock%20PBI.pbix?dl=0
- tamerj1Community Champion
Hi Anonymous
Please refer to attached sample file with the solutionND daily = SUMX ( CROSSJOIN ( VALUES ( DimEmployee[EMP Name] ), VALUES ( tSelectedDates[WorkingDate] ) ), VAR selDateEnd = tSelectedDates[WorkingDate] VAR selDateStart = selDateEnd - 42 VAR Summarized = SUMMARIZE ( CALCULATETABLE ( FactND ), FactND[StoreID], FactND[VisitTime] ) VAR tUpper = FILTER ( Summarized, FactND[VisitTime] <= selDateEnd && FactND[VisitTime] >= selDateStart ) VAR tLower = GROUPBY ( tUpper, FactND[StoreID], "VisitTime", MAXX ( CURRENTGROUP(), FactND[VisitTime] ) ) VAR Final = INTERSECT ( tUpper, tLower ) RETURN COUNTROWS ( Final ) )