7  Working with time in PX-files

7.1 TIMEVAL keyword in PX-file

The PX-file format has a specific keyword to deal with time series data. It is not so different from any other variable, but it helps the plotting feature in PX-web to identify that the table contains a time variable and make a meaningful plot for the user.

The TIMEVAL keyword can have a format of yearly, half-yearly, quarterly, monthly or weekly.

It follows the following formats:

  • Annual: 2024, 2025
  • Half-yearly: 2025H1, 2025H2
  • Quarterly: 2025Q1, 2025Q2, 2025Q3, 2025Q4
  • Monthly: 2025M01, 2025M02, 2025M03, 2025M04…
  • Weekly: 2025W01, 2025W02, 2025W03, 2025W04…

Below we import an example PX-file with employment data. It has the variables Time and Quarter and we want to join them together to make it easier for our users for PX-web when using the plot feature.

library(pxmake)
library(tidyverse)

px("data/Employment_example_tab1.px") %>% 
  px_data()
# A tibble: 252 × 5
   Sex    Education                    Time  Quarter figures_
   <chr>  <chr>                        <chr> <chr>      <dbl>
 1 " All" Employed population aged 16+ 2021  Q1      3204924.
 2 " All" Employed population aged 16+ 2021  Q2      3133607.
 3 " All" Employed population aged 16+ 2021  Q3      3179124.
 4 " All" Employed population aged 16+ 2021  Q4      3633132.
 5 " All" Employed population aged 16+ 2022  Q1      3585651.
 6 " All" Employed population aged 16+ 2022  Q2      3317268.
 7 " All" Employed population aged 16+ 2022  Q3      3711254.
 8 " All" Employed population aged 16+ 2022  Q4      3571236.
 9 " All" Employed population aged 16+ 2023  Q1      3803942.
10 " All" Employed population aged 16+ 2023  Q2      3984502.
# ℹ 242 more rows

This can be done with just a few lines of code, though the code below is quite compact and has a lot going on. It imports the PX-file we want to change, retrieve the data, joining together year and quarter to one variable called Time. Afterwards, the data in the PX-file is changed to our new modified dataset. We set the TIMEVAL keyword to Time and save the new PX-file.

px("data/Employment_example_tab1.px") %>% 
  px_data() %>% 
  # Use .keep to only preserve Time and drop quarter
  mutate(Time = paste0(Time, Quarter), .keep = "unused") %>% 
  # Assign data to px-file
  px_data(px("data/Employment_example_tab1.px"), .) %>% 
  px_timeval("Time") %>% # set timeval
  px_save("time_px_example.px")

The former PX-file’s metadata also had metadata for the variable Quarter, which we have now removed from the data. Luckily the metadata is also updated when modifying the data, so the metadata for Quarter is removed, but all other metadata, like title, content, source etc. are preserved from the initial PX-file.

The code below gives the same result, but with an intermediate step, which gives better readability.

Simpler code
time_example_data <- px("data/Employment_example_tab1.px") %>% 
  px_data() %>% 
  # Use .keep to only preserve Time and drop quarter
  mutate(Time = paste0(Time, Quarter), .keep = "unused") 

# Modifying data in the PX-file
px("data/Employment_example_tab1.px") %>% 
  px_data(time_example_data) %>% 
  px_timeval("Time") %>% # Set timeval
  px_save("time_px_example.px") # Save new file