简体   繁体   English

使用 Tableau 做 VLookup

[英]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.如果这很明显,Tableau 的新手非常抱歉,但我进行了一次狩猎,但找不到/理解任何东西。 So I have a cake shop and I want to know if people have purchased both éclairs and Eccles cakes in the same order所以我有一家蛋糕店,我想知道人们是否以相同的顺序购买了 éclairs 和 Eccles 蛋糕

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?作为参考,我有一个数据集(一个 csv 文件),所以我猜我不能对其运行 SQL 查询?

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.在工作表中,将Order IdItem添加到Rows部分。

  2. Duplicate your data source (right click > Duplicate).复制您的数据源(右键单击 > 复制)。 I'll refer to the original as A and the copy as B .我将原件称为A ,将副本称为B

  3. Unlink the Item field.取消链接Item字段。 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).保持Order Id字段链接(橙色)。

  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)留在同一张表上,单击数据源B并创建这两个计算字段: Cake_Flag: MAX(IF [Item] = 'Eccles cake' THEN 1 ELSE 0 END)

    Eclair_Flag: MAX(IF [Item] = 'éclair' 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通过选择它返回到数据源A并创建这个新的计算字段:é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. [Data Source B]应替换为B数据源的名称。

  6. Place the new calculated field éclair?放置新的计算字段é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.请参阅设置差异:在 Tableau Desktop 中为两个组查找不同的成员,以了解几种可能方法中的一种。 The crux is writing an aggregated calculated field that calculates whether an order id contains both items of interest.关键是编写一个聚合计算字段,用于计算订单 ID 是否包含两个感兴趣的项目。

You can use that calculation in a set or directly in a view where order id is a dimension.您可以在集合中或直接在订单 ID 为维度的视图中使用该计算。

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.对于具有至少一个 eclair 项目和至少一个 Eccles 蛋糕项目的订单,此(聚合)计算返回 true,对于其他订单返回 false。

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.为此,您需要订单 ID 作为视图中的维度,而不是项目,以便计算一次应用于订单中的所有项目。 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:如果您确实需要您指定的确切输出形式,则将此计算转换为 LOD 计算,例如:

{ 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.然后,您在每个订单 ID/项目旁边显示一列,以指示该订单是否同时包含泡芙和蛋糕。 (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. (这也不完全是您显示为所需输出的内容)或使用此新字段与当前项目一起定义最终计算,当且仅当项目是蛋糕并且订单包含两者时才为 1。

This evaluates to true and false.这评估为真和假。 I presume you can convert to 1 and 0 if needed.如果需要,我认为您可以转换为 1 和 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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