Search code examples
cube.js

Cube.js time range best practice


I have table with item price for date ranges. what is the best way to model this in cube.js to allow time dimension queries like price over time, or average price for item?

Thanks!

the table looks like:

CREATE pricing test_timestamp (
    id INT AUTO_INCREMENT PRIMARY KEY,
    itemId VARCHAR(255) NOT NULL,
    price INT,
    from TIMESTAMP,
    to TIMESTAMP
);

Solution

  • Considering periods as non overlapping:

    cube(`Pricing`, {
      sql: `select itemId, price, from as timestamp from pricing_test_timestamp
      UNION ALL
      select itemId, -1 * price as price, to as timestamp from pricing_test_timestamp
      `,
    
      measures: {
        price: {
          sql: `price`,
          type: `sum`,
          rollingWindow: {
            trailing: `unbounded`
          }
        }
      },
    
      dimensions: {
        timestamp: {
          sql: `timestamp`,
          type: `number`
        }
      }
    })