Forum Discussion
Help with dynamic filtering
- 3 years ago
With a lot of help from jdbuchanan71 , I was finally able to solve the problem.
First, I created a Calendar table using start and end dates based on my data.Calendar = CALENDAR(MIN('Table'[StartDate]),MAX('Table'[EndDate]))
Then, I added a measure to the main table that gets the filtered values and applies the count and filtering criteria using the recommendations in this post. A DISTINCTCOUNT was enough but I wanted to be super safe.
In this measure, I'm getting the min and max values selected in the slicer and assigning them to variables. Then counting ids based on the required criteria applying conditions in the filter.Number of Projects = VAR MinDate = MIN ( 'Calendar'[Date] ) VAR MaxDate = MAX ( 'Calendar'[Date] ) RETURN CALCULATE( DISTINCTCOUNTNOBLANK(Table[Id]) ,FILTER( Table ,OR( AND( Table[StartDate] <= MinDate ,Table[EndDate] >= MinDate || Table[EndDate] = BLANK() ) ,AND( Table[StartDate] >= MinDate ,Table[StartDate] <= MaxDate ) ) ) )
Once all of this was set, all I had to do was to use the new measure for counts, it even worked like a charm after adding related tables to the model (watch out for relationship directionality).Thanks jdbuchanan71 for all your help, this is now working nicely
If you have a calendar table in your model that is not connect to your fact table you can use a measure like this.
Record_Count =
VAR MinDate = MIN ( DATES[Date] )
VAR MaxDate = MAX ( DATES[Date] )
RETURN
CALCULATE (
COUNTROWS ( YourTable ),
YourTable[Start Date] <= MaxDate,
YourTable[End Date] >= MinDate
)
Then set a filter on your visual for [Record_Count] Is Not Blank.
The users would set the date range using the date column from the DATES table.
Thank you so much, this is a very nice, simple way to make it work, I really appreciate your insight!
I tried to use this solution you described, it matches my data model perfectly, and I came to an error that I don't understand the root cause for.
The measure I ended up using is similar to the one you posted but it adds a few conditions:
The weird thing is that I have an unfiltered distinct count of row ids of 313 if I only add locations and 316 if I count statuses and if I mix them in a matrix pivot-table style, the count goes down to 302. The model is very simple, there is only one table with all the data and I am not applying any other filters for the status and location columns. Also, if I leave the filter by measure aside, the count is 316 for both columns. Would you know why this might be happening?
Huge thanks again!
- jdbuchanan713 years ago
Super User
You should only need to check the start date vs the max date and the end date vs the min date like this.
DateFilterCount = VAR MinDate = MIN ( 'Calendar'[Date] ) VAR MaxDate = MAX ( 'Calendar'[Date] ) RETURN CALCULATE ( COUNTROWS ( Main ), Main[StartDate] <= MaxDate, Main[EndDate] >= MinDate )- arielschapiro3 years agoRegular Visitor
Thank you, that is a really good point! Unfortunately it didn't solve the count differences. I really appreciate it!
- Ashish_Mathur3 years ago
Super User
Hi,
Share the link from where i can download your PBI file.
- arielschapiro3 years agoRegular Visitor
Here's a sample file that mimics the DB. It works better than the real one but the issue persists. Thank you so much for your help!
- Ashish_Mathur3 years ago
Super User
Hi,
Please fill dates in the end date column. I cannot assume all blanks in that column to be today's date because in some of those rows, the start date itself is after today's date.