Table Manipulation DAX Functions
Table Manipulation DAX Functions
ADDCOLUMNS is DAX aggregation function, used to Adds calculated columns to the given table or table
expression.
It’s comes under Table Manipulation DAX Functions category. It will return a table with all its original
columns and the added ones.
Syntax:
ADDCOLUMNS(<table>, <name>, <expression>[ <name>, <expression>]…)
So, Let’s start with an example, You can download the sample Dataset from below link
SuperStoreUS-2015.xlxs
Create table
Step 2: DAX for Summarize table
Summarize Table =
Now you can perform other operations with this summarize table
East Sale = CALCULATE(
SUM('Summarize Table'[Total Sale]),
FILTER('Summarize Table', 'Summarize Table'[Region]="East"))Copy
Summarize DAX with Filter
Summarize Table with filter =
SUMMARIZE(Orders,
DAX GROUPBY function is similar to DAX SUMMARIZE function. However, GROUPBY does not do
an implicit CALCULATE for any extension columns that it adds.
Syntax:
GROUPBY (<table>, [<groupBy_columnName1>], [<name>, <expression>]… )
Note:
1. The expression used in GroupBy may include any of the “X” aggregation functions, such as SUMX,
AVERAGEX, MINX, MAXX, etc.
2. DAX CALCULATE function and calculated fields are not allowed in the expression.
3. groupBy_columnName must be either in table or in a related table.
4. Each name must be enclosed in double quotation marks.
5. This function is not supported for use in DirectQuery mode when used in calculated columns or row-
level security (RLS) rules.
6. CURRENTGROUP function can only be used in an expression that defines a column within the
GROUPBY function.
So, Let’s start with an example, you can download the sample Dataset from below link
o SuperStoreUS-2015.xlxs
Create new table, Go to Modeling tab and click to Table
Create table
GroupByDAX
It supports only “X” aggregations function with CURRENTGROUP DAX:-
GroupByTable =
GROUPBY(Orders, Orders[Region], Orders[Product Category],
"Sale", SUMX(CURRENTGROUP(),Orders[Sales]),
"Profit", SUMX(CURRENTGROUP(),Orders[Profit]))Copy
GroupByDaxCurrentGroup
Working of Nested GROUPBY:-
It will return MAX Product Category Sales region wise.
GroupByTable =
GROUPBY(
GROUPBY(Orders, Orders[Region], Orders[Product Category],
"TotalSale", SUMX(CURRENTGROUP(),Orders[Sales])
),
Orders[Region],
"MAX Sales", MAXX(CURRENTGROUP(),[TotalSale]))Copy
NestedGroupBY
GROUPBY with Filter:-
GroupByTable =
FILTER(
GROUPBY(Orders, Orders[Region], Orders[Product Category],
"Sale", SUMX(CURRENTGROUP(),Orders[Sales]),
"Profit", SUMX(CURRENTGROUP(),Orders[Profit]))),
Orders[Region]="Central")Copy
GroupByDAXWithFilter
GROUPBY with ADDCOLUMNS, support CALCULATE:-
GroupByTableWithAddColumns =
ADDCOLUMNS(
GROUPBY(Orders,
Orders[Region],
Orders[Product Category]),
"Sale", CALCULATE(SUM(Orders[Sales])),
"Profit", CALCULATE(SUM(Orders[Profit]))
)
Datatable is a DAX function, using this function you can create Static Dataset/ Table in Power BI,
that cannot be refreshed but you can modify it.
Syntax:
DATATABLE (column1, datatype1,
coulmn2, datatype2,
{
{value1, value2},
{value3, value4 }
}
)Copy
Power BI Data Types as below:
o BOOLEAN (True/False)
o CURRENCY (Fixed Decimal Number)
o DATETIME (Date/Time)
o DOUBLE (Decimal Number)
o INTEGER (Whole Number)
o STRING (Text)
Let’s start with an example
Step-1: Go to Modeling tab and click on New table
Sales_Range_Datatable =
DATATABLE (
{ "Low", 0, 1000 },
)Copy
Step-3: Table created successfully.
UNION is a Power BI DAX function, it is used to combine two or more tables rows. Its comes under
Table Manipulation DAX Functions category.
Refer similar DAX Post – EXCEPT, INTERSECT
Points to be remember:
o The tables should be same number of columns.
o Columns are combined by position in their respective tables.
o The column names in the return table will match the column names in the first Table.
o It returns the all tables rows, including duplicate rows.
Syntax:
UNION ( <Table 1>, <Table 2> …. ,<table N>)Copy
Description:
S no. Parameter Description
1 table name A table that will participate in the union and it is repeatable.
Union Query
EXCEPT DAX function is used to returns the rows of one table which do not present in another
table. Its comes under Table Manipulation DAX Functions category.
Syntax:
EXCEPT(<table_expression1>, <table_expression2>Copy
Refer similar DAX Post – INTERSECT, UNION
Description:
S no. Parameter Description
Note:
o The two tables must have the same number of columns.
o Columns are compared based on positioning.
o This function is not supported for use in Direct Query mode.
Let’s use these two tables for practical-
Table-1
ID Product Price
1 Bike 50000
2 Car 400000
Table-2
ID Product Price
1 Bike 50000
2 Car 400000
Create table
Step-2: DAX formula screen appears, write DAX formula here and press enter key.
Except = EXCEPT('Table-1', 'Table-2')Copy
EXCEPT DAX
Step-3: You can see the new created table under fields, and it will return the rows of Table-
1 which do not appear in Table-2.
EXCEPT DAX Output
EXCEPT-DAX
See the output, it will return the rows of Table-2 which do not appear in Table-1.
1 Bike 30
2 Car 12
3 AC 3
4 Bus 20
Table: Price
Item Price
Bike 60000
Bus 1500000
AC 50000
Car 500000
Truck 2500000
Lights 3000
-NATURALINNERJOIN
Performs an inner join of a table with another table. The tables are joined on common columns (by
name) in the two tables.
The NATURALINNERJOIN function joins the left table with right table using the Inner join
semantics.
This function returns a table with matching values in both tables.
Syntax:
NATURALINNERJOIN(<leftJoinTable>, <rightJoinTable>)Copy
Parameters:
leftJoinTable: A table expression defining the table on the left side of the join.
rightJoinTable: A table expression defining the table on the right side of the join.
Follow these steps in order to implement NATURALINNERJOIN-
Step-1: Go to the Modeling tab > click on create a new table icon.
-NATURALLEFTOUTERJOIN
Performs an inner join of a table with another table. The tables are joined on common columns (by
name) in the two tables.
The NATURALLEFTOUTERJOIN function joins the left table with right table using the left outer join
semantics.
This function returns all records from the left table (table1), and the matched records from the
right table (table2).
Syntax:
NATURALLEFTOUTERJOIN(<leftJoinTable>, <rightJoinTable>)Copy
Parameters:
leftJoinTable: A table expression defining the table on the left side of the join.
rightJoinTable: A table expression defining the table on the right side of the join.
NaturalLeftOuterJoin DAX
You can see in above screen shot it returns all records from the left table, and the matched records
from the right table.
INTERSECT DAX Function in Power BI
by Power BI Docs
DAX
INTERSECT DAX function – compares two tables and returns common rows. The output of
INTERSECT function is a table with the common rows. Its comes under Table Manipulation DAX
Functions category.
Syntax:
INTERSECT(<table_expression1>, <table_expression2>Copy
Refer similar DAX Post – EXCEPT, UNION
Description:
S no. Parameter Description
Note:
o A table that contains all the rows in table_expression1 that are also in table_expression2
o The two tables must have the same number of columns.
o Columns are compared based on positioning.
o This function is not supported for use in Direct Query mode.
1 Bike 50000
2 Car 400000
3 Cycle 15000
4 Bus 2500000
5 Truck 3500000
Table-2
ID Product Price
1 Bike 50000
2 Car 400000
5 Truck 3500000
6 AC 500000
7 Fan 5000
Create table
Step-2: DAX formula screen appears, write DAX formula here and press enter key.
Intersect DAX= INTERSECT('Table-1', 'Table-2')
Copy
Intersect DAX
Step-3: You can see the new created table under fields, and it will return the common rows
of Table-1 and Table-2.
Relationship Functions
Description
columnName1: A fully qualified name of a column that represents the many side of the
relationship to be used.
columnName2: A fully qualified name of a column that represents one side or lookup side of the
relationship to be used.
Create a Relationship
Step-4: Now create a measure to count the number of orders shipped, here no need to create a
measure for Orders count because Order date relationship is Active with Calendar date.
Count of Ship =
CALCULATE (
COUNT ( Orders[Ship Date] ),
USERELATIONSHIP ( Orders[Ship Date], 'Calendar'[Date] )
)Copy
Step-5: Now add one Matrix visual in Power BI report page and drag some fields with measure.
Rows: Drag Calendar dataset Month column.
Values: Drag Order Date then right click on Order date and select count. And drag “count of ship”
measure.
Display Active Inactive Relationship in Matrix
So, you can saw above screenshot how we active the “Ship date” column relationship with calendar
date and got the result.
Note:
o This function is used to identifying relationships by their ending point columns.
o we can join Table A and Table B in a calculation, each relationship must be indicated in a different by
using this function.
o This function can’t be used when row-level security is defined for the table in which the measure is
included. It can only be used in DAX functions that take a filter as a parameter. For example, CALCULATE,
CALCULATETABLE, CLOSINGBALANCEMONTH, CLOSINGBALANCEQUARTER, CLOSINGBALANCEYEAR,
TOTALMTD, TOTALQTD and TOTALYTD functions.
o Up to 10 nested function can be used with USERELATIONSHIP functions.
Hope you enjoyed the post. Your valuable feedback, question, or comments about this post are
always welcome or you can leave us message on our contact form , we will revert to you asap.
Description:
tableName – The name of an existing table using standard DAX syntax. It cannot be an expression.
For RelatedTable function relationship between two tables should be:
o One-to-One Relationship
o Many-to-One Relationship, new column or measure can only be defined on the many side of the
relationship
1 Bike 2
2 Cycle 1
3 Cooler 4
Product Table
Product
Bike
AC
Cooler
Cycle
New column
Step-3: Now write below Dax.
Qty = SUMX(RELATEDTABLE(Orders), Orders[Qty])
Copy
As you can see here we created a column inside Product table and RelatedTable returns
a Orders table value here.
Dataset Output