DAX Formulas, Columns and Measures
DAX Formulas, Columns and Measures
Measures
measures
Topics to be covered
Power BI offers the functionality to employ commands and queries for data analysis
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
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
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
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
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
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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:
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] <> "-")
Now that the interim measures are created, we can create the batting indicators:
measures
Additional Material
Quickstart with DAX in Power BI