Forum Discussion

ReubenLam's avatar
ReubenLam
Frequent Visitor
9 years ago
Solved

Using a filter as a value in an expression.

Here’s a problem I hope there’s an answer for.   My data table has a list of stores and in that table it includes two date fields (Store Close date and a date when the store becomes a comparative s...
  • technolog's avatar
    2 years ago

    The problem you're facing is quite common when trying to incorporate a disconnected date table to filter another table based on different date-related criteria. In DAX (the formula language for Power BI and other Microsoft BI tools), you can indeed retrieve a single value from a filter context and use that in a calculation. In your case, you want to get the selected date from the date table's filter context.

    To get the selected date from the filter context, we'll use the MAX function (or MIN, either one will work assuming only one date is selected at a time).

    Let's assume your date table is named DateTable and the date column in that table is DateValue.

    To get the selected date:

    SelectedDate = MAX(DateTable[DateValue])
    Next, let's create a measure that counts the stores that were comp stores at the selected date:

    CountOfCompStores =
    VAR CurrentDate = [SelectedDate]
    RETURN
    CALCULATE(
    COUNTA('Stores'[StoreName]),
    FILTER(
    'Stores',
    ('Stores'[compdate] <= CurrentDate && CurrentDate <= 'Stores'[ClosedDate]) ||
    ('Stores'[compdate] <= CurrentDate && ISBLANK('Stores'[ClosedDate]))
    )
    )
    This measure first captures the currently selected date into a variable called CurrentDate and then uses CALCULATE and FILTER to determine which rows of the 'Stores' table meet the comp store criteria based on CurrentDate.

    By using this measure in your report, users can select a date from DateTable, and the measure will show the count of stores that were comp stores at that specific date, as per your rules.

    Please note that this measure assumes that your store table's name is 'Stores' and has a column 'StoreName' to count. Adjust the table and column references accordingly.