Forum Discussion
Current vs Previous Period based on date selection in drop down, populate Table or Matrix Power BI
- 1 year ago
Hi sb4pbi
Use a column from a disconnected dates table for the dropdown as using one from the same or related table will show only that are selected from the slicer - eg., if you select April 30, your visual will show this date only. Create one using DAX or M. Here's a sample DAX calc table.
DisconnectedDates = DISTINCT ( 'Data'[Date] )Take note that there is no relationship between DisconnectedDates and Data in the screenshot below
Create the following measures
Current Week = VAR _RefDate = MAX ( DisconnectedDates[Date] ) VAR _start = _RefDate - 7 VAR _end = _RefDate - 1 RETURN CALCULATE ( SUM ( Data[Value] ), KEEPFILTERS ( Data[Date] >= _start && Data[Date] <= _end ) )Previous Week = VAR _RefDate = MAX ( DisconnectedDates[Date] ) VAR _start = _RefDate - 14 VAR _end = _RefDate - 8 RETURN CALCULATE ( SUM ( Data[Value] ), KEEPFILTERS ( Data[Date] >= _start && Data[Date] <= _end ) )Create the following measure that can be used to sort a table with the current dates first then the previous ones.
Current or Prev = VAR _number = SWITCH ( TRUE (), NOT ( ISBLANK ( [Current Week] ) ), 1, NOT ( ISBLANK ( [Previous Week] ) ), 2 ) RETURN IF ( NOT ( ISBLANK ( _number ) ), REPT ( UNICHAR ( 8203 ), _number ) )Rename this measure to a space once added to the table so the column name doesn't show. Resize it further so the arrow is not visible.
Please see the attached pbix.
Hi sb4pbi
Use a column from a disconnected dates table for the dropdown as using one from the same or related table will show only that are selected from the slicer - eg., if you select April 30, your visual will show this date only. Create one using DAX or M. Here's a sample DAX calc table.
DisconnectedDates =
DISTINCT ( 'Data'[Date] )
Take note that there is no relationship between DisconnectedDates and Data in the screenshot below
Create the following measures
Current Week =
VAR _RefDate =
MAX ( DisconnectedDates[Date] )
VAR _start = _RefDate - 7
VAR _end = _RefDate - 1
RETURN
CALCULATE (
SUM ( Data[Value] ),
KEEPFILTERS ( Data[Date] >= _start && Data[Date] <= _end )
)
Previous Week =
VAR _RefDate =
MAX ( DisconnectedDates[Date] )
VAR _start = _RefDate - 14
VAR _end = _RefDate - 8
RETURN
CALCULATE (
SUM ( Data[Value] ),
KEEPFILTERS ( Data[Date] >= _start && Data[Date] <= _end )
)
Create the following measure that can be used to sort a table with the current dates first then the previous ones.
Current or Prev =
VAR _number =
SWITCH (
TRUE (),
NOT ( ISBLANK ( [Current Week] ) ), 1,
NOT ( ISBLANK ( [Previous Week] ) ), 2
)
RETURN
IF ( NOT ( ISBLANK ( _number ) ), REPT ( UNICHAR ( 8203 ), _number ) )
Rename this measure to a space once added to the table so the column name doesn't show. Resize it further so the arrow is not visible.
Please see the attached pbix.