Wednesday, June 28, 2023

Explain the Table Calculation Function ' PREVIOUS_VALUE()' in Tableau with Example?

The PREVIOUS_VALUE() function is a table calculation function in Tableau that allows you to access the value of a field or calculation from the previous row within a specified context or partition. It is useful for comparing values between consecutive rows or performing calculations based on the previous row's value.

The syntax for the PREVIOUS_VALUE() function in Tableau is as follows:

PREVIOUS_VALUE(expression, [reset])

  • expression: The field or calculation for which you want to access the previous value.
  • reset (optional): Specifies the condition or field that determines when the calculation should be reset. If not specified, the calculation continues from the previous row.

Here's an example to demonstrate how to use the PREVIOUS_VALUE() function in Tableau:

Suppose you have a dataset that contains information about daily stock prices for different companies. You want to calculate the daily percentage change in stock prices compared to the previous day.

  1. Open Tableau and connect to your dataset.
  2. Drag the "Company" field to the Rows shelf to create a table view with companies.
  3. Drag the "Date" field to the Columns shelf to display dates horizontally.
  4. Drag the "Stock Price" field to the Marks card to apply the aggregation.
  5. Right-click on the "Stock Price" field in the Marks card and select "Add Table Calculation."
  6. In the calculation dialog box, choose "PREVIOUS_VALUE" from the list of table calculation functions.
  7. Specify the expression as the field or calculation for which you want to access the previous value (in this case, "Stock Price").
  8. Configure the specific settings for the PREVIOUS_VALUE function, such as the addressing and partitioning options.
  9. Click "OK" to apply the PREVIOUS_VALUE function to the stock prices.

Tableau will calculate the daily percentage change in stock prices compared to the previous day based on the applied table calculation. You can further customize the view, apply sorting, or add additional calculations based on the previous values.

The PREVIOUS_VALUE function allows you to access the value of a field or calculation from the previous row, making it useful for calculations that require comparing consecutive values or tracking changes over time.

By utilizing the PREVIOUS_VALUE function in Tableau, you can perform dynamic calculations and comparisons based on the values of the previous row within your dataset. 

No comments:

Post a Comment

If you have any doubts. Please let me know

How can you create an alias for a table in a SQL query?

In SQL, you can create an alias for a table in a query to give the table a temporary, alternative name that you can use in the query. Table ...