[Power BI] SELECTCOLUMNS and ADDCOLUMNS in DAX

Both ADDCOLUMNS and SELECTCOLUMNS are DAX functions used to add or manipulate columns within a table expression. However, they have different purposes and behavior.
Cả ADDCOLUMNS và SELECTCOLUMNS đều trả về kết quả là một bảng. Tuy nhiên, chúng có một vài đặc điểm và cách sử dụng khác nhau.

We have a table named Customer the following columns:
Chúng ta có bảng được đặt tên Customer và một số cột sau:
– customer_id,
– customer_email,


ADDCOLUMNS

=ADDCOLUMNS(Table, Name1, Expr1 [, Name2, Expr2 [, ...]])
  • Table: The table to which new columns will be added.
    Bảng được thêm cột vào, bảng này có thể là bảng sẵn có hoặc tạo mới thông qua các hàm tạo bảng như FILTER, ALL,…
  • Name1, Name2, ...: Names of the new columns.
    Tên cột
  • Expr1, Expr2, ...: Expressions that define the values for the new columns.
    Hàm tạo ra cột tương ứng

The ADDCOLUMNS function indeed adds new columns to the front of the existing columns. There isn’t a built-in parameter or option in ADDCOLUMNS to specify the position of the new columns behind the existing columns.
Những cột mới được thêm vào luôn nằm trước những cột sẵn có. Hàm này không có thông số để thiết lập các cột mới thêm vào phía sau.

Customers_more_info =
ADDCOLUMNS(
    FILTER( Customer, Customer[customer_id] IN { 300301302303304 } ),
    “Mail Extension”,
        MID(
            Customer[customer_email],
            FIND( “@”, Customer[customer_email] ) + 1,
            1000
        ),
    “Company”,
        UPPER(
            MID(
                Customer[customer_email],
                FIND( “@”, Customer[customer_email] ) + 1,
                FIND( “.”, Customer[customer_email] ) – FIND( “@”, Customer[customer_email] ) – 1
            )
        )
)

Detailed Explanation:
Giải thích chi tiết:

  • FILTER(Customer, Customer[customer_id] IN { 300, 301, 302, 303, 304 }):
    This part filters the Customer table to include only rows where the customer_id is one of the specified values (300, 301, 302, 303, 304).
    Lọc ra từ bảng Customer các dòng dữ liệu có customer_id thuộc một trong các số trong ngoặc nhọn.
  • ADDCOLUMNS(...):
    The ADDCOLUMNS function adds new columns to the filtered table.
    Hàm ADDCONLUMNS thêm các cột mới vào bảng đã lọc trước đó.
  • "Mail Extension":
    The name of the first new column being added.
    Tên của cột thứ nhất được thêm vào bảng.
  • UPPER(MID(Customer[customer_email], FIND("@", Customer[customer_email]) + 1, 1000)):
    This expression extracts the substring of the customer_email column starting from the position after the “@” symbol and extending to a length of 1000 characters (to ensure it captures the entire extension).
    Hàm MID giúp lấy ra các ký tự sau dấu @ trở đi.
  • "Company":
    The name of the second new column being added.
    Tên cột thứ hai được thêm vào bảng.
  • MID(Customer[customer_email], FIND("@", Customer[customer_email]) + 1, FIND(".", Customer[customer_email]) - FIND("@", Customer[customer_email]) - 1):
    This expression extracts the substring of the customer_email column starting from the position after the “@” symbol and extending up to the position just before the “.” symbol.
    Hàm MID giúp lấy ra các ký tự nằm giữa dấu “@” và dấu “.”.

Key points about ADDCOLUMNS:

  • Adds New Columns: adding new columns to the original table while retaining all existing columns.
    Thêm cột mới vào bảng sẵn có, và giữ tất cả các cột trong bảng đó.
  • Iterates Over Each Row: It iterates over each row of the specified table and evaluates the expressions provided for each row to calculate the values of the new columns.
    Lặp lại phép tính qua từng hàng một, tạo ra từng hàng giá trị trong cột mới đó.
  • Can Refer to Existing Columns: Expressions provided in ADDCOLUMNS can refer to existing columns in the table.
    Các hàm được thực hiện bên trong ADDCOLUMNS có thể tham chiếu đến các cột đang có sẵn trong bảng.
  • Supports Conditional Logic: You can include conditional logic within the expressions to calculate the values of the new columns conditionally.
    Có thể chứa các phép logic có điều kiện để tính toán các giá trị tạo ra trong cột mới.
  • Flexible for Complex Calculations: ADDCOLUMNS allows you to perform complex calculations involving existing columns and other DAX functions.
    ADDCOLUMNS cho phép bạn thực hiện các phép tính phức tạp với cột sẵn có cùng với các hàm DAX khác.

SELECTCOLUMNS

=SELECTCOLUMNS(Table, Name1, Expr1 [, Name2, Expr2 [, ...]])
  • Table: The table from which columns are selected.
    Bảng từ các cột được chọn.
  • Name1, Name2, ...: Names of the columns in the new table.
    Tên các cột trong bảng mới.
  • Expr1, Expr2, ...: Expressions that define the values for the new columns.
    Hàm để tạo ra giá trị trong các cột mới.
Customers_more_info =
SELECTCOLUMNS(
    FILTER( Customer, Customer[customer_id] IN { 300301302303304 } ),
    “Customer_ID”, Customer[customer_id],
    “Email”, Customer[customer_email],
    “Mail Extension”,
        MID(
            Customer[customer_email],
            FIND( “@”, Customer[customer_email] ) + 1,
            1000
        ),
    “Company”,
        UPPER(
            MID(
                Customer[customer_email],
                FIND( “@”, Customer[customer_email] ) + 1,
                FIND( “.”, Customer[customer_email] ) – FIND( “@”, Customer[customer_email] ) – 1
            )
        )
)

Detailed Explanation:
Giải thích chi tiết:

  • FILTER(Customer, Customer[customer_id] IN { 300, 301, 302, 303, 304 }):
    This part filters the Customer table to include only rows where the customer_id is one of the specified values (300, 301, 302, 303, 304).
    Lọc ra từ bảng Customer các dòng dữ liệu có customer_id thuộc một trong các số trong ngoặc nhọn.
  • SELECTCOLUMNS(...):
    The SELECTCOLUMNS function creates a new table with specific columns based on the filtered Customer table.
    Hàm SELECTCOLUMNS tạo ra bảng mới với các cột được chọn.
  • "Customer_ID", Customer[customer_id]:
    This creates a column named “Customer_ID” in the new table, which contains the values from the customer_id column of the Customer table.
    Chọn cột customer_id từ bảng Customer và đặt tên Customer_ID.
  • "Email", Customer[customer_email]:
    This creates a column named “Email” in the new table, which contains the values from the customer_email column of the Customer table.
    Chọn cột customer_email từ bảng Customer và đặt tên Email.
  • "Mail Extension":
    The name of the first new column being added.
    Tên của cột thứ nhất được thêm vào bảng.
  • UPPER(MID(Customer[customer_email], FIND("@", Customer[customer_email]) + 1, 1000)):
    This expression extracts the substring of the customer_email column starting from the position after the “@” symbol and extending to a length of 1000 characters (to ensure it captures the entire extension).
    Hàm MID giúp lấy ra các ký tự sau dấu @ trở đi.
  • "Company":
    The name of the second new column being added.
    Tên cột thứ hai được thêm vào bảng.
  • MID(Customer[customer_email], FIND("@", Customer[customer_email]) + 1, FIND(".", Customer[customer_email]) - FIND("@", Customer[customer_email]) - 1):
    This expression extracts the substring of the customer_email column starting from the position after the “@” symbol and extending up to the position just before the “.” symbol.
    Hàm MID giúp lấy ra các ký tự nằm giữa dấu “@” và dấu “.”.

Key points about SELECTCOLUMNS:

  • Creates a New Table: Unlike ADDCOLUMNS, SELECTCOLUMNS creates a new table by selecting specific columns from an existing table or by defining new columns with expressions.
    Không giống ADDCOLUMNS, SELECTCOLUMNS tạo ra một bảng mới bằng cách chọn trực tiếp các cột nào đó hoặc sử dùng hàm để biến đổi chúng.
  • No Iteration Over Rows: SELECTCOLUMNS does not iterate over individual rows of the original table. Instead, it creates a new table by applying expressions to the entire columns.
    SELECTCOLUMNS không lặp lại hàm qua từng dòng riêng lẻ. Thay vào đó, hàm tạo bảng bằng cách áp dụng công thức cho cả cột.
  • Useful for Column Renaming: It’s commonly used for renaming columns or creating new calculated columns based on expressions.
    Được dùng phổ biến trong việc đổi tên cột hoặc tạo cột mới.

Good luck!

Leave a comment