Introduction to Data Analysis Expression( power bi)
Introduction to Data Analysis Expression( power bi)
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.
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).
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.
Check out this Power BI Tutorial to learn more about the topic.
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:
• [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.
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.
Following are the Power BI DAX functions with examples to carry out the
calculations:
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:
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
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
The function returns the current DateTime value in the standard format.
Syntax:
NOW()
Example:
HOUR(NOW()) //returns 12:00:00AM
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)
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)))
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])
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:
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)
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”)
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:
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
Syntax:
CURRENCY(<value>)
Example:
CURRENCY(6.0) //returns 6
DAX SQRT 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:
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
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:
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())
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”)
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.
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:
• 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.
• 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.
• 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.