Forum Discussion

markmsc's avatar
markmsc
Icon for Resolver I rankResolver I
1 year ago
Solved

Building a slicer where each slice can contain the same data?

Hello community.   I have a slicer based on a date column in a table.  I'd like the user to be able to slice by dates in the last 7 days, last 14 days, last 21 days, and last 28 days.   The sinpl...
  • GeraldGEmerick's avatar
    1 year ago

    markmsc One method to make this work would be to create a separate table with no relationships that lists your slicer choices. Then, you could create a measure that returns 1 if a row of data is within the desired date range and 0 otherwise. You could then use this measure in a visual level filter and set it to only display rows/items where the measure is 1. Something like the following might work:

    My Filter Measure = 
    VAR _Days = 
      SWITCH( SELECTEDVALUE( 'Slicer'[Item] ),
        "Last 7 Days", 7,
        "Last 14 Days", 14,
        "Last 21 Days", 21,
        "Last 28 Days", 28,
      )
    VAR _Date = MAX( 'Table'[Date] )
    VAR _Result = IF( _Date <= TODAY() && _Date >= TODAY()-_Days, 1, 0 )
    RETURN _Result
  • Ilgar_Zarbali's avatar
    1 year ago

    Great question!

    1. Create a disconnected slicer table
      Create a small table with your choices. (No relationships needed.)

      Period =
      DATATABLE (
      "Label", STRING, "Days", INTEGER,
      {
      { "Last 7 days", 7 },
      { "Last 14 days", 14 },
      { "Last 21 days", 21 },
      { "Last 28 days", 28 }
      }
      )

      Use Period[Label] in a slicer. I recommend turning Single Select = On

    2. Add a “Selected N” helper measure
      This reads the slicer. If nothing is chosen, it defaults to 7.
      Selected Days :=
      VAR N = MAX ( Period[Days] )
      RETURN IF ( NOT ISBLANK ( N ), N, 7 )

    3. Choose the “as-of” date (respects your date slicer)
      This follows your preferred style (using MAXX with ALLSELECTED) so it anchors on the last selected date if a date slicer is present; otherwise it uses the max date in context.

      Selected Max Date :=
      MAXX ( ALLSELECTED ( 'Date'[Date] ), 'Date'[Date] )

      Replace 'Date'[Date] with your model’s primary date column. (No USERELATIONSHIP needed if your active relationship is already correct.)


    4. Build a reusable date-window measure

      Is In Selected Period :=
      VAR N = [Selected Days]
      VAR MaxDate = [Selected Max Date]
      VAR StartDate = MaxDate - N + 1
      VAR CurrentDate = MAX ( 'Date'[Date] )
      RETURN
      IF ( CurrentDate >= StartDate && CurrentDate <= MaxDate, 1, 0 )

    5. Filter visuals (easy, reusable way)

      Is In Selected Period :=
      VAR N = [Selected Days]
      VAR MaxDate = [Selected Max Date]
      VAR StartDate = MaxDate - N + 1
      VAR CurrentDate = MAX ( 'Date'[Date] )
      RETURN
      IF ( CurrentDate >= StartDate && CurrentDate <= MaxDate, 1, 0 )

      OR

    6. Wrap your core measures

      Sales (Last N Days) :=
      VAR N = [Selected Days]
      VAR MaxDate = [Selected Max Date]
      RETURN
      CALCULATE (
      [Total Sales], -- your base measure
      DATESINPERIOD ( 'Date'[Date], MaxDate, -N, DAY ) -- rolling N-day window
      )

      Use this pattern for any measure (counts, distincts, amounts). DATESINPERIOD is efficient and readable.

      You’re done—an elegant “Last N days” slicer without messy buckets.