What Is M, Power Query and DAX?
What Is M, Power Query and DAX?
The Power Query Formula Language is a powerful query language optimized for building queries that
mashup data. It's a functional, case sensitive language similar to F#, which can be used with Power
Query in Excel, Get & Transform in Excel 2016, and Power BI Desktop.
DAX stands for Data Analysis expressions. DAX is the formula language used in Power Pivot and Power BI
Desktop. DAX uses functions to work on data that is stored in tables. Some DAX functions are identical
to Excel worksheet functions, but DAX has many more functions to summarize, slice and dice complex
data scenarios. DAX is also found in other offerings from Microsoft, such as Power Pivot and SSAS
Tabular.
DAX is used after your data has been imported into your data model.
Use DAX to create Custom Columns and Measures within a data model.
MDX
What
Multi-Dimensional Expressions (MDX) is the standard language defined by Microsoft to query
multidimensional cubes, such as those created with Microsoft SQL Server Analysis Services. MDX might
at first appear to be similar to SQL, however, MDX is a completely different language. Whereas SQL was
designed to query relational databases composed of tables where data is organized in terms of columns
and rows, MDX was designed to query OLAP cubes where data is organized in terms of measures,
dimensions, hierarchies, and levels.
When
MDX is used before your data import.
Where
MDX is used when querying SQL Server Analysis Services (SSAS), Excel when connecting to
multidimensional cubes and in Power BI Designer. In Power BI Designer, you can use MDX when using a
data source connector for a multidimensional cube such SSAS. Similar to how to use SQL above, from
within the connector interface, choose "Advanced Options" and there will be an optional area into
which you can paste MDX code.
Why
Use SQL to pre-filter and join your data before you start working with it. It will generally be more
efficient to perform a complex join or selection of columns using MDX versus in M or within the data
model.
How
To learn more about MDX, see Microsoft's Multidimensional Data Access documentation
By Harsha
By Harsha
Difficulty
3 - MDX is more difficult to wrap your head around than SQL and is somewhat of a unique language as
far as languages go.
DAX
Data Analysis Expression. We have following different DAX functions in Power BI.
Date and Time
Filter Functions
Information Functions
Logical Functions
Mathematical and Trigonometric Functions
Statistical Functions
Text Functions
Time Intelligence Functions
Text Functions
BLANK: using blank function, we can replace the specific values with the blanks.
Ex: IF(SUM('Filters'[Sale]) = 100, BLANK(), SUM('Filters'[Sale])), this formula will replace 100 with the
blank.
‘Filters’ is a Table, [Sales] is a field in the table.
CONCATENATE: to combine or joins two text strings into one text string. The joined items can be text,
numbers or Boolean values represented as text, or a combination of those items.
Ex: create a new column, CONCATENATE(Naani&" ", Haksha) (&” “ is to get the delimiter space)
By Harsha
By Harsha
Note: it accepts only 2 arguments (Ex: concatenate(“Naani” , “Haksha”), it accepts 255 arguments in Excel.
EXACT: compares 2 string and returns true or false, if they are same then returns true, if not returns
false.
Ex: New Columns, Exact(Haksha, Haksha) = True
Exact(Haksha, Asha) = False
FIND: will find the position of the first letter of the given string
Ex: find(“k”, “Haksha”) = 3
SUBSTITUDE: will replace the taken string with the given conditions.
Ex: create a new column, SUBSTITUDE([Harsha], “rsha”, “ksha”) = Haksha
REPLACE: will replace the characters from the beginning of the taken field.
Ex: REPLACE (“NaaniHarsha”, 1, 5, “Haksa”) = HaksaHarsha
Note: “NaaniHarsha” is taken string, 1 is starting character in the NaaniHaksa, 5 is it will replace 5
characters from starting character (N to i) (Naani will get replace with Haksa)
Note2: also we can replace 5 characters with less than 5 or more than 5.
REPT: it will repeat the give text values in the new column.
Ex: REPT(“Harsha”, 2) = HarshaHarsha
Filter Functions
ADDMISSINGITEMS:
CALCULATE: Calculate is only the single functions in DAX which is able to modify the filter context.
returns true if the conditions satisfy, if not returns false.
Ex: calculate(sum(Sales) > 1000) = Returns true if the value is more than 1000 else false.
Ex2: CALCULATE(SUM(Orders[Sales]), Orders[Sales] > 120000), returns the values which are more than
120000.
Ex3: CALCULATE(SUM(Orders[Sales]), Orders[State] = "Arizona"), it will return Arizona state values to all
the states.
By Harsha
By Harsha
Scenario: want to develop a report, Ex 1 transaction value is 1000, profit is 200 from that transaction,
like that, want to see all the transactions values, which are giving more 100 profit. For all the states
1. Create a table, select state, Sales and profit.
2. Create a new measure, name it as Sales value from more than 100 profit, write the following
formula,
CALCULATE(SUM(Orders[Sales]), FILTER(ALL(Orders[Profit]), Orders[Profit] > 100))
3. It will show the state wise sales values. Which are giving more than 100 profit.
ALL: returns all the rows in the table or a column, it will ignore all the filters at Visual level.
Logical Functions
SWITCH: evaluates an expression against a list of values and returns one of multiple result expressions.
Ex: Create a new column,
SWITCH(Orders[Region],
“South”, “PST”,
“East”, “EST”,
“Central”, “CST”,
“West”, “MST”,
Orders[Region])
Note: all the result expression and else expression must be same data types, it will not allow multiple
data types in the formula.
Numeric Functions
Note: handing the nulls and blanks in divide function, we can use zero, EX: DIVIDE (Sum(Actual),
SUM(Target), 0) it replaces with the 0.
Note2: need to use an aggregation function, when we use DIVIDE, if the column is not an aggregated.
SUMX: will work on two fields, 'A' is a table name. to make use SUMX, first we need to take the table
name in the DAX formula.
Ex: SUMX ('A', 'A'[Price]*'A'[Quantity])
By Harsha
By Harsha
Number.RoundDown: returns highest integer less than or equal to number value
Ex: Number.RoundDown(25.278) = 25
Ex2: Number.Round.Down(-25.678) = -26
Visualizations or Tiles: visualization is a visual represent of the data or a smallest developed objective in
the Power BI or a single visualization in a Page (Ex: Bar chart, Pie chart, Doughnut)
Datasets: is a collection of data that Power BI used to created or uses to create visualization for analysis.
Like SQL Server, MySQL, Oracle, salesforce, Hadoop.
Parameters
By Harsha
By Harsha
1. Click on Enter Data from the Home menu bar, it will open the Create Table window, Rename the
Column1 as Measures Parameter, create 2nd column name it as Index.
2. Under the Measures Parameter column enter the required measure names, Sales, Profit and
Quantity, In the Index column give the 1, 2 and 3.
Note: Index is to get variables in sequence, If we assign 1 to Sales, in the list Sales is going be on top.
3. Bottom of the Create Table window, change the Table name Table 1 to Parameters, click on
load.
4. Now, we can see a new table with 2 columns which we have created in 1st step.
5. We need to create 3 new measures to get Sales, Profit and Quantity variables. To create a new
measure, click on the New Measure from Home menu and select New Measure, name it as Dyn
Sales, write the following formula, = SUM(Orders[Sales])
6. Repeat the same for Profit and Quantity.
7. Create a new measure field to assign sales, Profit and Quantity to Parameter, name it as
Dynamic Measures and write the following formula,
(SWITCH(TRUE(),
VALUES('Measures Parameter'[Dyn Measure]) = "Sales", [Dyn Sales],
VALUES('Measures Parameter'[Dyn Measure]) = "Profit", [Dyn Profit],
VALUES('Measures Parameter'[Dyn Measure]) = "Quantity", [Dyn Quantity],
0)
8. select clustered bar chart from the visualization and select State, Dynamic Measure measure filed,
select filter from the visualization and drop Measure Parameter into the filter.
Ranking / Index
By Harsha
By Harsha
Below is the table, table name is Project Comp.
Requirement is to get latest Submitted Date by Emp ID wise and Project wise. Ex: 101, FnA, 9/7/2016
6:20:25 PM is the latest Submitted date for Emp no 101 for FnA Project.
3. Hit enter, it will create a new table, select a table visualization, select Emp ID, Project, Date from
the newly create table. Below is the results, it will give.
By Harsha
By Harsha
Note: Rank is a Name of the new table, SUMMARIZE is function, ‘Project Comp’ is table name.
Note2: we should not use any interval, when you want find out the max date from the date field.
KPI’s creation
1. Select new measure, name it as KPI, write the following formula
Switch(true(),
sum(Sales) < 15000, “High”,
sum(Sales) < 10000, “Medium”,
sum(Sales) < 5000, “Low”,
“Very High”)
2. Select table from the list of visualization, select customer, KPI calculated field, Sales.
Note: we need to write the formula in the hierarchy mode from highest value to lowest value.
Dates
Month = month([Date]), month is a function to return month. It returns the month number
Ex: month ([1/25/2016]) = 1
Quarter = INT([month] +2)/3, we need to derive the month from the date field to get the quarter. INT
function is to return the date value as an integer.
Ex: INT ([1/25/2016] +2) / 3 = 1 (1st quarter)
Month Name = format([Date], “mmmm”), format will give you the given interval name, “mmmm” to get
the month name, if we want month abbreviation use “MMM”
Ex: format ([1/25/2016], “mmmm”) = ‘January’
By Harsha
By Harsha
Day of week = format([Date], “ddd”)
Ex: format ([1/25/2016], “ddd”) = “Mon”
Week = we don’t have any function, which derives the week from the date field, we need to create 2
new columns, one is to find the day of week and another one to find out the position of the day in the
data set, write a formula that returns values from the date field as an integer.
INT ([Date])
Then we need to find out the first Sunday in the data set. Then write the following formula to write
extract the week number, write the following.
INT = ([Date] - 200) / 7
Note: here Date is a Date field, 200 is the position of the first Sunday in the data set, then dividing it with
the 7 (days in a week)
DAY: it returns the day of the taken date fields, a number from 1 to 31.
Ex: create a new column, DAY (“September 25, 2017”) = 25
Ex2: IF (DAY([Order Date]) = 25, “Hakshaya”, “ “), it returns “Hakshaya”, the day each order date is 25 of
any month.
HOUR: it will return hour as a number from 0 to 23, it will take the input as time (time stamp)
Ex: create a new column, HOUT(“September 25, 2017 7:00 PM”) = 19
CALENDER: we can return a table with a single column with range of dates, the range of dates is from
specified start and end date.
We can’t use Calendar function as normal DAX functions,
To use calendar or to generate range of dates, follow the below steps
1. Click on modelling tab, select new table, it will open a new formula tab,
2. Write the following formula, CALENDAR (“1/1/2017”, “1/1/2020”), hit enter
it will generate a table with a field called Date, that contains the dates range from 1/1/2017 to
1/1/2020.
EDATE: it returns the date that’s is indicated number of months based on the taken date.
Ex: Create a new column, EDATE (“September 25, 2017”, 1) = October 25, 2017
Ex2: EDATE (“September 25, 2017”, 2) = November 25, 2017
It will add given number of months to the taken date.
We use EMONTH and EOMONTH functions mostly in the insurance domain to calculate maturity dates
and in all the domains where we need to find the Amount due date or to define the payment terms.
By Harsha
By Harsha
Note: Ex2 returns October 31, 2017, because the month’s argument is 1. It will add 1 month to
September.
Note2: if the date is not in the date format then EOMONTH through an error. To avoid unexpected
results, convert the string or number fields to date.
Date difference
1. Select create new column, name it as Days Difference, write the following formula
DATEDIFF(Orders[Order Date], Orders[Ship Date], day)
2. Select table from the list of visualizations, select customer name, order date, ship date and Days
diff calculated filed.
Note: date shows in the hierarchy mode, if we want get exact date, under Values, click on the Order
date drop down and Order data instead of Date hierarchy.
It shows weekday name, month name and year. If we want I numbers, select the order date in the table,
click on the modeling in the menu bar, from the list of format, select the required format.
OR
By Harsha
By Harsha
Day Level difference
SUM(Orders[Sales]) - CALCULATE(SUM(Orders[Sales]), PREVIOUSDAY(Orders[Order Date].[Date]))
How to extract month name from the order date or date field?
1. Create new column name it as Month, write the below power query
Orders[Order Date].[Month]
Note: Orders is tables name, Order Date is Date field, . (Dot) Month is the functions, which helps to
derive the month name.
How to extract the Month and Year from the date field?
1. Create a new a column, name it as Mon & Year, write the following formula
FORMAT('Date'[Date].[Date],"MMM") &" "& FORMAT('Date'[Date].[Date], "YY")
Note: Format is DAX function, ‘Date’ is table name, [Date].[Date] is a filed name which is coming for the
Date table, “MMM” is syntax to get month name, “YY” is to get Year.
By Harsha
By Harsha
How to find the recent transaction of every customer?
1. Create a new column, name is=t as Max Date, write the following formula
CALCULATE(MAX(Orders[Order Date]), FILTER(Orders,[Customer
Name]=EARLIER(Orders[Customer Name])))
Note: Orders is your table, Filter will perform on the customer Name field by taking which customers
comes from the Earlier. (Earlier is DAX function), it will give the output by performing group by on
Customer Name and the recent date.
2. Select table from the visualization, select Customer Name and Max Date to see the recent
transaction data.
3. We can also represent old transitions as Old Transaction, Recent one as New, for the create a
New column, Name is as Is Recent, write the following DAX,
= IF(Orders[Order Date] = Orders[Max Date], "Recent", "Late")
4. Select new table, select customer name, order date and Is Recent. To see the results.
Note: to find the Unicode for difference arrows and icons refer http://unicodelookup.com/#triangle/1
Note2: with the help of UNICODE we can get the up and down arrows. 9650 is to get up arrow and 9660
is to get down arrow.
By Harsha
By Harsha
How to get Central Region Profit using calculations?
1. Create a new measure, name it as Central Region Profit, write the following formula,
CALCULATE(SUM(Orders[Profit]), FILTER(Orders, Orders[Region] = "Central"))
How to extract Year, Quarter, Month and day from the date field?
1. Right click on the table from the fields, select edit query.
2. Select the required table under the Queries.
3. Select the field, from which field we need to extract the date parts, select the Order date (just
select the field)
4. Click on Add Column in the menu bar, under the Date, click on the drop down, in list we can see
all the date parts, select the required date parts. It will create a new column with the selected
interval name at the end of the table.
Grouping
1. Right click on the region field and select new group, it will open a create group window.
2. Give a name to group as region group, (in the field box, we can on which field we are creating a
group), under the Ungrouped values, select the required regions to be grouped, select Central,
East and South, click on the Group.
3. Selected regions will move to the Group and members box. Give it a required name as 3 regions.
By Harsha
By Harsha
4. Now, we can see central, east and south in 3 regions group, if we select Include Other group,
West region will appear as Others, if we don’t select Include Other group west will be shows as
west.
if [State Name] = "WASHINGTON, D. C." then "DC" else if[NewColumn.State] = null then [State Name]
else [NewColumn.State]
=if [State Name] = "WASHINGTON, D. C." then "DC" else if[State2] = 0 then [State Name] else [State2]
Joins
What is flattening?
A. Some BI products require you to collapse two or multiple tables into a single table before
loading into the product, this is called flattening.
Note: But in Power BI most of the time it automatically figure out the relationship between the tables
you bring in. it will find the relationship either at the time you load the tables into the model or the time
you go to use them. However there will some cases where we can’t find the relationship and in those
cases you will need to create the relationship manually.
Note: when we connect to any flat file, Power BI might not detect the relationship automatically.
Dept ID Table
By Harsha
By Harsha
1. Connect to these tables, Dept Table headers showing as Column 1 and column 2, to change it to
the actual field name, Right click on the Dept ID table, select Edit Query, in the Menu Bar, from
the List of Home, click on Use First Row as Header drop down, select Use First Row as Header.
Click on close & Apply.
Here, we have to show Project wise Hours in the Reports (we have to take Project field form the Dept ID,
the field name is Working On)
2. Select Table from the visualization, select Working On field filed from Dept ID table and Hours
from the Dept Details table.
If we validate the data, the results are not matching with the data. Hours are same for all the projects,
it’s due to the joining condition. Here Joining didn’t performe. We need to perform the joining manually.
To do that follow the below step.
3. Click on the Home in the menu bar, click on the Edit relationship, it will open the Manage
Relationship window.
By Harsha
By Harsha
4. Click on New in the Manage relationship window in the bottom, then it will open the Create
Relationship window. Select Dept ID Table from under the list of select tables and columns that
relate to one other.
5. Select the Dept Details from the list in the 2nd table pane,
filters
By Harsha
By Harsha
3. Under the queries select the Orders table.
4. From Home ribben, click on choose columns, it will open a choose columns window with all the
available fields in the select data set, just unselect the fields which are not required. Click on ok.
5. Click on apply and ok, check the fields either it’s unselect or not.
Note: we can achieve the same by making use of the Remove columns also but using Remove columns,
we need to select the not required field and select Remove columns.
In remove columns we have 2 options, Remove columns option will remove the selected column and
Remove other columns option will remove all the fields which are not selected.
By Harsha
By Harsha
By Harsha