0% found this document useful (0 votes)
43 views23 pages

DAX Formulas, Columns and Measures

Uploaded by

kachizih
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)
43 views23 pages

DAX Formulas, Columns and Measures

Uploaded by

kachizih
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/ 23

DAX Formulas, Columns and

Measures

© Explore Data Science Academy


Learning Objectives

After this train, you should be able to:

- Understand the different command functions in Power BI

- Identify different DAX formulas

- Differentiate between calculated columns and measures

- Apply DAX formulas to creating calculated columns and

measures
Topics to be covered

The following material will be covered in this train.

1. Overview of commands and queries in Power BI


Understand the purpose of commands in Power BI

2. DAX formula categories


Different types of DAX formula functions

3. Calculated columns and measures


Understand and identify differences between
the two

4. Creating measures for IPL data


Create both calculated columns and measures using the IPL
data
Overview of Power BI Commands and Queries

Power BI offers the functionality to employ commands and queries for data analysis

What makes Power BI commands and queries so powerful?

DAX formulas are powerful functions used to manipulate, aggregate and


calculate metrics for a Power BI dashboard
DAX Formulas

Calculated columns assist to create new columns or fields from columns that
have current dependencies or are related in some way; this allows a user to
Calculated Columns create columns needed to obtain the results for analysis

Creating measures makes it easier to apply calculations on the dataset; you are
able to finetune the input and output data
Measures
DAX Formulas

Categories of DAX Formulas

AGGREGATE COUNTING LOGICAL TEXT

MIN DISTINCTCOUNT AND REPLACE


MAX COUNT OR SEARCH
Average COUNTA NOT UPPER/LOWER
SUM COUNTROWS IF FIXED
SUMX COUNTBLANK IFERROR CONCATENATE

TEMPORAL INFORMATION FILTER

DATE ISBLANK ALL


HOUR ISNUMBER CALCULATE
WEEKDAY ISTEXT CALCULATETABLE
NOW ISNONTEXT FILTER
EOMONTH ISERROR LOOKUPVALUE
DAX Formulas

Categories of DAX Formulas - Aggregate

MIN(<column>) - Finds the smallest (minimum value) in a


column
AGGREGATE

MAX(<column>) - Finds the largest (maximum value) in a


column

MIN AVERAGE(<column>) - Finds the average/mean value in a


MAX column
AVERAGE
SUM
SUMX SUM(<column>) - Finds the sum of all the values in a column

SUMX(<table>, <expression>)- Sums all values as an


iterator (row by row) ; allows a user to calculate the sum over
multiple columns

Note: Aggregations require the data type to be a number data type


DAX Formulas

Categories of DAX Formulas - Counting

DISTINCTCOUNT(<column>) - Counts the distinct values in a


single column
COUNTING

COUNT(<column>) - Counts the number of cells with


numerical values in a column (omits blank values)

DISTINCTCOUNT
COUNT COUNTA(<column>) - Counts the number of cells in total in a
COUNTA column (omits blank values)
COUNTROWS
COUNTBLANK
COUNTROWS(<table>) - Counts the number of rows in a
table or table defined by an expression

COUNTBLANK(<column>)- Counts the number of blank cells


in a column
DAX Formulas

Categories of DAX Formulas - Logical

AND(<logical1>,<logical2>) - Evaluates two TRUE


arguments as TRUE or else returns FALSE
LOGICAL

OR(<logical1>,<logical2>) - Evaluates if one argument


is TRUE; returns FALSE if both arguments are false

AND
OR NOT(<logical>) - Reverses TRUE arguments to FALSE and
NOT FALSE arguments to TRUE
IF
IFERROR
IF(<logical_test>, <value_if_true>[,
<value_if_false>]) - Evaluates a logic test (condition) to
return a value if proven TRUE or a value if proven FALSE

IFERROR(value, value_if_error)- Evaluates an


expression and returns a value if the expression raises an error
DAX Formulas

Categories of DAX Formulas - Text


REPLACE(<old_text>, <start_num>, <num_chars>,
<new_text>) - Replaces a string based on the character
TEXT numbers provided at a specific position with a new text

SEARCH(<find_text>, <within_text>[,
[<start_num>][, <NotFoundValue>]]) - Returns the
positional number of a character(s); can search within a
column
REPLACE
SEARCH UPPER(<text>) - Converts text string to upper case
UPPER/LOWER LOWER(<text>) - Converts text string to lower case
FIXED
CONCATENATE
FIXED(<number>, <decimals>, <no_commas>) - Rounds
a number to a specified decimal point and returned as a text

CONCATENATE(<text1>, <text2>)- Combines two texts


next to each other (order is important) ; can be used on whole
columns
DAX Formulas

Categories of DAX Formulas - Temporal

DATE(<year>, <month>, <day>) - Returns the specified


data in the datetime format
TEMPORAL

HOUR(<datetime>) - returns the hour as a whole number


(eg. 10:00 am is returned as “10”)

DATE
HOUR WEEKDAY(<date>, <return_type>) - Returns a whole
WEEKDAY number representing the day of the week (eg. Sunday will
return “1”)
NOW
EOMONTH
NOW() - Returns the current date and time in datetime
format

EOMONTH(<start_date>, <months>)- Returns the date in


datetime format of the last day of the month, before or after a
specified number of months.
DAX Formulas

Categories of DAX Formulas - Information

ISBLANK(<value>) - Checks whether a value is blank, and


returns TRUE or FALSE.
INFORMATION

ISNUMBER(<value>) - Checks whether a value is a number,


and returns TRUE or FALSE.

ISBLANK
ISNUMBER ISTEXT(<value>) - Checks whether a value is text, and
ISTEXT returns TRUE or FALSE.
ISNONTEXT
ISERROR
ISNONTEXT(<value>) - Checks if a value is not text (blank
cells are not text), and returns TRUE or FALSE.

ISERROR(<value>)- Checks whether a value is an error, and


returns TRUE or FALSE.

These are usually used in conjunction with conditional formulas


DAX Formulas

Categories of DAX Formulas - Filter


ALL( [<table> | <column>[, <column>[,
<column>[,…]]]] ) - Returns all the rows in a table, or all
FILTER the values in a column, ignoring any filters that might have
been applied.

CALCULATE(<expression>[, <filter1> [, <filter2>


[, …]]]) - Evaluates an expression in a modified filter
context.

ALL
CALCULATE CALCULATETABLE(<expression>[, <filter1> [,
CALCULATETABLE <filter2> [, …]]]) - Evaluates a table expression in a
FILTER modified filter context.
LOOKUPVALUE
FILTER(<table>,<filter>) - Returns a table that
represents a subset of another table or expression.

LOOKUPVALUE(<result_columnName>,<search_columnName>,<search_va
lue> [, <search2_columnName>, <search2_value>]…
[, <alternateResult>])- Returns the value for the row that
meets all criteria specified by one or more search conditions.
Calculated columns vs measures

Here we will cover the differences between calculated columns and measures and when to use
which.

Calculated column Measure

A calculated column is a column like any other, A measure is usually a calculation that works
created in the table. on an aggregated level basis.

Features: Features:
● Row by row calculation ● Calculated based on filters
● Stored in memory ● Not stored in memory or pre-calculated

When to use: When to use:


If the calculation is performed row-by-row If the calculation is an aggregation or will be
based on a combination of other columns affected by filter criteria in the report
Calculated columns vs measures

Here we will show where in Power BI we create columns and measures.

Calculated column Measure

To create a new column, first, select the To create a new measure, first, select the
Modeling tab from the top panel. Then select Modeling tab from the top panel. Then select
New Column. New Measure.

NB: Make sure you are currently in the correct


table when you create the column.
Creating calculated columns for IPL data

Here we will create the RCB_involved calculated column. The brief is as follows:

1. We need columns that indicate whether a team (let’s say Royal Challengers Bangalore, RCB) was
involved in a match and who their opposition was for that match ; because currently, a team can be
involved if they are either in the matches[team1] column or the matches[team2] column.

2. We also want to be able to determine whether the match was a home or away match.

Let’s start by creating a column called “RCB_involved” - which


indicates whether RCB is involved in the match (“1” if they
were, and “0” if they were not). We want to do this to filter out
all games that RCB was not involved in.

RCB_involved = IF(OR(matches[team1] = "Royal Challengers


Bangalore", matches[team2] = "Royal Challengers Bangalore"),
1 , 0)

The matches dataset should now look like the snippet on the
right.
Creating calculated columns for IPL data

Here we will create the RCB_opposition_team calculated column. The brief is as follows:

Let’s create a column called “RCB_opposition_team” - which is the name of the team playing against
RCB.
We want to do this in order to analyse performance against specific teams.

RCB_opposition_team = IF(matches[team1] = "Royal Challengers Bangalore", matches[team2],


IF(matches[team2] = "Royal Challengers Bangalore", matches[team1], "-"))

The matches dataset should now look like the snippet below.
Creating calculated columns for IPL data

Here we will create the RCB_venue calculated column. The brief is as follows:

Let’s create a column called “RCB_venue” - which is either “Home” or “Away”, depending on the stadium
name. RCB’s home stadium is “M Chinnaswamy Stadium”.

We want to do this in order to analyse RCB’s match performance away and at home.

RCB_venue = IF(matches[venue] = "M Chinnaswamy Stadium", "Home", "Away")

The matches dataset should now look like the snippet below.
Creating measures for IPL data

Here we will create some measures for team statistics. The brief is as follows:

We want to have an idea of team performance over time. An aggregation is required, therefore we need to
create a measure.

Let’s create a measure that calculates the match winning percentage given certain filters.

RCB Win % = CALCULATE(COUNTA(matches[winner]), matches[winner] = "Royal Challengers Bangalore") /


SUM(matches[RCB_involved])

In the calculation above we count how many matches were won and divide it by the total number of
matches played.

Before visualising, highlight the measure and head to Modeling and select “%” in the Formatting section.

The measure (with no filters applied) should look as follow on a card:


Creating measures for IPL data

Here we will create some measures for team statistics. The brief is as follows:

Let’s create a measure that calculates RCB’s run rate. Run rate is the number of runs divided the number of
overs (six balls in one over).

Every row in the deliveries dataset represents a ball, however, it contains some extras (balls that needed to be
re-bowled). We only want to count the number of legitimate deliveries (exclude the extra balls).

Run Rate =
SUM(deliveries[total_runs]) / CALCULATE(COUNT(deliveries[ball]), (deliveries[wide_runs] = 0),
(deliveries[noball_runs] = 0)) * 6

In the calculation above, we sum all the runs and divide it by the total number of legitimate balls bowled (i.e.
not a wide or no-ball), then multiply it by 6 to get the run rate per over.

The measure (with no filters applied) should look as follow on a card:


Creating measures for IPL data

Here we will create some interim measures for batting statistics. The brief is as follows:
We want to create some measures that will help us analyse batting performance. Good metrics are:

● Runs
● Strike Rate - runs per 100 balls
● Average - runs per dismissal

Looking at the metrics above, we need some interim measures - balls faced and number of dismissals.
We can write the following queries to create these interim measures:

Batsman Balls = CALCULATE(COUNT(deliveries[ball]), deliveries[wide_runs] = 0)

We don’t count the wides for balls faced, since they don’t count towards the batsman’s balls.

Batsman Wickets =
CALCULATE(COUNTA(deliveries[player_dismissed]), deliveries[player_dismissed] <> "-")

The measures (with no filters applied) should look as follow on a card:


Creating measures for IPL data

Creating batting statistics - continued

Now that the interim measures are created, we can create the batting indicators:

Batsman Runs = SUM(deliveries[batsman_runs])

Batsman Strike Rate = [Batsman Runs] / [Batsman Balls] * 100

Batting Average = [Batsman Runs] / [Batsman Wickets]

The measures (with no filters applied) should look as follow on a card:

See if you can create the bowling measures on your own!


Conclusion
In this train we have covered the following:

- Understand the purposes of DAX formulas, calculated columns and

measures

- Defining and identify different DAX formulas

- Define and differentiate between calculated columns and measures

- Apply DAX formulas and create calculated measures and columns


Appendix

Official Microsoft Documentation for Power BI


A Free PDF to Begin with DAX and Power BI

Additional Material
Quickstart with DAX in Power BI

You might also like