简体   繁体   中英

How to replicate running total table calculation using calculated field in Tableau?

I have two fields: date and new patients. I need to calculate the cumulative sum of patients. Using table calculation for running total does the job but prevents me from using forecast and cluster in analytics. Is there a way to create a calculated field to calculate sum of patients if date is less than date used in dimensions on the chart?

Do you want to keep the cumulative sum but not display the dates used to calculate that sum? If so check this post which contains an example how to filter dates but maintain the running sum in Tableau .

Here are two approaches, both of which involve editing the physical layer of your data source

  • a self join
  • a SQL windowing query (aka analytic query) by writing custom SQL

Andy Granowitz wrote a clear example showing both approaches in SQL. (I copied his SQL to the bottom of this answer in case that link ever disappears, but recommend reading his post for a better explanation.)

In this case, I think the self join is a bit easier as you don't have to write any SQL, but in more complex cases the windowing query approach would give you more flexibility. The windowing query will likely be more efficient if you have very large data sets. Note, some databases may not yet supporting windowing queries, but most do. If you are using a CSV or Excel file, you may have to convert it to a Hyper extract before you can use custom SQL.

To define a self join in Tableau, edit the data source, double click on your table to drop to the physical layer, and drag your table onto the canvas a second time. Then define your join criteria. Custom SQL is available for most databases, but perhaps not from text files. See the help.

In most cases, you are better off avoiding custom SQL and letting Tableau generate optimized SQL. Tableau is often described by power users as “ a SQL generator with a charting package attached ” to highlight the fact that it is very good at generating SQL. New users often insist on hand writing SQL because they are used to it which sacrifices some of the main benefits that Tableau brings. Tableau has grown to the point where writing custom SQL is rarely needed, - but there are still some important use cases. Taking advantage windowing queries is one good use case for custom SQL. Another is when you want to use a non-standard feature or function that your database provides (but in that case, first try using a RAW SQL function from Tableau - which lets you embed just a fragment of custom SQL)

Table calcs are in many ways inspired by SQL windowing queries, and are often quick and easy (but sometimes not so easy). Table calcs are calculated on the client side which can help or hurt performance depending on whether you've already fetched the data.

The hyper API supports some windowing queries now. Hopefully, someday they will be easily accessed from the Tableau without custom SQL.

————-

SQL for self-join approach

select 
  a.date,
  sum(b.sales) as cumulative_sales
from sales_table a 
join sales_table b on a.date >= b.date
group by a.date

SQL for windowing query approach

select
  date,
  sum(sales) over (order by date rows unbounded preceding) as cumulative_sales
from sales_table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM