This article describes how to go from tables that show all data...
...to tables that only show data for the last 2 periods, for example:
Requirements
Please note this requires the Data Stories module or a Displayr license.
A data set that includes a Nominal (also known as categorical) wave variable. In this example, we have a variable called Wave that tells us what quarter the survey was completed. Waves must be in ascending order when shown in a table, like:
Instructions for how to convert date/time variables to nominal variables can be found here: How to Create a Categorical Variable from a Date/Time Variable.
Method
1. Select your data set in the Data Sources tree.
2. In the Data Sources tree, hover over any variable and click the Plus (+) then select Custom Code > R - Numeric.
3. Paste the below under General > R CODE in the object inspector:
unique.waves = levels(Wave) #Get wave categories
last.two.waves = unique.waves[(length(unique.waves)-1):length(unique.waves)] #Find last 2 waves
Wave %in% last.two.waves
- The above code gets the levels or categories from the Wave variable. You will need to replace Wave with the Name of your nominal wave variable.
- It then identifies the last 2 wave categories. Note, this code assumes the waves are in the correct order.
- Finally, we return TRUE or FALSE based on whether the data matches these 2 wave categories.
4. Tick Usable as a filter.
5. Give the filter a Name and Label.
6. OPTIONAL: Update the Structure to Nominal or Ordinal.
7. Select the relevant outputs and apply this filter via Data > FILTERS & WEIGHT > Filter(s) in the object inspector.
Next
How to Create a Custom Numeric R Variable
How to Build Tables that Automatically Filter to the Latest Periods
How to Create a Categorical Variable from a Date/Time Variable