## 一般规则：避免在另一个计算中使用某个计算字段多次

### 示例

`RIGHT([Twitter Handle], LEN([Twitter Handle]) -1)`

`MID([Twitter Handle], 2)`

## 提示 1：将多个等式比较转换为 CASE 表达式或组

`IF [Person (calc)] = 'Henry Wilson'OR [Person (calc)] = 'Jane Johnson'OR [Person (calc)] = 'Michelle Kim'OR [Person (calc)] = 'Fred Suzuki'OR [Person (calc)] = 'Alan Wang'THEN 'Lead'ELSEIF [Person (calc)] = 'Susan Nguyen'OR [Person (calc)] = 'Laura Rodriguez'OR [Person (calc)] = 'Ashley Garcia'OR [Person (calc)] = 'Andrew Smith'OR [Person (calc)] = 'Adam Davis'THEN 'IC'END`

### 解决方案 1

`CASE [Person (calc)]WHEN 'Henry Wilson' THEN 'Lead'WHEN 'Jane Johnson' THEN 'Lead'WHEN 'Michelle Kim' THEN 'Lead'WHEN 'Fred Suzuki' THEN 'Lead'WHEN 'Alan Wang' THEN 'Lead'WHEN 'Susan Nguyen' THEN 'IC'WHEN 'Laura Rodriguez' THEN 'IC'WHEN 'Ashley Garcia' THEN 'IC'WHEN 'Andrew Smith' THEN 'IC'WHEN 'Adam Davis' THEN 'IC'END`

## 提示 2：将多个字符串计算转换为单一 REGEXP 表达式

### 示例 1：CONTAINS

`IF CONTAINS([Segment (calc)],'UNKNOWN')OR CONTAINS([Segment (calc)],'LEADER')OR CONTAINS([Segment (calc)],'ADVERTISING')OR CONTAINS([Segment (calc)],'CLOSED')OR CONTAINS([Segment (calc)],'COMPETITOR')OR CONTAINS([Segment (calc)],'REPEAT')THEN 'UNKNOWN'ELSE [Segment (calc)] END`

#### 解决方案

`IF REGEXP_MATCH([Segment (calc)], 'UNKNOWN|LEADER|ADVERTISING|CLOSED|COMPETITOR|REPEAT') THEN 'UNKNOWN'ELSE [Segment (calc)] END`

### 示例 2：STARTSWITH

`IF STARTSWITH([Segment (calc)],'UNKNOWN')OR STARTSWITH([Segment (calc)],'LEADER')OR STARTSWITH([Segment (calc)],'ADVERTISING')OR STARTSWITH([Segment (calc)],'CLOSED')OR STARTSWITH([Segment (calc)],'COMPETITOR')OR STARTSWITH([Segment (calc)],'REPEAT')THEN 'UNKNOWN'`

#### 解决方案

`IF REGEXP_MATCH([Segment (calc)], '^(UNKNOWN|LEADER|ADVERTISING|CLOSED|COMPETITOR|REPEAT)') THEN 'UNKNOWN'ELSE [Segment (calc)] END`

### 示例 3：ENDSWITH

`IF ENDSWITH([Segment (calc)],'UNKNOWN')OR ENDSWITH([Segment (calc)],'LEADER')OR ENDSWITH([Segment (calc)],'ADVERTISING')OR ENDSWITH([Segment (calc)],'CLOSED')OR ENDSWITH([Segment (calc)],'COMPETITOR')OR ENDSWITH([Segment (calc)],'REPEAT')THEN 'UNKNOWN'ELSE [Segment (calc)] END`

#### 解决方案

`IF REGEXP_MATCH([Segment (calc)], '(UNKNOWN|LEADER|ADVERTISING|CLOSED|COMPETITOR|REPEAT)\$') THEN 'UNKNOWN'ELSE [Segment (calc)] END`

## 提示 3：使用 REGEXP（而不是 LEFT、MID、RIGHT、FIND、LEN）操作字符串

### 示例 1

`IF (STARTSWITH([Server], "http://")) THEN MID([Server], Len("http://") + 1)ELSEIF(STARTSWITH([Server], "https://")) THEN MID([Server], Len("https://") + 1)ELSEIF(STARTSWITH([Server], "tcp:")) THEN MID([Server], Len("tcp:") + 1)ELSEIF(STARTSWITH([Server], "\\")) THEN MID([Server], Len("\\") + 1)ELSE [Server]END`

#### 解决方案

`REGEXP_REPLACE([Server], "^(http://|https://|tcp:|\\\\)", "")`

### 示例 2

`IF (FINDNTH([Server], ".", 2) > 0) THENMID([Server],FIND([Server], ".") + 1,FINDNTH([Server], ".", 2) - FINDNTH([Server], ".", 1) - 1)END`

#### 解决方案

`REGEXP_EXTRACT([Server], "\.([^\.]*)\.")`

## 提示 4：不要在计算中使用集

### 示例

`IF ISNULL([Customer Name]) OR [Top customers (set)] THEN [Segment] ELSE [Customer Name] END`

### 解决方案 1

`CASE [Customer Name]WHEN 'Henry Wilson' THEN TrueWHEN 'Jane Johnson' THEN TrueWHEN 'Michelle Kim' THEN TrueWHEN 'Fred Suzuki' THEN TrueWHEN 'Alan Wang' THEN TrueELSE FalseEND`

### 解决方案 2

`IF ISNULL([Customer Name]) OR [Top Customers(group)]='IN' THEN [Segment] ELSE [Customer Name] END`

## 提示 5：不要使用集对数据进行分组

`IF [Americas Set] THEN "Americas"ELSEIF [Africa Set] THEN "Africa"ELSEIF [Asia Set] THEN "Asia"ELSEIF [Europe Set] THEN "Europe"ELSEIF [Oceania Set] THEN "Oceania"ELSE "Unknown"END`

• 集并不始终是排他的。某些成员可能会出现在多个集中。例如，“俄罗斯”既可能放在“欧洲”集中，也可能放在“亚洲”集中。

• 集并不会始终转换为组。如果集是通过排除、条件或限制定义的，则可能很难甚至无法创建等效的组。

Tableau 中的计算类型

Tableau 中的函数