0% found this document useful (0 votes)
0 views17 pages

DAX Functions (Slides)

The document provides an overview of DAX functions used in Power BI, categorizing them into groups such as aggregation, count, logical, information, text, temporal, and filter functions. It includes examples of how to use these functions for calculations, data manipulation, and quality checks. The document emphasizes the importance of understanding these functions for effective data analysis and visualization in Power BI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views17 pages

DAX Functions (Slides)

The document provides an overview of DAX functions used in Power BI, categorizing them into groups such as aggregation, count, logical, information, text, temporal, and filter functions. It includes examples of how to use these functions for calculations, data manipulation, and quality checks. The document emphasizes the importance of understanding these functions for effective data analysis and visualization in Power BI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Calculated columns with DAX

DAX functions
Please do not copy without permission. © ExploreAI 2023.
Calculated columns with DAX

Diving deeper into DAX functions

| 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

Diving deeper into DAX functions


These functions are similar to string functions found in spreadsheets, with the ability to be
Text / applied to columns and tables.

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.

Filter functions in Power BI allow us to manipulate data context, resulting in dynamic


Filter
Text / calculations.

Power BI documentation contains detailed information on mathematical, financial,


Other / statistical, and many other categories of functions that could be of use.

3
Calculated columns with DAX

Aggregation functions

| These functions perform calculations on a set of values to produce a single, summarising


value, often used in measures.

Measure = FUNCTION(Table_name[column_name])
Syntax
Measure_with_expression = FUNCTIONX(Table_name, Expression)

Common aggregation functions

SUM() Aggregates the values in a column. X for expression


SUMX() Aggregates the values in an expression. Power BI has
extensive The X at the end of these
MIN()/MAX() Returns the min/max value in a column. documentation aggregation functions refers
MINX()/MAXX() Returns the min/max value of an expression. explaining syntax to expression and it means
for all the DAX that we can add any
AVERAGE() Returns the arithmetic mean of a column.
functions. expression which will then be
AVERAGEX() Returns the arithmetic mean of an expression. used as the ‘input columnʼ.

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)

Common count functions

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_%])

Why care about BLANKS?

Missing values might influence our dashboardʼs reliability.


If a large proportion of data points are missing, the
summary might present a skewed view.

7
Calculated columns with DAX

Logical and conditional functions

| These functions enable us to make use of conditional logic when creating new columns.

Common logical functions

AND() Returns TRUE if both arguments are TRUE. Important


OR() Returns TRUE if one of the arguments is TRUE.
NOT() Returns TRUE if the condition is not met. Logical functions can only
IF() Evaluates a condition, if TRUE, it returns one value, otherwise it returns the second be used on 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_or_large = IF(Demographic_info[Area_km2] > 100000,


"Large country", "Small country")

Small_Med_Large = IF(Demographic_info[Area_km2] > 500000,


"Large country", IF(Demographic_info[Area_km2] > 100000,
"Medium country", "Small country"))

This exact statement can also be written with a SWITCH() function:

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.

Common information functions

ISBLANK() Checks whether a value/expression is BLANK. Case sensitive?


ISNUMBER() Checks whether a value/expression is numeric.
Certain functions are not
ISTEXT() Checks whether a value is of type text. case sensitive, such as
ISNONTEXT() Checks whether a value is non-text. CONTAINSSTRING(). If we
are trying to find a case
ISERROR() Checks whether value/expression results in error. sensitive string, weʼd use
CONSTAINSSTRING() Checks if the specified text is found in a string. CONTAINSSTRINGEXACT()
instead.

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.

ID_check = CONTAINSSTRING(Test_data2[Student_ID],"ID") Power BI BLANK functions

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.

Common text functions

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

String manipulation example

|
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

Country_new = REPLACE(Country_simple[Country], Power BI documentation


LEN(Country_simple[Country]) - 1 ,2 ,"")
Here we used LEN() (returning the
Getting the position of where the replacement text should Specifying that we want to replace 2 length of a string) within our text
go, in this case the length of the name, 1 position characters, with nothing ("") manipulation formula. Power BI
documentation contains a full list of
useful functions not directly
covered here.

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.

Common date functions

DATE() Returns a specified date in datetime format.


HOUR() Returns the hour as a number from 0 to 23, based on 24-hour time notation 0000  2359.
WEEKDAY() Returns the day of the week as a number 17.
NOW() Returns the current date and time in a datetime format.
EOMONTH() Returns the date in datetime format of the last day of the month, before or after a specified number of months.
DATEDIFF() Counts the number of intervals between two dates. Intervals can be seconds up to years.

14
Calculated columns with DAX

Date and time example

|
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.

Start_date = DATE(Grants[Start_year], Grants[Start_month], Grants[Start_day])


Weekday_nr = WEEKDAY(Grants[Start_date], 1)
Weekday_format = FORMAT(Grants[Weekday_nr], ”DDD”)
Diff_months = DATEDIFF(Grants[Start_date], Grants[End Date], MONTH)

15
Calculated columns with DAX

Filter and relationship functions

| 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.

Common filter functions


ALL() Returns all rows in a table, ignoring any filters that may have been applied.
CALCULATE() Evaluates an expression in a modified filter context.
CALCULATETABLE() Evaluates a table expression in a modified filter context.
FILTER() Returns a table that is a subset of another table or expression.
LOOKUPVALUE() Returns a row based on criteria specified in a search condition. There can be multiple search
conditions.
USERELATIONSHIP() The function specifies a relationship to be used in a specific calculation.
RELATED() Returns a related value from another table.

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.

E_Africa = FILTER(Demographic_info, Country_name =


Demographic_info[Region] == "Eastern Africa") RELATED(Country_simple[Country_name])

17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy