DAX Functions (Slides)
DAX Functions (Slides)
DAX functions
Please do not copy without permission. © ExploreAI 2023.
Calculated columns with DAX
| There are roughly 250 different functions within Power BI, but the most popular ones can be
grouped into the following broad categories.
These functions are used for summarising or aggregating data in columns or tables into a
Aggregation
single value. They correspond closely to spreadsheet functions.
Count functions are used for returning the number of rows/values in a column or table.
Count
Columns are the only accepted input for count functions.
Logical functions return values or sets based on whether an expression is true or false (i.e.
Logical
whether a condition has been met or not).
These functions are useful when examining data quality or types. A cell/row of data is
Information provided as input and the function returns whether the cell/row value matches the expected
type.
2
Calculated columns with DAX
Used when doing calculations based on dates and time. These functions use a datetime
Temporal / data type and can use values from a column as input.
3
Calculated columns with DAX
Aggregation functions
Measure = FUNCTION(Table_name[column_name])
Syntax
Measure_with_expression = FUNCTIONX(Table_name, Expression)
4
Calculated columns with DAX
Aggregation example:
|
In this example, we are interested in the number of males living in rural areas in the different
countries in our Gender_parity_2022 table. By multiplying the population size with the
proportion of rural male population, we can easily see this metric.
Total_pop = SUM(Gender_parity_2022[Population
size])
Rural_male_pop = SUMX(Gender_parity_2022,
Gender_parity_2022[Population size] *
Gender_parity_2022[Rural population, male (% of
total)])
5
Calculated columns with DAX
Count functions
| We use count functions to count certain elements in a table or column. These functions follow
the same format as the common aggregation functions.
Count_of_something = FUNCTION(Table_name[column_name])
Syntax
Count_of_some_expression = FUNCTIONX(Table_name, Expression)
COUNT() Returns the number of non-blank values in a column. Supports strings, integers, or dates.
COUNTX() Returns the number of non-blank values as the result of an expression. Uses table as input.
COUNTROWS() Counts the number of rows in a table. Uses table as input.
DISTINCTCOUNT() Counts the number of distinct values in a column, including the BLANK value.
COUNTBLANK() Counts the number of BLANK cells in a column.
COUNTA() Counts the number of non-blank cells in a column. In contrast to COUNT(), it also supports Boolean values.
6
Calculated columns with DAX
Count example
| In the dataset below, we have a very small class with 5 students and a test mark per student. We
want to know how many students didnʼt write the test (with a valid excuse).
Counting = COUNTBLANK(Test_data[Mark_%])
Total_blanks
Total_students = COUNT(Test_data[Student_ID])
Count_rows = COUNTROWS(Test_data)
Count_marks = COUNT(Test_data[Mark_%])
7
Calculated columns with DAX
| These functions enable us to make use of conditional logic when creating new columns.
value.
IFERROR() Evaluates an expression and returns an alternative pre-specified value if the
expression results in an error.
SWITCH() Evaluates an expression (or column) against a list of values and returns one of
the pre-specified results. By using Boolean values in the expression, we can also use this
instead of nested IF() statements.
8
Calculated columns with DAX
IF/SWITCH example
|
Using our Demographic_info dataset, weʼre interested in creating a column that would
categorise countries as small or large, based on the Area_km2 column. The second code
block shows a nested version of IF(), based on three categories for the country sizes.
Small_Med_Large = SWITCH(TRUE,
Demographic_info[Area_km2] > 500000, "Large country",
Demographic_info[Area_km2] > 100000, "Medium country",
Demographic_info[Area_km2] > 0, "Small country")
9
Calculated columns with DAX
Information functions
|
These functions operate on a cell/row level and evaluate whether the input argument is what is
expected. If it is, the function returns TRUE, otherwise FALSE. They are often used to perform
calculations wrapped within an IF statement, to prevent errors in the results.
10
Calculated columns with DAX
Example
|
We need to determine whether all the Student_IDs in our data are in the right format (contains
“ID_ˮ as a prefix), and also whether the marks are populated. We can use two information
functions to solve this issue.
Is_blank = ISBLANK(Test_data2[Mark_%])
BLANK cells in Power BI can display
different behaviours depending on
column types. It is good practice to
check that ISBLANK and
COUNTBLANK functions behave in
the way assumed, especially if used
?
for quality checks.
11
Calculated columns with DAX
Text functions
|
Text columns frequently need to be manipulated in some way in order to be used in
visualisations. Power BI functions can help us create columns in a specific format, with the
benefit that they correspond closely to string functions found in spreadsheets and SQL.
SUBSTITUTE() Replaces existing text with new text in a text string. Text to be replaced is explicitly stated.
REPLACE() Replaces part of a string with new text, based on the number of characters and position specified.
SEARCH() Returns the number of the character at which a specific string/character first occurs in text, from left to right. Can
specify starting position for the search.
FIXED() Rounds a number to specified decimals, then converts to string.
CONCATENATE() Joins two text strings together into one.
UPPER/LOWER() Converts the case of a text string to all uppercase or all lowercase.
LEFT() Returns the specified number of characters from the start of a text string.
RIGHT() Returns the last character or characters in a text string, based on the number of characters specified.
12
Calculated columns with DAX
|
Our Country column in our dataset accidentally includes the first two letters of the Country_ID
column, as something went wrong during the data processing phase. We need to clean the
data by making use of some text manipulation functions.
Column of interest
13
Calculated columns with DAX
Temporal functions
|
By using temporal functions in Power BI, we can do time-based analysis on columns. It is
useful for productivity measures, comparisons over different periods, and determining
person-level indicators such as age.
14
Calculated columns with DAX
|
We have a small dataset (Grants), but the day, month, and year have been captured in separate
columns. Convert these columns to a single date field, showing the actual day of the week, and
then calculate how many months have gone by between the end and start date.
15
Calculated columns with DAX
| Filter functions in DAX enable us to manipulate the data context to create dynamic calculations,
whereas relationship functions help to manage and utilise relationships between tables.
16
Calculated columns with DAX
Filter example
|
We are interested in creating a table E_Africa) that contains a subset of the data in our original
table. We also want to add the Country name, a column from another table. The linking columns
have already been identified and relationships are set up.
17