简体   繁体   中英

Using Tableau to do VLookup

New to Tableau so sorry if this is obvious, but I had a hunt and couldn't find/understand anything. So I have a cake shop and I want to know if people have purchased both éclairs and Eccles cakes in the same order

ORDER ID     item 
0001         éclair
0001         iced bun
0001         Eccles cake
0002         éclair 
0002         iced bun
0003         éclair          
0003         Eccles cake

What I want as an output is the following:

ORDER ID     item          éclair?
0001         éclair          0
0001         iced bun        0
0001         Eccles cake     1
0002         éclair          0
0002         iced bun        0
0003         éclair          0
0003         Eccles cake     1

I'm thinking it requires something like a lookup, or a helper column?!

For reference, I've got a single data set (a csv file) so I'm guessing I can't run a SQL query on it?

Here is how I was able to do this - there may be a better way that I am not familiar with. Nonetheless...


Steps:

  1. In a sheet, add Order Id and Item to the Rows section.

  2. Duplicate your data source (right click > Duplicate). I'll refer to the original as A and the copy as B .

  3. Unlink the Item field. This is done by clicking the orange chain link icon to the right of the field name. A gray link is what you want. Leave the Order Id field linked (orange).

  4. Staying on the same sheet, click on data source B and create these two calculated fields: Cake_Flag: MAX(IF [Item] = 'Eccles cake' THEN 1 ELSE 0 END)

    Eclair_Flag: MAX(IF [Item] = 'éclair' THEN 1 ELSE 0 END)

  5. Return to data source A by selecting it and create this new calculated field: éclair?: IF ATTR([Item]) = 'Eccles cake' AND [Data Source B].[Eclair Flag] = 1 AND [Data Source B].[Cake Flag] = 1 THEN 1 ELSE 0 END

    [Data Source B] should be replaced with whatever the name of your B data source is.

  6. Place the new calculated field éclair? in the Text card.


Result:

在此处输入图像描述

See Set difference: find distinct members for two groups in Tableau Desktop for a start one of several possible approaches. The crux is writing an aggregated calculated field that calculates whether an order id contains both items of interest.

You can use that calculation in a set or directly in a view where order id is a dimension.

An example would be:

count(if item = "eclair" then 1 end) > 0 and count(if item = "Eccles cake" then 1 end) > 0

This (aggregated) calculation returns true for orders that have at least one eclair item and at least one Eccles cake item, false for other orders.

For this to work, you need Order ID as a dimension in your view, but not item, so that the calculation is applied to all the items in an order at one time. That answers the question you posed, but displays one row per order instead of one row per order/item.

If you really need the exact output form you specified, then you turn this calculation into an LOD calc, such as:

{ FIXED [ORDER ID] : count(if item = "eclair" then 1 end) > 0 and count(if item = "Eccles cake" > 0 then 1 end) }

Then you display a column next to each order id/item to indicate whether or not the order contained both an eclair and a cake. (Which is also not exactly what you show as desired output) or use this new field along with the the current item to define a final calculation that is 1 if and only if the item is cake and the order contains both.

This evaluates to true and false. I presume you can convert to 1 and 0 if needed.

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