> ## Documentation Index
> Fetch the complete documentation index at: https://api.globalwebindex.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Examine Trends over Time

To explore how behaviours or preferences shift over time, a powerful method is to use a Crosstab analysis. A Crosstab
structures data into a matrix where rows or columns represent audiences, specific data points, or variables
like time periods or locations. That way, Crosstabs allow you to examine large amounts of data and spot trends, compare
audience behaviours, and generate actionable insights.

For example, using GWI’s API, you can track how Facebook usage among Boomers and Millennials has changed over the last
four quarters. Such analysis helps identify behavioural shifts and refine marketing strategies based on evolving trends.

You can use the `waves` question and its datapoints in Crosstab columns to see the shift in trends over time.
For that, you can set appropriate audiences (we'll call them `fb_boomers` and `fb_millennials`) *as rows* and the waves
of the last 4 quarters *as columns*:

<CodeGroup>
  ```bash Create Crosstab theme={null}
  curl -X POST "https://api.globalwebindex.com/v2/saved/crosstabs" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -d @req.json
  ```
</CodeGroup>

The response is potentially a large JSON Lines file but you can extract the relevant data from response with a script
like the one provided in examples.

<RequestExample>
  ```json req.json theme={null}
  {
    "base_audience": {
      "id": "1",
      "name": "All Internet Users",
      "expression": null
    },
    "locations": [
      "s2_1"
    ],
    "waves": [
      "q1_2024",
      "q2_2024",
      "q3_2024",
      "q4_2024"
    ],
    "rows": [
      {
        "id": "1",
        "name": "fb_boomers",
        "expression": {
          "and": [
            {
              "question": "q42011a",
              "datapoints": [
                "q42011a_3"
              ],
              "suffixes": [
                1,
                2
              ]
            },
            {
              "question": "q3new",
              "datapoints": [
                "q3new_1"
              ]
            }
          ]
        }
      },
      {
        "id": "2",
        "name": "fb_millennials",
        "expression": {
          "and": [
            {
              "question": "q42011a",
              "datapoints": [
                "q42011a_3"
              ],
              "suffixes": [
                1,
                2
              ]
            },
            {
              "question": "q3new",
              "datapoints": [
                "q3new_3"
              ]
            }
          ]
        }
      }
    ],
    "columns": [
      {
        "id": "1",
        "name": "Q1 2024",
        "expression": {
          "question": "waves",
          "datapoints": [
            "q1_2024"
          ]
        }
      },
      {
        "id": "2",
        "name": "Q2 2024",
        "expression": {
          "question": "waves",
          "datapoints": [
            "q2_2024"
          ]
        }
      },
      {
        "id": "3",
        "name": "Q3 2024",
        "expression": {
          "question": "waves",
          "datapoints": [
            "q3_2024"
          ]
        }
      },
      {
        "id": "4",
        "name": "Q4 2024",
        "expression": {
          "question": "waves",
          "datapoints": [
            "q4_2024"
          ]
        }
      }
    ]
  }
  ```

  ```python Python Script theme={null}
  import requests
  import json
  import pandas as pd

  with open('req.json', 'r') as file:
      request_json = json.load(file)

  url = 'https://api.globalwebindex.com/v2/query/crosstab'
  headers = {"Authorization": "Bearer YOUR_API_KEY"}

  response = requests.post(url, json=request_json, headers=headers)
  response.raise_for_status()

  # Process the response (JSON Lines format)
  results = []
  for line in response.iter_lines():
      if line:
          obj = json.loads(line)
          if obj.get('row_index') and obj.get('column_index'):  # Check for non-zero index values
              row_name = obj['row']['name']
              column_name = obj['column']['name']
              percentage = obj['intersect']['percentage']
              results.append((row_name, column_name, percentage))

  # Convert the results to a DataFrame and pivot the table for readability
  df = pd.DataFrame(results, columns=['Row Name', 'Column Name', 'Percentage'])
  pivot_table = df.pivot(index='Row Name', columns='Column Name', values='Percentage').reset_index()
  print(pivot_table.to_string(index=False))
  ```
</RequestExample>

<ResponseExample>
  ```json theme={null}
  {
    "row": {
      "id": "1",
      "name": "fb_boomers",
      "expression": {
        "and": [
          {
            "question": "q42011a",
            "options": [
              "q42011a_3"
            ],
            "suffixes": [
              1,
              2
            ]
          },
          {
            "question": "q3new",
            "options": [
              "q3new_1"
            ]
          }
        ]
      }
    },
    "row_index": 1,
    "column": {
      "id": "1",
      "name": "Q1 2024",
      "expression": {
        "question": "waves",
        "options": [
          "q1_2024"
        ]
      }
    },
    "column_index": 1,
    "audiences": {
      "column": {
        "audience": "1",
        "size": 257758496,
        "sample": 24922,
        "percentage": 100,
        "intersect_percentage": 100
      },
      "row": {
        "audience": "1",
        "size": 36643034,
        "sample": 3728,
        "percentage": 14.2,
        "intersect_percentage": 14.2
      }
    },
    "base": {
      "size": 257758496,
      "sample": 24922
    },
    "intersect": {
      "size": 36643034,
      "sample": 3728,
      "percentage": 14.2,
      "index": 100
    }
  }
  {}
  {}
  ...
  ```

  ```plaintext Script Result theme={null}
       Row Name  Q1 2024  Q2 2024  Q3 2024  Q4 2024
     fb_boomers     14.2     14.8     15.0     15.5
  fb_millenials     16.5     15.9     15.5     15.1
  ```
</ResponseExample>
