Dax Functions Flashcards
RELATEDTABLE
Returns a table with all the rows related to the current one. One to many.
NumOfStudents = Countrows (Relatedtable (Students))
Can be used to generate a row context through a one to many relationship.
“X” Aggregation functions
Evaluate an expression for each row in a table. Two parameters - table and expression. SUMX AVERAGEX MINX MAXX
IN
Check if the result of an expression is included in a list of values. Use curly brackets:
Student[State] IN {“NC”,”SC”,GA”}
SWITCH
Changes values, instead of nested IF().
SWITCH ( Product[Size], "S", "Small", "M", "Medium", "L", "Large", "XL", "Extra Large", "Other")
SWITCH(TRUE()…)
Can use for if then statements.
SWITCH (TRUE (), Product[Size] = "S", 0.5, AND ( Product[Size] = "L", Product[Price] < 100 ), 0.2, Product[Size] = "L", 0.35,0 )
MAX
Returns the largest value in a column
MAX ( Sales[SalesAmount] )
or between two columns
MAX ( Sales[Amount], Sales[ListPrice] )
IFERROR
Returns an alternative expression IFERROR ( Sales[GrossMargin] / Sales[Amount], BLANK () )
DIVIDE
Avoid using IF to check for errors. Returns an alternative value.
Variables
Calculate intermediate values in a measure or column.
VAR….
RETURN….
Variable values are assigned in the context they are defined, not in the context they are used. Calculate cannot modify a variable.
FILTER
FILTER •Adds a new condition •Restricts the number of rows of a table •Returns a table •Can be iterated by an «X» function Needs a table as input The input can be another FILTER
Filters on entire tables are bad. Narrow the table using ALL; add KEEPFILTERS if you need it to respond to other filters in the report.
ALL
ALL •Returns all the rows of a table •Ignores any filter •Returns a table that can be iterated by an «X» function Needs a table as input
Can be used with a single column
ALL ( Customers[CustomerName] )
•The result contains a table with one column that contains all unique values in the column
COUNTROWS ( ALL ( Orders[Channel], Orders[Color], Orders[Size] )) Returns a table with all the values of all the columns passed as parameters.
ALLEXCEPT
Removes all filters except those applied to the specified column.
Returns a table with all existing combinations of the given columns.
ALLEXCEPT(
Orders, Orders[City] )
{NEED MORE INFO HERE}
DISTINCT
Returns the unique values of a column, only the ones visible in the current filter context.
NumOfProducts := COUNTROWS ( DISTINCT ( Product[ProductCode] ) ) Compare to VALUES()
VALUES
Returns the unique values of a column, only the ones visible in the current filter context, including the additional blank row if it is visible in the filter context. NumOfProducts := COUNTROWS ( VALUES ( Product[ProductCode] ) )
ALLNOBLANKROW
ALL returns the additional blank row, if it exists. ALLNOBLANKROW omits it.—- Returns only the existing products
COUNTROWS (
ALLNOBLANKROW ( Products[ProductKey] )
)
Counting different values
DISTINCT and ALLNOBLANKROW won’t include blank row, VALUES and ALL will include blank row
ALLSELECTED
With the date filters, I needed to always be evaluating the actual dates that were being filtered. I didn’t want to have any additional information. I only wanted to focus on what was selected. This is in simplest form what ALLSELECTED does. It focuses on what you have selected within your report page.
(Need more here)
1 x 1 Tables
when a table contains one row and one column, can treat it as a scalar value. Use to display selected values as text, make visible to the user.
SELECTEDVALUE
Retrieves the value of a column when only one value is visible. Sel Category := "You selected: " & SELECTEDVALUE ( 'Product Category'[Category], "Multiple values")
ISEMPTY
Checks if a table is empty, Returns True or False.
ISEMPTY ( VALUES ( Product[Unit Price] ) )
is equivalent to
COUNTROWS ( VALUES ( Product[Unit Price] ) ) = 0
Table variables
Variables can contain a scalar value or a table. Using table variables greatly helps in splitting complex expressions.
Evaluation context
All of the filters etc. applied to a formula that affect its evaluation.
Slicers, visual filters, other graphs on page, etc.
Filter context
Defined by
Row selection, column selection, report filters, slicer selection.
By default, flows from the one side to many side of relationships.
Row context
Iterates. Needed to evaluate column values, it is the concept of “current row”. In calculated column, defined automatically for each row. In row iteration functions, it is defined by user formulas.
Does not propagate through relationships.