Forum Discussion
Building a slicer where each slice can contain the same data?
- 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 - 1 year ago
Great question!
- 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 - 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 ) - 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.) - 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 ) - 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 - 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.
- Create a disconnected slicer table
- markmsc1 year ago
Resolver I
Hi Aburar_123 -- Heh, to be perfectly honest: it never occurred to me. I think I was focused on the exact presentation that I was looking for so this built-in option didn't come to mind. Ultimately it's a bit fussier from a user perspective than I want, so it's not what I went with, but indeed it would have worked functionally. Thank you for reminding me that it was an option.