0% found this document useful (0 votes)
3 views18 pages

Introduction to Data Analysis Expression( power bi)

Uploaded by

vermaratan12911
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views18 pages

Introduction to Data Analysis Expression( power bi)

Uploaded by

vermaratan12911
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Introduction to Data Analysis Expression (DAX)

Data Analysis Expression (DAX) is a powerful formula language used in


Microsoft Power BI, Excel, and SQL Server Analysis Services (SSAS) to perform
calculations and data analysis. DAX enables users to create custom calculations,
manipulate data, and generate insights from datasets. While it may appear similar
to Excel formulas, DAX is specifically designed for working with data models,
making it more efficient for largescale data analysis.

Importance of DAX
Powerful Calculations: DAX allows users to create calculated columns,
measures, and tables to perform advanced analytics directly within Power BI and
Excel.
Time Intelligence: DAX provides builtin functions for handling dates and times,
making it possible to easily create timebased calculations (e.g., yearoveryear
growth, cumulative totals).
Modeling: DAX helps define relationships and hierarchies, which are essential
for proper data modeling and creating interactive reports.
Optimization: It allows for efficient data retrieval, enhancing performance when
working with large datasets by performing calculations on the data model rather
than in a report.

Data Types in DAX


DAX supports several data types, which can be broadly categorized as follows:
1. Numeric Types:
Integer: Whole numbers (positive or negative).
Decimal: Numbers with a fractional component (e.g., 12.34).
2. Boolean: Represents logical values (`TRUE` or `FALSE`).
3. Text (String): Alphanumeric values (e.g., "Product A").
4. Date/Time: Used to represent dates and times, enabling timebased
calculations.
5. Currency: Specialized for handling monetary values.
6. Blanks: Represents missing or undefined data.

DAX Calculations
DAX calculations come in the form of:
Calculated Columns: These are values that are calculated row by row in a table.
They are part of the data model and can be used in other calculations,
visualizations, or as part of a filter.
Example:
DAX
Total Sales = Sales[Quantity] * Sales[Unit Price]
Measures: These are dynamic calculations that are evaluated based on the
context of the data in the report (e.g., a filter, slicer, or other visuals).
Example:
DAX
Total Sales = SUM(Sales[Amount])
Calculated Tables: These create new tables from existing data, based on a
calculation or expression.
Example:
DAX
Top Customers =
FILTER(Sales, Sales[Amount] > 1000)
DAX Syntax
DAX follows a specific syntax for its formulas:
Functions: DAX includes numerous functions like `SUM()`, `AVERAGE()`,
`IF()`, `CALCULATE()`, `FILTER()`, and `DATE()`.
Identifiers: These refer to tables, columns, and measures within the data model
(e.g., `Sales[Amount]`).
Operators: DAX supports mathematical, comparison, logical, and text
operators.
Here’s an example of a basic DAX formula:
Total Sales = SUM(Sales[Amount]) + SUM(Sales[Discount])

DAX Operators
Operators are used to perform operations between values in DAX. Some key
categories are:
1. Mathematical Operators:
`+` (addition), `` (subtraction), `*` (multiplication), `/` (division), `^`
(exponentiation).
2. Comparison Operators:
`=` (equal to), `<>` (not equal to), `>` (greater than), `<` (less than), `>=`
(greater than or equal to), `<=` (less than or equal to).
3. Logical Operators:
`AND`, `OR`, `NOT` (used in conditions to combine or negate expressions).
4. Text Operators:
`&` (concatenation) used to combine text strings.
5. Other Operators:
`IN` (used to check if a value is within a specified list), `ISBLANK()` (checks
if a value is blank).

Example of DAX Syntax Using Operators


Discounted Sales =
IF(Sales[Amount] > 1000, Sales[Amount] * 0.9, Sales[Amount])
This formula checks if the sales amount is greater than 1000 and applies a 10%
discount.

Conclusion
DAX is a powerful language for data modeling and analysis within Power BI and
Excel. Its ability to perform sophisticated calculations and work with large
datasets makes it indispensable for professionals working in data analysis,
business intelligence, and reporting. By understanding DAX's syntax, data types,
and operators, users can harness its full potential to create complex data models
and meaningful insights.

There are two primary data types in Power BI DAX functions:

• Numeric: numeric data types include decimals, currency values, integers,


etc.

• Non-numeric: it consists of strings, binary objects, etc.

Check out this Power BI Tutorial to learn more about the topic.

DAX Formula – Syntax

Breaking down a statement into individual elements helps you understand any
language with ease. It’s important that your study the syntax of these expressions
and be able to create the new ones as per the requirements. Consider the image
shown below as an example of the DAX depression:

It represents a custom formula used to create new columns by multiplying the


values of the other two columns. Let’s break it down and understand what each
element does:

• Test Column: Name of the new measure


• (=) Sign: Indicates the starting of your DAX formula

• [Units Sold] and [Manufacturing Price]= These two are the arguments or
columns whose values are used to generate the output.

• (*): The ‘*’ operator multiplies the values of the two-column variables.

• [Segment]= Segment represents the classification of the corresponding


formula. Unlike regular columns, the calculated columns are necessary
to have at least one.

Importance of DAX in Power BI

It’s important to learn the DAX functions in Power BI as they help you implement
the functionalities like data transformation and visualization. With basic
knowledge of the Power BI interface, you can create decent reports and share
them online. However, for calculation and dimensional analysis, you need to
know how Power BI DAX functions are carried out.

For example, you can calculate the growth percentage and visualize the growth
percentage in different regions of a country to compare the data over the years.
DAX in Power BI helps a designer create new measures, which in turn helps a
business to identify the problems and find appropriate solutions.

DAX Functions

DAX Functions in Power BI are the predefined formulas used to calculate the
arguments in a function, executed in a particular order. These arguments could be
numbers, constants, texts, another function or formula, and logical values such as
True or False. The functions perform particular operations on one or more
arguments in a DAX formula. Below are the key points of DAX functions:
• DAX functions in Power BI will never refer to individual values, they
always refer to a complete field, column, or table. However, you have to
create filters inside the DAX formula, if you want to use DAX functions
on individual values.

• DAX functions can also be applied for separate rows without any filters.
The calculations can be applied based on the context of each row.

• DAX uses the time intelligence function to calculate the time and date
ranges. We’ll discuss these functions below in detail.

• These functions can sometimes return the entire table, which can be used
as an input for other DAX functions in Power BI. However, the user
cannot display these output tables returned by the functions.

Types of DAX Functions

Following are the Power BI DAX functions with examples to carry out the
calculations:

Date and Time Functions

Similar to Excel, these functions are used to calculate the date and periods in the
DateTime format. Below are some of the Date and time functions with their
syntax:

DAX CALENDAR function:

The start-date and end-date arguments could be any DateTime value. It returns
the table of a single column with a set of dates.
Syntax:
CALENDAR(<StartDate>,<EndDate>)
Example:
COUTDAYS(CALENDAR(DATE(2020,4,1), DATE(2020,6,5))) //returns 65

DAX DATEDIFF function:

The DATEDiff function calculates the difference between two dates and returns
it in terms of interval boundaries given specified by the user.
Syntax:
DATEDIFF(<StartDate>,<EndDate>,<Interval>)
Example:
DATEDIFF(DATE(2020,1,1), DATE(2020,1,31), HOUR) //returns 720
DATEDIFF(DATE(2020,1,1), DATE(2020,3,31), DAYS) //returns 90
DATEDIFF(DATE(2020,1,1), DATE(2020,4,31), MONTH) //returns 3

DAX NOW function:

The function returns the current DateTime value in the standard format.
Syntax:
NOW()
Example:
HOUR(NOW()) //returns 12:00:00AM

DAX DATEVALUE function:

Converts the given date into a text-to-date-time format.


Syntax:
DATEVALUE(<DateText>)
Example:
DATEVALUE(“1/4/2020”) //returns
DATEVALUE(“20-3-2020”) //returns 1/20/3/2020 12:00:00AM
DATEVALUE(“15-Jan-2020”) //returns15/1/2020 12:00:00 AM

Time Intelligence Functions


The time intelligence functions allow the user to calculate the time values over a
fixed period o time such as weeks, months, quarters, and years. These functions
are mainly used to calculate aggregation, per data manipulation, and Business
Intelligence with the table of dates as input.

DAX DATEADD function

Returns a table with the columns of dates shifted, either forward or backward,
based on the specified intervals of time.
Syntax:
DATEADD(<Dates>, <Number_of_Intervals>, <Intervals>)
Example:
DATEADD(ProductInventory[InventoryDate],1,YEAR)

DAX DATESBETWEEN function

The return table contains a column of dates between the start date and the end
date.
Syntax:
DATESBETWEEN(<Dates>, <StartDate>, <EndState>)
Example:
CALCULATE(SUM(Sales([Sales Amount]), DATESBETWEEN(Sales[Date],
Date(2020,1,1), Date(2020,3,31)))

DAX LAST DATE function

returns the last date of the modifications done in the date columns. For example,
the command mentioned below will return the last date when a sale was made.
Syntax:
LASTDATE(<Dates>)
Example:
LASTDATE(Sales[Date])

DAX ENDOFYEAR function


Syntax:
ENDOFYEAR(<DATES>, [<Year_End_Date>])
Example:
ENDOFYEAR(Start[Date])

Logical Functions

DAX logical functions are used to perform logical operations and return them as
either True or False. Below are the DAX logical functions:

DAX AND function

The AND function checks if both arguments are True or False. It will return True
if only if both arguments are true, otherwise it will return False.
Syntax:
AND(<argument1>, argument2)
Example:
AND([Country]=”USA”,[Medal]=”Gold”)

DAX OR function

The function will return True if at least one or both the arguments is True,
otherwise False.
Syntax:
OR(<argument1>,<argument2>)
Example:
OR([Medal Count]<100, [Count of Sports]>100)

DAX IF function:

It checks the first argument given in the statement. The function returns the first
value if the condition is True and returns the second if the condition is False.
Syntax:
IF(<condition>, <first_value>, <second_value>)
Example:
IF([Country]=”India”,1,0)

DAX SWITCH function:

The function evaluates arguments and returns one of the values listed against it.
Syntax:
SWITCH(<argument>, <value>, <result>, <value>, <result>, <value>, <result>,
<value>, <result>….,[<Else>])
Example:
SWITCH([Weekdays], 0, ”Monday”, 1, ”Tuesday”, 2, ”Wednesday”, 3,
”Thursday”, 4, ”Friday”, 5, ”Saturday”, 6, ”Sunday”, ”Unknown”)

Mathematical and Trigonometric Functions

These functions are very similar to the mathematical and trigonometric functions
of Microsoft Excel. They are used to perform all sorts of calculations in Power
BI. Some of the popular mathematical and trigonometry functions are:

DAX ABS function

The function returns the absolute value of a given number, meaning it will remove
the sign from the number.
Syntax:
ABS(<number>)
Example:
ABS(-6) //returns 6

DAX CURRENCY function

Syntax:
CURRENCY(<value>)
Example:
CURRENCY(6.0) //returns 6
DAX SQRT function:

SQRT function returns the square root of a given number


Syntax:
SQRT(<number>)
Example:
SQRT(36) //returns 6
SQRT(625) //returns 25

DAX LOG10 function:

For a positive number, the function returns the value in base 10 logarithmic.
Syntax:
LOG10(<Value>)
Example:
LOG10(10) //returns 1

Statistical Functions

The statistical functions carry out the DAX expressions used in statistical models
and aggregations. Some of these functions with their syntax and example are
listed below:

DAX BETA.DIST function

The function performs beta distribution and is used to show the variation in the
percentage across the sample.
Syntax:
BETA.DIST(x, Alpha, Beta, Cumulative, [P,[Q]])
Here, x is the value between the lower bound P and upper bound Q, alpha and
beta are the parameters for the distribution, and cumulative determines the form
of the function.
Example:
BETA.DIST(0,5,9,10), TRUE(), 0,1) //returns 0.592735290527344

DAX CONFIDENCE.NORM function:

The function returns the confidence interval of the given sample.


Syntax:
CONFIDENCE.NORM(alpha, deviation, size)
Example:
CONFIDENCE.NORM(0.05, 2.5, 50) //returns 0.692951912174839

GEOMEAN:

The function returns the geometric mean of the given column at decimal places.
Syntax:
GEOMEAN(<column_name>)
Example:
GEOMEAN(Sales[Sales_Amount])

Text Functions

Text functions allow the user to work with the strings in tables and columns. You
can get a substring, perform different operations like string concatenation. Some
of these functions are listed below:

DAX CONCATENATE function

Combine two strings into one string.


Syntax:
CONCATENATE(<string1>,<string2>)
Example:
CONCATENATE(“Dwayne”,” Johnson”) //return Dwayne Johnson
DAX EXACT function:

EXACT is a case-sensitive function that compares two given strings. The


function returns True if they are exactly the same, otherwise False.
Syntax:
EXACT(<string1>,<string2>)
Example:
EXACT(Results[Sport],[Sport])

DAX FIND function:

The function search for the given string and returns the starting position of the
string.
Syntax:
FIND(<find_text>, <within_text>, [<StartingValue>], [DefaultValue])
Example:
FIND([ProductName], [Description], , BLANK())

DAX SUBSTITUTE function:

The function replaces the selected text with a new one in a given string.
Syntax:
SUBSTITUTE(<string>,<old_text>,<new_text>)
Example:
SUBSTITUTE([Product], “Hairgel”,”Hairmask”)

DAX Calculation Types

DAX in Power BI has two types of calculations or formulas used to generate a


resultant value from the input values. These are:

Calculated Columns:
Calculated columns are used to merge new columns into existing ones with filters.
These columns can be created from the Modeling tab in Power BI Desktop, where
new columns can be created by entering their names and formula.

Calculated Measures:

Measures enable the user to create fields with aggregate values like average, ratio,
percentage, etc. Just like calculated columns, the measures are created from the
modeling tab of Power BI Desktop.

Now that you’ve learned about the Syntax of DAX formulas, let’s discuss the
DAX functions and their types.

Creating Measures in Power BI

You can create two types of Measures or DAX formulas in Power BI Desktops:
Automatic and user-specified. Let’s see how to create each of these DAX
expressions in Power BI:

Automatic Measures

Follow the steps mentioned below to let Power BI Desktop create the measures
automatically:

• On Power BI Desktop, go to File>> Open Report and locate the report in


your system storage.

• Load the datasheet and move the pointer towards the Fields panel on the
right side.
• Now, drag-n-drop any field like ‘SalesAmount’ to the reports panel. You
can also click on the check box of the SalesAmount field.

• New visualization in the form of a column chart will appear showing the
total sum of all the values of the SalesAmount column.
• Instead of showing every value in the column of more than two million
rows, Power BI automatically creates an aggregate for these columns.
Every field with a Sigma icon has numerical values.

• Depending on the measure you can create or change the chart aggregation
from the Value area of the visualizations panel.

• Create your Measure


Let’s assume you need a measure that analyzes net sales from total sales
amounts. For that, create a measure that calculates the sum of the
ReturnAmount and DiscountAmount and subtracts it from the aggregate
sum of SalesAmount. So, follow the steps given below:

o Right-click on the Sales table and click on the ‘NewMeasure’


option.

• This measure will be saved in the Visualization panel, where you can add
more Fields and customize them.
• Now, select the Sales table, right-click on the Measure and a dialog will
appear. You can edit the measure name to make it more identifiable.

• Following that, enter the formula and the fields you want to include in the
measure at the top of the interface.
• The expressions will appear inside the parenthesis where you can enter the
column names.

• The syntax of your formula should look something like this:

• Click on the Enter button, then Commit to validate and complete the
formula.

• At last, you can apply the measure and calculate the Net Sales by adding
the values to the visualization panel.

You might also like