相关文章推荐
玩篮球的佛珠
·
闻学展堂丨画说君子:管道升《烟雨丛竹图》 ...
·
3 月前
·
谦逊的针织衫
·
外国政党政要和组织祝贺中共十九大召开_ ...
·
1 年前
·
神勇威武的斑马
·
豆瓣酱能做的菜有哪些种类? - 知乎
·
1 年前
·
玩滑板的黄花菜
·
人间烟火谁作词作曲- 抖音
·
1 年前
·
心软的饺子
·
王家卫电影《春光乍泄》经典台词截图-公众号: ...
·
1 年前
·
Code
›
逻辑函数- Tableau
tableau
逻辑表达式
逻辑函数
https://help.tableau.com/current/pro/desktop/zh-cn/functions_functions_logical.htm
玩足球的灯泡
1 年前
</noscript><div id="app" class="wrapper"><header id="tableau-help-article-header" class="container--full-width quick-help-header"><div class="container--centered"><div class="header__back-button"><back-button title="Go back"/></div><div class="header__mobile-menu quick-help-hidden"><menu-tree-toggle/></div><div class="header__logo quick-help-hidden"><a href="https://www.tableau.com/zh-cn/"><img src="./Resources/tableau-logo.png" class="header__logo__img" alt="Tableau"/></a></div><div class="header__search"><search-header-help placeholder="搜索"/></div><div class="header__home-button"><home-button title="Go home"/></div></div></header><div class="container--navigation-top quick-help-hidden content-only-hidden"><div id="help-subheader" class="subheader print-hidden"><div class="container--centered"><h4 class="heading--subheader">Tableau Desktop 和 Web 制作帮助</h4></div></div><div class="container--top-links"><div class="container--centered container--breadcrumbs"><div><breadcrumb-links-help/></div></div><div id="help-container-menu-headings" class="container--menu-headings"><nav class="nav-medium-screen"><menu-heading-links-static-help menu-title="本文内容" :disabled="false" :headings="pageHeadings"/></nav></div></div></div><div class="section--main container--full-width"><div class="container--centered"><nav class="nav-side nav-side--left" role="navigation"><menu-tree-help menu-title="内容"/></nav><article role="main"><h1 id="contentH1"/><div class="caption article__tags content-only-hidden quick-help-hidden"><span class="article__tags--applies-to">适用于: Tableau Cloud, Tableau Desktop, Tableau Server</span><br/><span class="article__tags--role"> </span></div><div id="content-body"> <div role="main" id="mc-main-content"><p>本文介绍 Tableau 中的逻辑函数及其用途。它还使用示例来演示如何创建逻辑计算。</p><h2 is="heading-item" :level="2" id="为什么使用逻辑计算"><a name="Why"/>为什么使用逻辑计算</h2><p>逻辑计算允许您确定某个特定条件为真还是假(布尔逻辑)。例如,您可能希望快速确定您分销商品的每个国家/地区的销售额是高于还是低于特定阈值。</p><p>逻辑计算可能如下所示:</p><p class="example"><code>SUM(Sales) > 1,000,000 </code></p><h2 is="heading-item" :level="2" id="tableau-中的可用逻辑函数:"><a name="Logical"/>Tableau 中的可用逻辑函数:</h2><table style="width: 100%;mc-table-style: url('Resources/TableStyles/Basic-Border-Light.css');" class="TableStyle-Basic-Border" cellspacing="0"><colgroup><col class="TableStyle-Basic-Border-Column-Column1" style="width: 15%;"/><col class="TableStyle-Basic-Border-Column-Column1" style="width: 20%;"/><col class="TableStyle-Basic-Border-Column-Column1"/></colgroup><tbody><tr class="TableStyle-Basic-Border-Body-Body1"><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><h4>函数</h4></td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><h4>语法</h4></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><h4>描述</h4></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><span class="uicontrol">IN</span></td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code><expr1> IN <expr2> </code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>如果 <code><expr1></code> 中的任何值与 <code><expr2></code> 中的任何值匹配,则返回 TRUE。 </p><p><code><expr2></code> 中的值可以是集、文本值列表或合并字段。</p><p>示例:</p><p class="note"><code>SUM([Cost]) IN (1000, 15, 200)</code></p><p class="note"><code>[Field] IN [SET]</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1"><p>AND</p></td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IF <expr1> AND <expr2> THEN <then> END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>对两个表达式执行逻辑合取运算。</p><p>示例:</p><p class="example"><code>IF (ATTR([Market]) = "New Business" AND SUM([Sales]) > [Emerging Threshold] )THEN "Well Performing"</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">CASE</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>CASE <expression> WHEN <value1> THEN <return1> WHEN <value2> THEN <return2> ... ELSE <default return> END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>执行逻辑测试并返回相应的值。CASE 函数可评估 <code>expression</code>,并将其与一系列值(<code>value1</code>、 <code>value2</code> 等)比较,然后返回结果。遇到一个与 <code>expression</code> 匹配的值时,CASE 返回相应的返回值。如果未找到匹配值,则使用默认返回表达式。如果不存在默认返回表达式并且没有任何值匹配,则会返回 Null。</p><p>CASE 也支持 WHEN IN 构造,例如</p><p class="note"><code>CASE <expression> WHEN IN <set1> THEN <return1> WHEN IN <combinedfield> THEN <return2> ... ELSE <default> END</code></p><p>WHEN IN 中的值可以是集、文本值列表或合并字段。</p><p><b>其他注意事项</b></p><ul><li value="1"><i>CASE 对比 IF</i>:CASE 比 IIF 或 IF THEN ELSE 更易于使用。通常,IF 函数执行一系列任意测试, CASE 函数搜索与表达式的匹配值。但 CASE 函数都可以重写为 IF 函数,不过 CASE 函数一般更加简明。</li><li value="2"><i>CASE 对比组</i>:很多时候可以使用组获得与复杂 CASE 函数相同的结果。您可能想看看哪个更适合您的场景。</li></ul><p>示例:</p><p class="example"><code>CASE [Region] WHEN 'West' THEN 1 WHEN 'East' THEN 2 ELSE 3 END</code></p><p class="example"><code>CASE LEFT(DATENAME('weekday',[Order Date]),3) WHEN 'Sun' THEN 0 WHEN 'Mon' THEN 1 WHEN 'Tue' THEN 2 WHEN 'Wed' THEN 3 WHEN 'Thu' THEN 4 WHEN 'Fri' THEN 5 WHEN 'Sat' THEN 6 END</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">ELSE</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IF <expr> THEN <then> ELSE <else> END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>测试一系列表达式,同时为第一个为 true 的 <expr> 返回 <then> 值。</p><p>示例:</p><p class="example"><code>If [Profit] > 0 THEN 'Profitable' ELSE 'Loss' END</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">ELSEIF</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IF <expr> THEN <then> [ELSEIF <expr2> THEN <then2>...] [ELSE <else>] END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>测试一系列表达式,同时为第一个为 true 的 <expr> 返回 <then> 值。</p><p>示例:</p><p class="example"><code>IF [Profit] > 0 THEN 'Profitable' ELSEIF [Profit] = 0 THEN 'Breakeven' ELSE 'Loss' END</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">END</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IF <expr> THEN <then> [ELSEIF <expr2> THEN <then2>...] [ELSE <else>] END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>测试一系列表达式,同时为第一个为 true 的 <expr> 返回 <then> 值。必须放在表达式的结尾。</p><p>示例:</p><p class="example"><code>IF [Profit] > 0 THEN 'Profitable' ELSEIF [Profit] = 0 THEN 'Breakeven' ELSE 'Loss' END</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">IF</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IF <expr> THEN <then> [ELSEIF <expr2> THEN <then2>...] [ELSE <else>] END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>测试一系列表达式,同时为第一个为 true 的 <expr> 返回 <then> 值。</p><p>示例:</p><p class="example"><code>IF [Profit] > 0 THEN 'Profitable' ELSEIF [Profit] = 0 THEN 'Breakeven' ELSE 'Loss' END</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">IFNULL</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IFNULL(expr1, expr2)</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>如果 <expr1> 不为 null,则返回该表达式,否则返回 <expr2>。</p><p>示例:</p><p class="example"><code>IFNULL([Profit], 0)</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">IIF</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IIF(test, then, else, [unknown])</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>检查某个条件是否得到满足,如果为 TRUE 则返回一个值,如果为 FALSE 则返回另一个值,如果未知,则返回可选的第三个值或 NULL。</p><p>示例:</p><p class="example"><code>IIF([Profit] > 0, 'Profit', 'Loss')</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">ISDATE</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>ISDATE(string)</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>如果给定字符串为有效日期,则返回 true。</p><p>示例:</p><p class="example"><code>ISDATE("2004-04-15") = True</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">ISNULL</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>ISNULL(expression)</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>如果表达式为 NULL(未包含有效数据),则返回 true。</p><p>示例:</p><p class="example"><code>ISNULL([Profit])</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">MAX</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>MAX(expression)</code> 或 <code>Max(expr1, expr2)</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>返回单一表达式所有记录中的最大值,或返回每条记录两个表达式中的最大值。</p><p>示例:</p><p class="example"><code>MAX([Sales])</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">MIN</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>MIN(expression)</code> 或 <code>MIN(expr1, expr2)</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>返回单一表达式所有记录中的最小值,或返回每条记录两个表达式中的最小值。</p><p>示例:</p><p class="example"><code>MIN([Profit])</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">NOT</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IF NOT <expr> THEN <then> END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>对一个表达式执行逻辑非运算。</p><p>示例:</p><p class="example"><code>IF NOT [Profit] > 0 THEN "Unprofitable" END</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">或者</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IF <expr1> OR <expr2> THEN <then> END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>对两个表达式执行逻辑析取操作。</p><p>示例:</p><p class="example"><code>IF [Profit] < 0 OR [Profit] = 0 THEN "Needs Improvement" END</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">THEN</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>IF <expre> THEN <then> [ELSEIF ,expr2> THEN <then2>...] [ELSE <else>] END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>测试一系列表达式,同时为第一个为 true 的 <expr> 返回 <then> 值。</p><p>示例:</p><p class="example"><code>IF [Profit] > 0 THEN 'Profitable' ELSEIF [Profit] = 0 THEN 'Break even' ELSE 'unprofitable' END</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyE-Column1-Body1">WHEN</td><td class="TableStyle-Basic-Border-BodyE-Column1-Body1"><code>CASE <expr> WHEN <Value1> THEN <return1> ... [ELSE <else>] END</code></td><td class="TableStyle-Basic-Border-BodyD-Column1-Body1"><p>查找第一个与 <expr> 匹配的 <value>,并返回对应的 <return>。</p><p>示例:</p><p class="example"><code>CASE [RomanNumberal] WHEN 'I' THEN 1 WHEN 'II' THEN 2 ELSE 3 END</code></p></td></tr><tr class="TableStyle-Basic-Border-Body-Body1"><td style="font-weight: bold;" class="TableStyle-Basic-Border-BodyB-Column1-Body1">ZN</td><td class="TableStyle-Basic-Border-BodyB-Column1-Body1"><code>ZN(expression)</code></td><td class="TableStyle-Basic-Border-BodyA-Column1-Body1"><p>如果 <expression> 不为 null,则返回该表达式,否则返回零。</p><p>示例:</p><p class="example"><code>ZN([Profit])</code></p></td></tr></tbody></table><br/><p><b>注意</b>:其中一些实际上是逻辑运算符,显示为黑色,而不是蓝色。有关详细信息,请参见<a href="functions_operators.htm#Operators" class="MCXref xref">运算符语法</a>。</p><h2 is="heading-item" :level="2" id="创建逻辑计算"><a name="Create"/>创建逻辑计算</h2><p>按照下面的步骤进行操作以了解如何创建逻辑计算。</p><ol><li value="1"><p>在 Tableau Desktop 中,连接到 Tableau 附带的<b>“Sample - Superstore”</b>已保存数据源。</p></li><li value="2"><p>导航到工作表。</p></li><li value="3"><p>从“<b>数据</b>”窗格中,将<span class="uicontrol">“State”(州/省/市/自治区)</span>拖到<span class="uicontrol">“行”</span>功能区。</p></li><li value="4"><p>从“<b>数据</b>”窗格中,将<span class="uicontrol">“Category”(类别)</span>拖到<span class="uicontrol">“行”</span>功能区,并将其放在“State”(州/省/市/自治区)的右侧。</p></li><li value="5"><p>从“<b>数据</b>”窗格中,将<span class="uicontrol">“Sales”(销售额)</span>拖到<span class="uicontrol">“列”</span>功能区。</p></li><li value="6"><p>选择<span class="uicontrol">“分析”</span>><span class="uicontrol">“创建计算字段”</span>。</p></li><li value="7"><p>在打开的计算编辑器中,执行以下操作:</p></li></ol><ul><li value="1"><p>将计算字段命名为“KPI”。</p></li><li value="2"><p>输入以下公式:</p><p class="example"><code>SUM([Profit]) > 0</code></p><p>此计算将快速检查某个成员是否大于零。如果大于零,则返回 true;否则返回 false。</p></li><li value="3"><p>完成后,单击<b>“确定”</b>。</p><p>新计算字段会显示在“数据”窗格中的“度量”下。就像其他字段一样,您可以在一个或多个可视化项中使用该字段。</p></li></ul><li value="8"><p>从“数据”窗格中,将<b>“<b>KPI</b>”</b>拖到“标记”卡上的<b>“颜色”</b>。</p><p>您现在可以看到每个州/省/市/自治区的哪些类别在亏损。</p><p><img src="Img/logicalfunctions1.png" alt=""/></p></li><h2 is="heading-item" :level="2" id="另请参见">另请参见</h2><p><a href="kpi.htm" class="MCXref xref">显示关键进度指标</a></p><p><a href="https://www.tableau.com/zh-cn/learn/tutorials/on-demand/logic-calculations?product=tableau_desktop&version=10.4&topic=calculations">逻辑计算免费视频教程</a></p><p><a href="functions.htm" class="MCXref xref">Tableau 中的函数</a></p><p><a href="functions_all_categories.htm" class="MCXref xref">Tableau 函数(按类别)</a></p><p><a href="functions_all_alphabetical.htm" class="MCXref xref">Tableau 函数(按字母顺序)</a></p></div></div><div class="article__footer--back-to-top text--centered print-hidden quick-help-hidden"><a href="#" class="text--caps"><span class="icon--med icon--arrow-up"/> 回到顶部</a></div><feedback-form :disabled="false" class="print-hidden content-only-hidden" helpful-question="此内容是否有帮助?" helpful-yes="是" helpful-no="否" comment-question="我们重视您的反馈。在此处添加注释。" submit-option="发送" cancel-option="取消" loading-text="正在发送..."><span slot="submittedMessage">感谢您的反馈!</span></feedback-form></article><nav class="nav-side nav-side--right"><menu-heading-links-scrollable-help menu-title="本文内容" :disabled="false" :headings="pageHeadings"/></nav></div></div><feedback-button-floating tooltip="发送反馈" :disabled="false"/><footer class="container--full-width"><div class="container--centered"><div class="footer__links text--caps"><ul><li><a href="https://www.tableau.com/zh-cn/legal" class="">法律</a></li><li><a id="privacyLink" class="">隐私</a></li><li><a href="#" data-ignore-geolocation="true" class="optanon-toggle-display">Cookie Preferences</a></li></ul></div><div class="footer__copyright text--caps">© 2003-2022 Tableau Software LLC.保留所有权利</div></div></footer></div> </body>
推荐文章
玩篮球的佛珠
·
闻学展堂丨画说君子:管道升《烟雨丛竹图》 - 同济大学图书馆
3 月前
谦逊的针织衫
·
外国政党政要和组织祝贺中共十九大召开_ 最新报道_中国政府网
1 年前
神勇威武的斑马
·
豆瓣酱能做的菜有哪些种类? - 知乎
1 年前
玩滑板的黄花菜
·
人间烟火谁作词作曲- 抖音
1 年前
心软的饺子
·
王家卫电影《春光乍泄》经典台词截图-公众号:电影台词精选
1 年前