DAX VII: Packet aggregation function

DAX There are three functions for generating the aggregate data packet, these two functions have three common features: grouping columns and extended column.

  • Grouping columns is a column for grouping, only saved from the underlying table column, the column can be derived from the same grouping table may be derived from the associated column.
  • Column name and extension is constituted of expression, name is a string, expression is an expression containing a function of the polymerization.

Grouping columns in the column and expanded, these three functions are very unique approach.

A, SUMMARIZE

Table SUMMARIZE function interrelated in a specific field (column packet) or more fields, for packet aggregation. Since the packet is the only column by SUMMARIZE function, unique values ​​can be obtained two-dimensional table composed of a plurality of rows:

SUMMARIZE(<table>, <groupBy_columnName>[, <groupBy_columnName>]…[, <name>, <expression>]…) 

Parameter Notes:

  • table: Required parameter represents the main table, the table may be any return expression.
  • groupBy_columnName: an optional parameter indicating the packet column, which must be the parameter table column, or the associated column in the table. Grouping columns must be fully qualified column name, format table [column], grouping column can have 0, 1 or more.
  • name, expression: an optional parameter represents the custom summary row / column expansion, which always appear in pairs parameter, name is the name of the calculation result of the expression, value calculation expression for the polymerization column.

The return value of this function is a summary table, the matrix comprising columns and custom grouping extension columns.

1, the multi-column unique value

Grouping columns are unique, it may not return to the summary column, but only columns return packet, the table is a unique value thus obtained multiple columns.

SUMMARIZE(ResellerSales  
      , DateTime[CalendarYear]  
      , ProductCategory[ProductCategoryName] 
      ) 

2, to obtain aggregated data

For example, the data table ResellerSales, according field DateTime [CalendarYear] and ProductCategory [ProductCategoryName] packets, calculating ResellerSales [SalesAmount] and ResellerSales [DiscountAmount] additivity.

SUMMARIZE(ResellerSales 
      , DateTime[CalendarYear]  
      , ProductCategory[ProductCategoryName]  
      , "Sales Amount", SUM(ResellerSales[SalesAmount])  
      , "Discount Amount", SUM(ResellerSales[DiscountAmount])  
      ) 

The function and use of the relationship between the ResellerSales DateTime, ProductCategory, to give the association table data (an intermediate temporary table), and ProductCategory [ProductCategoryName] After grouping the data according to the associated DateTime [CalendarYear], and the calculated discount sales are added with.

Note, ResellerSales and DateTime, ResellerSales and ProductCategory explicit relationship must exist, otherwise, can not be used for grouping column.

3, the packet aggregation effect

The first temporary table as an intermediate to provide data for subsequent calculations; the second is to create a new table in the Modeling menu, create a new expression via Table DAX from "New Table":

Reference document: SUMMARIZE - groupping in the Data Models (DAX - Power Pivot, Power BI)

4, ROLLUP options

ROLLUP function for grouping columns on the volume operation, the function sets a predefined plurality of packets:

SUMMARIZE(<table>, <groupBy_columnName>[, <groupBy_columnName>]…[, ROLLUP(<groupBy_columnName>[,< groupBy_columnName>…])][, <name>, <expression>]…)

TSQL act like the rollup function, e.g., by for Group  rollup (A, B)  , which represents the set of packet group by (), group by ( a), group by (a, b).

5,ROLLUPGROUP

ROLLUPGROUP function calculates the subtotal group. If instead ROLLUPGROUP ROLLUP function, and then generates ROLLUPGROUP ROLLUP same result by adding rows to the results summarized groupBy_columnName column. However, adding in ROLLUPGROUP ROLLUP syntax () can be used to prevent the summary row subtotal. For example, ROLLUP (ROLLUPGROUP (A, B)), the packet is set (A, B), and ():

SUMMARIZE(ResellerSales_USD  
      , ROLLUP(ROLLUPGROUP( DateTime[CalendarYear], ProductCategory[ProductCategoryName]))  
      , "Sales Amount (USD)", SUM(ResellerSales_USD[SalesAmount_USD])  
      , "Discount Amount (USD)", SUM(ResellerSales_USD[DiscountAmount])  
)  

6,ISSUBTOTAL

SUMMRIZE function can only be used for checking whether the subtotal column group.

SUMMARIZE(<table>, <groupBy_columnName>[, <groupBy_columnName>]…[, ROLLUP(<groupBy_columnName>[,< groupBy_columnName>…])][, <name>, {<expression>|ISSUBTOTAL(<columnName>)}]…)

For example, this function checks whether the ProductCategoryName CalendarYear and subtotal groups:

SUMMARIZE(ResellerSales_USD  
      , ROLLUP( DateTime[CalendarYear], ProductCategory[ProductCategoryName])  
      , "Sales Amount (USD)", SUM(ResellerSales_USD[SalesAmount_USD])  
      , "Discount Amount (USD)", SUM(ResellerSales_USD[DiscountAmount])  
      , "Is Sub Total for DateTimeCalendarYear", ISSUBTOTAL(DateTime[CalendarYear])  
      , "Is Sub Total for ProductCategoryName", ISSUBTOTAL(ProductCategory[ProductCategoryName])  
)

二,SUMMARIZECOLUMNS

This function is also used for packet aggregation, and differential SUMMARIZE function is that the relationship between the non-essential grouping columns, column packet cross-connection performed between the presence or automatically.

SUMMARIZECOLUMNS( <groupBy_columnName> [, < groupBy_columnName >]…, [<filterTable>]…[, <name>, <expression>]…)

Parameter Notes:

  • groupBy_columnName: grouping columns, the column must be fully qualified name of the column, the format is base_table [column], the column must be the basis of the existing columns in the table, the grouping columns there may be 0, 1 or more. Table packets between the plurality of columns are not required to have a relationship, for different tables, a cross-connect between the packet column (cross-join); for the same table, the column is used between packets exist automatically (auto-existed).
  • filterTable: an optional parameter table based on the grouping columns where the filter, the filter present in the table for values ​​before performing cross-connection / automatic presence filtered.
  • name, expression: an optional parameter represents the custom summary column, which always appear in pairs parameter, name is the name of the calculation result of the expression, value calculation expression for the polymerization column.

The return value is a summary table that contains the grouping columns and custom columns, rows returned data, comprising at least one non-null value, if a data line, are all the result of expression BLANK / NULL, then the line does not contain the the summary table.

1, the packet fields Cartesian product

DAX grouped according to the following SalesTerritory field of Education and Customer Category field, filter and Customer table:

SUMMARIZECOLUMNS ( 'SalesTerritory'[Category], 'Customer' [Education], FILTER('Customer', 'Customer'[First Name] = “Alicia”) )

After filtration of the data summary calculation, the result returned is the Cartesian product of Category and Eduction.

2, IGNORE option

The line contains NULL / BLANK filter out

SUMMARIZECOLUMNS(<groupBy_columnName>[, < groupBy_columnName >]…, [<filterTable>]…[, <name>, IGNORE(…)]…)

For example, if Sum (Sales [Qty]) contains a NULL / BLANK, then concentrated to remove the line from the result:

SUMMARIZECOLUMNS( Sales[CustomerId], "Total Qty", IGNORE( SUM( Sales[Qty] ) ), “BlankIfTotalQtyIsNot3”, IF( SUM( Sales[Qty] )=3, 3 ) )

3, other options

NONVISUAL()

ROLLUPADDISSUBTOTAL()

ROLLUPGROUP()

Three, GROUPBY

In addition to the function can not be extended GROUPBY column using CALCULATE functions outside the same and usage SUMMARIZE:

GROUPBY (<table>, [<groupBy_columnName1>]..., [<name>, <expression>]… ) 

CALCULATE expression parameter function can not be used, CURRENTGROUP function only for the topmost scan table (Table Scan) operation.

GROUPBY operation function is executed:

  • # 1: Start from the specified table (and all related tables "to-one" direction)
  • # 2: Create a group in accordance with all the GroupBy columns, each packet represents a group of rows of data
  • # 3: For each packet, to assess the increased expansion column (Extension column). SUMMARIZE with different functions, the CALCULATE implied not performed, and the set is not placed in the context filter.

In this function, you can call CURRENTGROUP function:

CURRENTGROUP()

This function can only be used for expression parameter GROUPBY function of representing the current packet. CURRENTGROUP with no arguments, only one of the following polymeric supports as a function of the first parameter: AverageX, CountAX, CountX, GeoMeanX, MaxX, MinX, ProductX, StDevX.S, StDevX.P, SumX, VarX.S, VarX. P. For example, for Sales table, grouped according to Category and Country, and calculated for each group in the product of the Price * Qty.

GROUPBY (  
Sales,   
Geography[Country],   
Product[Category],   
“Total Sales”, SUMX( CURRENTGROUP(), Sales[Price] * Sales[Qty])  
)

 

Reference documents:

SUMMARIZE

SUMMARIZECOLUMNS

GROUPBY

DAX function reference

Guess you like

Origin www.cnblogs.com/ljhdo/p/10280962.html