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.
Define what you mean by "previous" and then choose if you want to use Visual Calculations (which have a dedicated PREVIOUS DAX function), one of the window functions (likely OFFSET(-1)), or a more complex DAX statement finding the penultimate value.
Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Please show the expected outcome based on the sample data you provided.
Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216