Forum Discussion

arielschapiro's avatar
arielschapiro
Regular Visitor
3 years ago
Solved

Help with dynamic filtering

Hi! I'm having a problem I can't quite resolve creating a report in PowerBI.   I have a table with a start and end date and need users to be able to filter a report dynamically by date range. The c...
  • arielschapiro's avatar
    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