简体   繁体   中英

How do I count how many times a certain dimension is used TABLEAU/SQL

Im trying to count how many times my barcodes come up in multiple places. Im trying to bucket barcodes into two categories 1) barcodes that only come up in ONE business unit 2) barcodes that show up in multiple business units

在此处输入图像描述

I dont care about how many times they are shown at multiple business units, just simply if it does show up in more than one

SOS

Calculate a count of how many fields are not null.

SELECT *, Abs(NOT IsNull(CH101) + NOT IsNull(CH311) + etc) AS CntBU FROM table;

Otherwise, normalize data structure so an aggregate query can be used. UNION can rearrange data to normalized structure then use UNION query in an aggregate query.

SELECT BarCode, RecordType, ProductCo, CH101 AS Data, "CH101" AS BU FROM table
UNION SELECT BarCode, RecordType, ProductCo, CH311, "CH311" FROM table
etc

Limit of 50 SELECT lines in UNION.

One solution is to define a set based on the barcode (or mint if preferred) field. Right click on barcode (or mint) in the data pane sidebar and create set. Define membership in the set with the condition

countd([Business Unit]) > 1

Barcodes belong to the set if they appear with multiple business units. You can use the set on any shelf or calculation

For an even simpler approach to understand how COUNTD() works. In a new worksheet, put one discrete field on the Rows shelf, say Barcode, then put Business Unit on Columns. Then right click on Business Unit on the columns shelf, and convert it from a dimension to a measure using the COUNTD function.

You should then see a mark for each of your Barcodes showing how many distinct Business Units that Barcode appears with in the data. For the Barcodes where COUNTD([Business Unit]) = 1, those are the ones where they only appear in the data with one particular [Business Unit]

What the set does is distinguish the barcodes that appear with multiple Business Units from those that appear only with one. One way to think of the set is as a boolean function based on Barcodes.

You can use sets for all kinds of things in Tableau, well described in the help and training, but they are not essential for your problem (just convenient) The function that solves your problem is COUNTD(). Its worth the time to make sure you understand it well. In SQL, it is equivalent to "COUNT DISTINCT"

Last point, COUNTD() is useful and important, but it is more computationally intensive than other aggregation functions like COUNT() or SUM(). So if you have very large datasets and see performance problems, see if calls to COUNTD may be the culprit. You can then look at ways to shape the data differently to avoid the need for COUNTD(). Still, for most small or medium size datasets, the convenience of COUNTD() outweighs any performance costs

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