Array calculation in Tableau, maxif routine

2019-06-12 20:55发布

I'm fairly new to Tableau, and I'm struggling in building some routines that could be easily implemented in Excel (though it would take forever for big sets of data).

So here is the deal, consider a dataset with the following fields:

int [id_order] -> id of the sales order (deepest level, there are only unique entries of id_order) int [id_client] -> as I want to know who bought it date [purchase_date] -> when the customer bought the product

What I want to know is, for each order, when was the last time (if ever) the client has bought something. In order words, what is the highest purchase_date for that user that is smaller than current purchase_date.

In excel, approach is simple (but again, not efficient) {=max(if(id_client=B1,if(purchase_order

Is there a way to do this kind of calculation in Tableau?

2条回答
等我变得足够好
2楼-- · 2019-06-12 21:26

You can do this in Tableau using table calculations. They take a little time to understand how to use well, but are very powerful and flexible. I posted a sample Tableau workbook for a similar question in an answer for SO question Find first time a condition is met

Your situation is similar, but with the extra complication that you want to repeat the analysis for each client id, so you might want to try a recursive approach using the Previous_Value() function instead of the approach used in that example - though I'm not certain that previous_value() will fit your situation.

Still, it might be helpful to download the example workbook I mentioned to get an idea how table calculations can address similar problems.

查看更多
Explosion°爆炸
3楼-- · 2019-06-12 21:29

Just to register the solution, in case someone has the same doubt.

So, basically the solution I found use table calculation, which is not calculated until it's called on a sheet (and is only calculated on the context of the sheet). That's a little bit limiting, so what I do is create a sheet with all the fields I need (+ what is necessary for the table calculation) then export the data (to mdb) and connect to this new file.

So, for my example, the right table calculation is (let's name it last_order_date):

LOOKUP(MAX([purchase_date]),-1)

Explanations. The MAX() is necessary because Lookup (and all table calculations) does not work with data directly, only with aggregations. You can use sum, avg, max, attr, whatever suits you. As in my case there will be only 1 correspondence, any function will do just fine and return the same value.

The -1 indicates that I'm looking for the element immediately before the current entry (of the table, as you define it). If it were FIRST(), it would go for the first entry of the table, and LAST() would go for the last.

Now, I have to put it on a sheet. So I'll bring the fields id_client, id_order, purchase_date and last_order_date.

Then I have to define the parameters of my table calculation last_order_date (Edit Table Calculation). I'll go to Compute using and choose advanced. Now I'll do Partitioning: id_client, and addressing all the rest. What will that do? This mean Tableau will create temporary tables for each id_client, and table calculations will use those tables as parameter.

Additionally, I will Sort by field purchase_date, Max (again the aggregation issue) and ascending, to guarantee my entries are in chronological order.

Now, what will it do? For each entry it will access the table of the id_client, and check what was the purchase_date that is immediately before the current entry (that is being assessed), exactly what I need.

To avoid spending Tableau processing in Visualization, I often put all the fields in details (and leave nothing on screen), use Bar chart (it's good because it allows me to see the data). Then I export it to mdb, then connect to it again. Unfortunately Tableau doesn't directly export to tde.

查看更多
登录 后发表回答