ALLSELECTED 函數 (DAX)

從目前查詢的資料行和資料列中移除內容篩選,同時保留所有其他內容篩選或明確篩選。

ALLSELECTED 函數會取得代表查詢中所有資料列和資料行的內容,同時保留資料列和資料行篩選以外的明確篩選和內容。 這個函數可用於取得查詢的視覺化總計。

語法

ALLSELECTED([<tableName> | <columnName>])

參數

  • tableName
    使用標準 DAX 語法之現有資料表的名稱。 這個參數不能是運算式。 這個參數是選擇性的。

  • columnName
    使用標準 DAX 語法之現有資料行的名稱 (通常是完整名稱)。 它不能是運算式。 這個參數是選擇性的。

傳回值

不含任何資料行和資料列篩選的查詢內容。

備註

  • 這個函數接受單一引數或沒有引數。 如果存在單一引數,此引數就是 tableName 或 columnName。

  • 這個函數與 ALL() 不同,因為它會保留在查詢中明確設定的所有篩選,而且它會保留資料列和資料行篩選以外的所有內容篩選。

範例

下列範例說明如何使用 DAX 運算式,在資料表報表中產生不同層級的視覺化總計。 在報表中,已將之前的兩個篩選套用到 Reseller Sales 資料,一個篩選 Sales Territory Group = Europe,另一個篩選 Promotion Type = Volume Discount。 一旦套用篩選之後,就可以針對整份報表、所有年份或是所有產品類別目錄來計算視覺化總計。 此外,為了說明,也會取得 All Reseller Sales 的總計,以移除報表中的所有篩選。 使用建立具有視覺化總計之資料表所需的所有資訊,評估資料表中的以下 DAX 運算式結果。

define
measure 'Reseller Sales'[Reseller Sales Amount]=sum('Reseller Sales'[Sales Amount])
measure 'Reseller Sales'[Reseller Grand Total]=calculate(sum('Reseller Sales'[Sales Amount]), ALL('Reseller Sales'))
measure 'Reseller Sales'[Reseller Visual Total]=calculate(sum('Reseller Sales'[Sales Amount]), ALLSELECTED())
measure 'Reseller Sales'[Reseller Visual Total for All of Calendar Year]=calculate(sum('Reseller Sales'[Sales Amount]), ALLSELECTED('Date'[Calendar Year]))
measure 'Reseller Sales'[Reseller Visual Total for All of Product Category Name]=calculate(sum('Reseller Sales'[Sales Amount]), ALLSELECTED('Product Category'[Product Category Name]))
evaluate
CalculateTable(
    //CT table expression
    summarize( 
//summarize table expression
crossjoin(distinct('Product Category'[Product Category Name]), distinct('Date'[Calendar Year]))
//First Group by expression
, 'Product Category'[Product Category Name]
//Second Group by expression
, 'Date'[Calendar Year]
//Summary expressions
, "Reseller Sales Amount", [Reseller Sales Amount]
, "Reseller Grand Total", [Reseller Grand Total]
, "Reseller Visual Total", [Reseller Visual Total]
, "Reseller Visual Total for All of Calendar Year", [Reseller Visual Total for All of Calendar Year]
, "Reseller Visual Total for All of Product Category Name", [Reseller Visual Total for All of Product Category Name]
)
//CT filters
, 'Sales Territory'[Sales Territory Group]="Europe", 'Promotion'[Promotion Type]="Volume Discount"
)
order by [Product Category Name], [Calendar Year]

針對 AdventureWorks DW Tabular Model 2012 在 SQL Server Management Studio 中執行上述運算式之後,您會取得以下結果:

[Product Category Name]

[Calendar Year]

[Reseller Sales Amount]

[Reseller Grand Total]

[Reseller Visual Total]

[Reseller Visual Total for All of Calendar Year]

[Reseller Visual Total for All of Product Category Name]

Accessories

2000

80450596.9823

877006.7987

38786.018

Accessories

2001

80450596.9823

877006.7987

38786.018

Accessories

2002

625.7933

80450596.9823

877006.7987

38786.018

91495.3104

Accessories

2003

26037.3132

80450596.9823

877006.7987

38786.018

572927.0136

Accessories

2004

12122.9115

80450596.9823

877006.7987

38786.018

212584.4747

Accessories

2005

80450596.9823

877006.7987

38786.018

Accessories

2006

80450596.9823

877006.7987

38786.018

Bikes

2000

80450596.9823

877006.7987

689287.7939

Bikes

2001

80450596.9823

877006.7987

689287.7939

Bikes

2002

73778.938

80450596.9823

877006.7987

689287.7939

91495.3104

Bikes

2003

439771.4136

80450596.9823

877006.7987

689287.7939

572927.0136

Bikes

2004

175737.4423

80450596.9823

877006.7987

689287.7939

212584.4747

Bikes

2005

80450596.9823

877006.7987

689287.7939

Bikes

2006

80450596.9823

877006.7987

689287.7939

Clothing

2000

80450596.9823

877006.7987

95090.7757

Clothing

2001

80450596.9823

877006.7987

95090.7757

Clothing

2002

12132.4334

80450596.9823

877006.7987

95090.7757

91495.3104

Clothing

2003

58234.2214

80450596.9823

877006.7987

95090.7757

572927.0136

Clothing

2004

24724.1209

80450596.9823

877006.7987

95090.7757

212584.4747

Clothing

2005

80450596.9823

877006.7987

95090.7757

Clothing

2006

80450596.9823

877006.7987

95090.7757

Components

2000

80450596.9823

877006.7987

53842.2111

Components

2001

80450596.9823

877006.7987

53842.2111

Components

2002

4958.1457

80450596.9823

877006.7987

53842.2111

91495.3104

Components

2003

48884.0654

80450596.9823

877006.7987

53842.2111

572927.0136

Components

2004

80450596.9823

877006.7987

53842.2111

212584.4747

Components

2005

80450596.9823

877006.7987

53842.2111

Components

2006

80450596.9823

877006.7987

53842.2111

報表中的資料行如下:

  • Reseller Sales Amount
    年份和產品類別目錄的 Reseller Sales 實際值。 這個值會出現在報表中央的資料格內 (年份與類別目錄的交集處)。

  • Reseller Visual Total for All of Calendar Year
    某個產品類別目錄橫跨所有年份的總計值。 這個值會針對特定的產品類別目錄以及橫跨報表的所有年份,出現在資料行或資料列的結尾。

  • Reseller Visual Total for All of Product Category Name
    所有產品類別目錄在某一年份的總計值。 這個值會針對特定的年份以及橫跨報表的所有產品類別目錄,出現在資料行或資料列的結尾。

  • Reseller Visual Total
    所有年份和產品類別目錄的總計值。 這個值通常會出現在資料表的最右下角。

  • Reseller Grand Total
    這是所有轉售商銷售額的總計 (在套用任何篩選之前);您應該注意到與 [Reseller Visual Total] 之間的差異。 您記得此報表包含兩個篩選,一個篩選 Product Category Group,另一個則篩選 Promotion Type。

下列範例顯示不含任何引數之 ALLSELECTED() 的使用方式,以便示範如何針對已經使用水平和垂直交叉分析篩選器篩選之資料表中顯示的總計值計算比率。 此範例會使用 SQL Server 2012、PowerPivot for Excel 和 PowerPivot 範例資料 (DAX_AdventureWorks)

  • 在 PowerPivot 欄位清單中,將 ResellerSales[SalesAmount_USD] 資料行拖曳至 [值] 區域。

  • 將 Promotion[PromotionType] 拖曳至 [垂直交叉分析篩選器] 區域。 選取 Discontinued Product、Excess Inventory 和 Seasonal Discount 的交叉分析篩選器。

  • 將 SalesTerritory[SalesTerritoryGroup] 拖曳至 [水平交叉分析篩選器] 區域。 選取 Europe 的交叉分析篩選器。

  • Sum Of SalesAmount_USD 的值應該是 $19,614.37。

  • 將 ProductCategory[ProductCategoryName] 拖曳至 [資料列標籤] 區域,而將 DateTime[CalendarYear] 拖曳至 [資料行標籤] 區域。

    此時,資料表看起來應該像這樣:

     

    SalesTerritoryGroup

     

     

     

     

     

     

    Europe

    North America

     

     

     

     

     

    Pacific

     

     

     

     

     

     

     

     

     

     

     

    PromotionType

     

     

     

     

     

     

    Discontinued Product

     

    Sum of SalesAmount_USD

    資料行標籤

     

     

     

    Excess Inventory

     

    資料列標籤

    2006

    2007

    2008

    Grand Total

    New Product

     

    Accessories

    $1,111.22

    $3,414.43

     

    $4,525.66

    No Discount

     

    Bikes

    $8,834.94

    $6,253.78

    $15,088.72

    Seasonal Discount

     

    Grand Total

    $9,946.16

    $3,414.43

    $6,253.78

    $19,614.37

    Volume Discount

     

     

     

     

     

     

  • 使用掌上型計算機,確認金額 $3,414.43 (2007 年的 Accessories) 代表 $19,614.37 的 17.41%。

  • 使用掌上型計算機,確認金額 $6,253.78 (2008 年的 Bikes) 代表 $19,614.37 的 31.88%。

  • 使用掌上型計算機,確認金額 $15,088.72 (Bikes Grand Total) 代表 $19,614.37 的 76.93%。

  • [值] 區域中移除 ResellerSales[SalesAmount_USD]。

  • 使用下列公式 (將結果格式化為百分比),在 ResellerSales 資料表中建立名為 Reseller Sales Ratio 的量值:

=SUM(ResellerSales_USD[SalesAmount_USD])/ CALCULATE(SUM(ResellerSales_USD[SalesAmount_USD]), ALLSELECTED())

此時,資料表看起來應該像這樣:

SalesTerritoryGroup

Europe

North America

Pacific

PromotionType

Discontinued Product

Reseller Sales Ratio

資料行標籤

Excess Inventory

資料列標籤

2006

2007

2008

Grand Total

New Product

Accessories

5.67 %

17.41 %

23.07 %

No Discount

Bikes

45.04 %

31.88 %

76.93 %

Seasonal Discount

Grand Total

50.71 %

17.41 %

31.88 %

100.00 %

Volume Discount

  • 比較掌上型計算機的結果與 Reseller Sales Ratio 所提供的結果,而且結果應該相符。您的分母值會固定為資料表底部的值。

  • 因為分母中的 CALCULATE 公式會使用 ALLSELECTED 函數,所以分母代表在套用垂直、水平和頁面交叉分析篩選器之後但套用資料列和資料行交叉分析篩選器之前,銷售量的總計 (位於資料表底部)。

    注意:如果您的運算式具有明確篩選,這些篩選也會套用至運算式。