知識庫

Showing Different Calculation for Sub-totals and Grand Totals


發佈時間 : 18 Sep 2019
上次修改日期 : 15 Sep 2023

問題

How to show a different calculation for Subtotals and Grand Totals. For example, showing SUM([Sales]) in cell, but SUM([Profit]) in Sub-totals & SUM([Quantity]) in Grand Totals.

環境

Tableau Desktop

答案

Follow along with the attached packaged workbook to review the steps below.  The workbook can be found to the right of this text in the "Attachments" section.


Step 1 - Create a calculated field

  1. Select Analysis > Create calculated field
  2. Name the calculated field "New Totals"
  3. Enter the following formula and click OK
    IF WINDOW_COUNT(ATTR([Category]))=0 THEN SUM([Quantity])
    ELSEIF WINDOW_COUNT(ATTR([Category]))=3 THEN SUM([Profit])
    ELSE SUM([Sales])
    END

Build the view

  1. Drag Category and Sub-Category to Rows
  2. Drag the new calculated field to Text
  3. Click Analysis > TotalsShow Column Grand Totals
  4. Click Analysis > TotalsAdd All Subtotals
Important: The result of window_count depends on the data and the view, and such results will be effected by filters. We recommend using this workaround after sufficient testing.
這篇文章是否解決了問題?