77
POWER BI DESKTOP Contents GETTING DATA AND USING TRANFORM................................................. 1 CREATING A DATE TABLE.......................................................... 10 TIDYING UP THE QUERIES LIST.................................................... 12 ADVANCED TRANSFORMATIONS....................................................... 13 DATA MODELLING................................................................. 18 RELATIONSHIPS BETWEEN TABLES................................................... 21 USING DAX IN POWER BI DESKTOP.................................................. 26 RELATED TABLES................................................................. 38 ADVANCED DAX................................................................... 43 Organizing DAX Measures.......................................................46 Aggregation Functions Allowed.................................................59

GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

POWER BI DESKTOPContents

GETTING DATA AND USING TRANFORM...............................................................................................................................1

CREATING A DATE TABLE.....................................................................................................................................................10

TIDYING UP THE QUERIES LIST............................................................................................................................................12

ADVANCED TRANSFORMATIONS........................................................................................................................................13

DATA MODELLING...............................................................................................................................................................18

RELATIONSHIPS BETWEEN TABLES......................................................................................................................................21

USING DAX IN POWER BI DESKTOP.....................................................................................................................................26

RELATED TABLES.................................................................................................................................................................38

ADVANCED DAX..................................................................................................................................................................43

Organizing DAX Measures...............................................................................................................................................46

Aggregation Functions Allowed.......................................................................................................................................59

Page 2: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Power BI DesktopGETTING DATA AND USING TRANFORMWhen you download and run Power BI Desktop, you are presented with the Power BI Desktop opening screen, as below, offering a lot of options and it can be difficult to know where to go, what to do, or even, where to start.

The easiest way to get started is to close this screen. Click the X at the top right corner of the screen, and you will see an Untitled – Power BI Desktop screen as below.

1 | P a g e

x

Page 3: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The main reason for using Power BI is to get data from a source, could be Excel, or a Text file, a database, an SQL server etc. then use and manipulate that data to create Reports, Charts, Maps, Queries, New Tables etc. There are many options available under the Home tab in the data group that you could use, but it is much simpler and faster to click on Home tab and select the Transform data button in the Queries group, which opens the Untitled - Power Query Editor screen as below:

At this time, you have not loaded any data files into Power BI Desktop. On the left of the screen you can see Queries (0). This screen is much simpler than the previous screen and all it needs now is to get the data you are going to use and upload it into Power BI Desktop.

Click the New Source button in the Home tab’s New Query group

2 | P a g e

Page 4: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The Get Data dialog box will open. Select Excel as the file we require for this exercise is an Excel file, then click Connect.

Navigate to the folder containing the resource files supplied with this manual and open the file: ExcelToPBID and click Open. The Navigator window will open with the file name at the top of a list, this list contains the sheet names from the original Excel file.

3 | P a g e

Page 5: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

For the purpose of this exercise we only need to select the following tables by placing a tick to their left. Customer Data, Location_Data, Product_Data, Sales_2015, Sales_2016, Sales_2017 and Salespeople_Data. As you tick off each name, the actual Excel data displays on the right side of the screen. You can scroll down or across to check it out.

Click Ok and you will be returned to the Power Query Editor window.

4 | P a g e

Page 6: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Notice that on the left side of the screen you can see Queries (7), these being the ‘7 table’ names you selected when you clicked their names in the Navigator dialog box.

The main reason for working in the Power Query Editor window is that you have some very powerful tools to help to you manipulate the raw data you have brought from the Excel. The Power Query Editor will help you to prepare the data to go into the Power BI model and create reports, charts, maps etc.

If you click on the Transform tab you will see some of the options available, some you might have already used in Excel, like Split Column, Transpose, Reverse Rows, Format etc. and will learn more about these later.

If you right-click on a column heading name you will see a pop-out list of options..

5 | P a g e

Page 7: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The first thing we will do is tidy up the original sheet names, now called Queries.

On the left side of the screen, right click on Customer Data and select Rename and name it to Customers. This is called a Transformation, meaning you are making changes to the data in Power BI Desktop, not to the original Excel file, which remains unchanged.

Rename the remaining queries as below:

Transformations can also be used to change the name of a column heading, making it easier to understand, especially if the name is ambiguous. On the left side of the screen, select the Products table. The table appears, right-click on Cost column name and rename it to Product Cost, as this is what it is and not just any cost value. The other column head names are fine.

Next you want to look at what columns you do not need. Click on the Locations table. To remove a single column, right-click it and select remove. We do not need the Area Code column, remove it. On the right side of the screen, under Applied Steps, you will see the last step you took:

The steps you are making in the table are being recorded. Your last step was that you removed a column. This list is a bit like the Undo used in other packages. When you click on the x next to Removed Columns, the column your removed i.e. Area Code will reappear back in the table, remove it again.

6 | P a g e

Page 8: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

To remove multiple columns, use the Ctrl key and select columns that are non-adjacent, (that is, separated by other columns). Still in Locations, we do not need the Median Income, or the Time Zone columns. First select Median Income, then hold down the Ctrl key and select and Time Zone column heading names. The selected columns become highlighted. Right click on one of them, select x Remove Columns.

To remove columns that area adjacent, i.e. next to one another in the table, select the first column head name, then use the Shift key and select the last column head name. For example, in our Locations query we no longer need Households, Land Area or Water Area.

Select the first column name to be removed, Households, then hold the Shift key down, select Water Area, right-click close to a highlighted column, select x Remove Columns.

These simple changes, or transformations are now completed for the Locations query. Select the Products query and change the column name Cost to Product Cost

Each of the Sales years in the Queries have been given the wrong year in the original Excel worksheets. Select Sales 2015, check the dates in the Purchase Date column. We will rename them, so they read correctly.

Simply right click on each name and rename each one as on the right.

Each of these queries (tables) have the same structure, they just contain the three different time frames and we do not want to work with three separate tables, so we will use the Append Queries function to append them into one query (table).

Select the Sales_2018 query. From the Home tab select Append Queries from the Combine group. The Append dialog box appears as below. Select Three or more tables. Add the Sales_2019 and Sales_2020 tables

Click OK.

7 | P a g e

Page 9: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

On the right under APPLIED STEPS, you will

see Appended Query. On the left select Sales_2018

Select Purchase Date column head. Click on theFilter arrow on the right and select Descending Order. You will see the year is 2020 and if you scroll down the last year will be 2018. So now all three tables have been appended into one, which we will rename to a more identifiable name.

Right click on Sales_2018 query and rename it to Sales.

We still need the other 2 tables data when we refresh the data, but we do not need them to be loaded every time, as this would result in duplication. Click on Sales_2019 and remove the tick from Enable Load, click Continue for the warning message, then do the same for Sales_2020. Their names will be italicized, indicating that they will not be loaded upon refresh.

When data is imported in Power BI Desktop, it is good to check the data types in the columns, although Power BI Desktop is usually good at bringing in the data correctly with indicators on the left side. For example, the column name will have ABC to its leftindicating that the data is text, or 123 to indicate it is numeric and dates have a small Calendar icon that expands for date searching and selecting.

If data has been incorrectly entered and saved when it was originally created and it imports into Power BI Desktop the same way, for example, an Order Number was defined as text, you can easily change in in Power BI Desktop by right clicking the column heading and selecting Change Type and you get to select number, date, percentages etc. for the type.

Every step you make is recorded under Applied Steps on the right, if you decide to change your mind, click on the small x on the left of the step you want to delete and it will revert to the previous step.

There are many simple actions you can take, using the Transform tab. Click the Customers Query and click in Customer Name. The first and surnames are in the one column. It could be easier for sorting if the names were in separate columns.

8 | P a g e

Page 10: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Click on the column name heading Customer Name and from the Transform tab click Split Column in the Text Column group. Select By Delimiter and the dialog box will appear as below. Check that Space is displayed in the Select or enter delimiter and Each occurrence of the delimiter and click OK.

You will now have two columns and you could rename them as below.

On the right side of the screen under Applied Steps, click the small x to the left of all steps back to Changed Type

There are other things you can try, like converting to Upper Case, Capitalize each word etc. These are simple commands but will help you to Transform (called Shaping) your data into exactly you want it to be.

CREATING A DATE TABLE

Now that we have cleaned up the data, we will create a Date Table. If you have dates in any of your tables, it is recommended that you create a separate date table, that dates in your tables can relate to, that way helping to keep query dates accurate.

9 | P a g e

Page 11: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

There is a code created for you that will create the date table, this code is included in the training materials. Open the file: Power BI Date Table code

The code appears on the screen, select all the code (you could use Ctrl A) and copy it.

(Note: you can copy and use this code in any Power BI Desktop project)

Go back to Power BI Desktop and from the Ribbon, from the Home tab, select New Source in the Query group, Select Blank Query from the list of options. Staying in the Home tab, from the Query group, select Advanced Editor.

The Advanced Editor box will open, if any code is displayed, delete it and paste the code you copied into it. Click Done. On the left side of the screen you will see Query 1 has been added to the Queries list, now numbering 8. This code is specifically designed for date and creates the following Date Parameters box, very useful for start and end dates which it is connected to dates in your tables.

10 | P a g e

let fnDateTable = (StartDate as date, EndDate as date, FYStartMonth as number) as table => let DayCount = Duration.Days(Duration.From(EndDate - StartDate)), Source = List.Dates(StartDate,DayCount,#duration(1,0,0,0)), TableFromList = Table.FromList(Source, Splitter.SplitByNothing()), ChangedType = Table.TransformColumnTypes(TableFromList,{{"Column1", type date}}), RenamedColumns = Table.RenameColumns(ChangedType,{{"Column1", "Date"}}), InsertYear = Table.AddColumn(RenamedColumns, "Year", each Date.Year([Date]),type text), InsertYearNumber = Table.AddColumn(RenamedColumns, "YearNumber", each Date.Year([Date])), InsertQuarter = Table.AddColumn(InsertYear, "QuarterOfYear", each Date.QuarterOfYear([Date])), InsertMonth = Table.AddColumn(InsertQuarter, "MonthOfYear", each Date.Month([Date]), type text), InsertDay = Table.AddColumn(InsertMonth, "DayOfMonth", each Date.Day([Date])), InsertDayInt = Table.AddColumn(InsertDay, "DateInt", each [Year] * 10000 + [MonthOfYear] * 100 + [DayOfMonth]), InsertMonthName = Table.AddColumn(InsertDayInt, "MonthName", each Date.ToText([Date], "MMMM"), type text), InsertCalendarMonth = Table.AddColumn(InsertMonthName, "MonthInCalendar", each (try(Text.Range([MonthName],0,3)) otherwise [MonthName]) & " " & Number.ToText([Year])), InsertCalendarQtr = Table.AddColumn(InsertCalendarMonth, "QuarterInCalendar", each "Q" & Number.ToText([QuarterOfYear]) & " " & Number.ToText([Year])), InsertDayWeek = Table.AddColumn(InsertCalendarQtr, "DayInWeek", each Date.DayOfWeek([Date])), InsertDayName = Table.AddColumn(InsertDayWeek, "DayOfWeekName", each Date.ToText([Date], "dddd"), type text), InsertWeekEnding = Table.AddColumn(InsertDayName, "WeekEnding", each Date.EndOfWeek([Date]), type date), InsertWeekNumber= Table.AddColumn(InsertWeekEnding, "Week Number", each Date.WeekOfYear([Date])), InsertMonthnYear = Table.AddColumn(InsertWeekNumber,"MonthnYear", each [Year] * 10000 + [MonthOfYear] * 100), InsertQuarternYear = Table.AddColumn(InsertMonthnYear,"QuarternYear", each [Year] * 10000 + [QuarterOfYear] * 100), ChangedType1 = Table.TransformColumnTypes(InsertQuarternYear,{{"QuarternYear", Int64.Type},{"Week Number", Int64.Type},{"Year", type text},{"MonthnYear", Int64.Type}, {"DateInt", Int64.Type}, {"DayOfMonth", Int64.Type}, {"MonthOfYear", Int64.Type}, {"QuarterOfYear", Int64.Type}, {"MonthInCalendar", type text}, {"QuarterInCalendar", type text}, {"DayInWeek", Int64.Type}}), InsertShortYear = Table.AddColumn(ChangedType1, "ShortYear", each Text.End(Text.From([Year]), 2), type text), AddFY = Table.AddColumn(InsertShortYear, "FY", each "FY"&(if [MonthOfYear]>=FYStartMonth then Text.From(Number.From([ShortYear])+1) else [ShortYear]))in AddFYin fnDateTable

Page 12: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

To bring this into our table, enter 1/1/2018 into the StartDate, and for the EndDate, enter 31/12/2021. For the FYStartMonth, we will use July as the financial year starts in July, type 7. When finished, check for errors, if all good, click the Invoke button.

On the left side of the screen you will see the following:

Right click on Query 1, rename it to Dates Query and rename Invoked Function to Dates.

On the table you have a new first column named Date, containing all the days, months and years from 2018 to 2021 and many other columns which were created by the code and from the dates you entered into the Date Parameters box

As you look through the other columns heading names, many of them have no spaces, this is how they were picked up from the original data. Go through and right click and rename the column heading names as follows:

QuarterOfYear Quarter of Year MonthOfYear Month of YearDayOfMonth Day of Month MonthName Month NameMonthInCalendar Month & Year QuarterInCalendar Quarter & YearDayInWeek Day of Week DayOFWeekName Day of Week NameWeekEnding Week Ending MonthnYear Month & YearQuarternYear Quarter & Year DayInWeek Day in WeekDayOfWeekName Day of Week Name WeekEnding Week Ending

We will not be using the column DateInt. Right-click and select Remove.

TIDYING UP THE QUERIES LIST

As more tables are added to Power BI Desktop the list of queries on the left can 11 | P a g e

Page 13: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

become quite lengthy, making it more difficult for you to find the query you are looking for. Fortunately, there is a simple tidy up method that we can use to fix this. We can create groups and allocate queries into those groups

On the left side of the screen select the shaded query names, use the Ctrl key to select each one.

With the query names selected, right click in a highlighted area and select Move to Group. Select New Group.

The New Group dialog box will appear. Type the name for the group, Data Model.

The Description is optional, you could type something like ‘Key Queries/Tables’. Click OK.

The Queries list will now look as on the left with 6 queries/tables in the Data Model folder.

The small black diamond shaped arrow on the left is for collapsing and expanding the names inside the group.

Note, the Other Queries (3) group has been created by Power BI Desktop.

We will create two more groups. Select Sales_2019 and Sales_2020, right click and select Move to Group. Select New Group and name it Supporting Queries.

Select Dates Query, right click and select Move to Group, select New Group and name it

Parameter Queries

The Queries list now looks better organized, as below:

The list on the left is displayed Expanded.

12 | P a g e

Page 14: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

When Collapsed it will hide the Query names as on the right, making it easier to work with when you have multiple queries that fill up the list.

When working on this report in the future, you will be able to clearly see the queries (tables) that will be used in the model, making it more intuitive for yourself and others, along with the parameters and supporting query groups.

ADVANCED TRANSFORMATIONS

When working in the Power Query Editor, you can Load or Import data from many different sources. A source worth looking at is Web. There is a web site that contains information that would help us with our Power BI Desktop report we are currently building. It contains the Demographics for California.

The link is to the web site containing the data we need is:https://www.california-demographics.com/

Scroll to the bottom of the web page and you will see information about California.

At the bottom of California Cities by Population there is a link to ‘See all California Cities by Population’

Click on this link and a full list of the cities will appear as on the following page.

Click into the address bar at the top of the page, select it and copy it.

13 | P a g e

Page 15: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Back in Power BI Desktop, select the Home tab, from the New Query group, select New Source, select Web. The From Web dialog box opens, paste the web address into the URL box as below and click OK.

The Access Web Content dialog box appears, click Connect.

Depending on your speed of the internet, you may have to wait, but when the connection is made the Navigator dialog box appears. The data will by default named Table 1, place a tick to the left and a preview of the California data by Rank, City and Population will appear on the right.

14 | P a g e

Page 16: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Click OK and Power BI Desktop will display the data as part of the Queries list.

Right-click on Table 1 and rename it to CA Population Data.

It will be part of the Supporting Queries. Simply drag it up into the correct group.

There is a connection between the Locations query and the data we brought in from the Web. Click on Locations in the queries and scroll across and find the Population column in this query. Go back the CA Population Data query and you will see this also has Population data. The data from the web is up-to-date, as it is updated monthly, but we have been informed that the data in the Locations query is out-of-date and we need to bring in the updated Population data in the Locations query.

Fortunately, Power BI Desktop offers an easy way to do this by using Merged Queries.

To get started, check that you are still in the Locations query. Select the Name column and from the Home tab on the Ribbon, select Merge Queries, from the Combine group.

15 | P a g e

Page 17: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The Merge dialog box will appear. Select Name from Locations, it will become highlighted.

From the drop-down arrow, select CA Population Data and select City.

The data from the Locations query has 1 occurrence for the name of each city, whereas the CA Population data query, uses the city names repeatedly.

Power BI Desktop has identified this Join Kind and allocated a Left Outer join, meaning every city name (74 records) from the Locations query, has been found

to match city names in the CA Population Data query (1353 records). Click OK

You may receive a Privacy Levels message, tick Ignore Privacy level checks …

Click Save.

Now you have merged the data from CA Population Data query into the Locations query, it appears as a single column, with the heading CA Population Data with the word Table repeated in every cell. This is because all the columns have come across in the merge and we need to identify which of the column or columns we want to show in the Locations table.

Notice in the APPLIED STEPS on the right x Merged Queries again, remember, all your steps are being recorded.

16 | P a g e

Page 18: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

To the right of CA Population Data column head name, there is a small expand button, click this. A list of the three column names from the CA Population Data query displays. We only want the Population data, remove the Select (All Columns) tick, Population Click OK.

To tidy up, remove the original Population column and then rename the CA Population Data.Population column heading to Population

On the left side of the screen under Queries (10) right click on CA Population Data query and remove the tick from Enable Load as we do not want this data loading every time we refresh the report, the data in in the Locations query now.

17 | P a g e

Page 19: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

None of the Supporting Queries will now load, indicated by the italic style of the names.

With the Locations query now containing the population data, we could set up a query that will take the population of a city in California and work out what their percentage of sales is against the total population etc. We could then see which city has the highest percentage of sales and which city has the lowest percentage of sales, just one of the many ways this data could be used.

DATA MODELLING

For now, we are finished with the Power Query Editor, which we have used to import data from local Excel files and import from a web site. We used Transform (meaning shaping, or editing) to make changes to the data, like renaming column head names and Query/table names, used Append Queries, to append 3 query/tables into one main query/ table, and used Merge Queries to join a column from one query/table to another. You will be returning to the Power Query Editor as needed, for example, you may need to load or import more data, add or remove columns, rows etc.

To leave the Power Query Editor window, and move on to the

Data Modelling, in the Ribbon, from the Home tab, click on the

18 | P a g e

Page 20: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Close & Apply button. Check Locations from the Fields list and after the Query applies and updates you will have the new merged Population column.

Click on the Report icon on the left of the screen.

You will arrive at what appears to be a blank screen, called a Canvas with the Query/table names displayed on the right. This is called the Report view and this is where you will build reports, like tables, charts and maps etc. based on the data you worked with in the Power Query Editor window

On the right side of the screen under the heading Fields, click

Locations and a list of the column head names appear.

Click a second time and they collapse, the small up and arrows

are a quick way of expanding and collapsing the column names,

now called Fields and have small selection boxes to their left.

Try right clicking on a Query/Table name, then a Field name you will see different options available. We will go through these later.

One of the biggest differences between working with data in Excel worksheets and working with data in Power BI Desktop is that in Power BI Desktop the tables will be used to extract data from to create reports that require the tables to be linked to one

19 | P a g e

Page 21: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

another by one or even multiple links. In other words, the tables need to form a relational database, when tables are loaded or imported into Power BI Desktop.

We are going to take a look at the Model view in Power BI Desktop, which will show you a ‘schema’ (similar to a Flow Chart) of the current data and which tables have been linked and which tables are not. Power BI Desktop attempts to make the relationships, which are based mainly on column name headings, and unfortunately does not always get it right. Sometimes you will need to change, delete or even create a link, you may even need to create a table to help to join two tables together because they do not have anything in common to link to each other. On the left side of the screen there are three icons:

Report view icon – used to create tables, charts, maps etc.

Data view icon – used to view and Transform loaded or imported data tables

Model view icon – shows all the tables, both linked and unlinked

Click the Model icon and the following screen appears, the layout is called a Schema.

Power BI Desktop has been programmed to attempt to smart link the tables it decides should be joined but it is not always correct.

It is likely that you will not see all the tables on your screen. On the bottom right corner of the screen there are a set of resize buttons, using them, try and get all the tables to fit on the screen. You can use the mouse to drag them into a position where you can 20 | P a g e

Page 22: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

see the link lines clearly. If there is scroll bars bottom or left, then you might need to stretch down or out to view all the field names.

If you hover over a link line it will become orange and helps you to see the from and to names, which also highlight in orange. Note Dates, the table we created is not linked.

RELATIONSHIPS BETWEEN TABLES

To help you to understand relationships and how incorrect relationships between tables, can results in data error, drag and rearrange the tables as below:

Next, we are going to remove all the link lines that Power BI Desktop has decided are correct for our data, then we can work through each one and understand the Why.

21 | P a g e

Page 23: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Right click on a link line and select Delete, the Delete Relationship message box will appear, to allow you to Cancel or Delete. Click Delete.

We are going to make sure that all the links are correct. Repeat above until all the link lines have been deleted and again rearrange the tables similar to below, with the main table Sales at the bottom and all the supporting tables lined up above.

Click the Data view button and on the right side of the screen, under Fields, click on the Sales to display the data on the left. Let’s look at some of the column names.

22 | P a g e

Page 24: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

You will see there are lots of ID columns, Order, Product, Location, Sales Person and Customer.

If you return to the Model view again, you will see that these ID’s all have a match to each of the supporting tables except for Dates.

We will create our first link, then check out how and why it works. In Model view, drag the Salesperson ID from the Salespeople table to the Sales Person ID in the Sales table. When you release the mouse button, you will see a line from one table to the other. If you click on this line, it and the selected fields will be highlighted orange. You will see a 1 on the end of the Salesperson Id link line in the Salespople table and a small star at the end of Sales Person Id in the Sales table, with a small arrow pointing forward in the middle.

So, what does this mean?

Remember, on the bottom right corner of the Model screen are a set of tools that can help you to zoom in and out, fit to screen etc.

You will observe that the names we linked in these table names are not identical, i.e. Salesperson ID and Sales Person ID, this often happens when tables are being designed and can make relating tables very difficult, but it is not so much having matching names in the tables that is important, it is the data that is contained in them

23 | P a g e

DataView

Page 25: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

that is important. To explain further, the Relationship we just created is the most common used, it is called One to Many.

In the Salespeople table each salesperson’s name and ID has been entered once, this ID is unique to each salesperson, a little bit like your Licence, only you have that number, but you may own more than one car. There are 44 sales people in the Salespeople table.

In the Sales table, because each salesperson can have lots of sales, their ID, is repeated many, many times. There are 10,088 rows of Sales. Out of this total, 239 are for the Salesperson with the ID of EMP1000, this one sales person has made multiple sales. This relationship will make it easy for us to see which sales persons are in the top 10 or the bottom 10 and create relevant reports later.

The next table is the Products table, this table has 101 rows, 1 row of data per product, with a unique product number for each one. Drag the Product Id from the Products table to the Product Id in the Sales table, again this has defaulted to a One to Many relationship as the Product Id number will be repeated many times in the Sales table.

We could do the same for dates, but there is another way to do this. At top of the Model view screen, on the Ribbon, click Manage relationships in the Relationships group.

The Manage relationships dialog box will open, showing the two relationships you just created and ticked Active.

Click New.

The Create relationship dialog box will open. We need to join Date from the Dates table to Purchase Date in the Sales table.

24 | P a g e

Page 26: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Click OK, after a short time the Model will reappear and you will see your new link, again it is a One to Many relationship, that has been created between the two tables.

You may remember that earlier we created the Dates table, which had 1 row per day from the 1 January 2018 to 31 December 2021, this will now nicely link to the Purchase Date data when many items may be purchased on the same day.

Complete the remaining joins, using whichever method you prefer, at this stage the default continues to be a One to Many relationship, so no need to change this. You will end up with a Model similar to the following.

Now we are ready to move to the next stage and use these joined tables to create some exciting and innovative Reports.

25 | P a g e

Page 27: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

USING DAX IN POWER BI DESKTOP

What is DAX? DAX stands for DATA ANALYSIS EXPRESSION and is the language used in Power BI Desktop to create Measures (similar to formulas in Excel).

Click into the Report View icon on the left of the screen.

The screen will appear to be blank with the Filters, Visualizations and Fields panes on the right side of the screen. The queries/tables names are listed under the Fields pane.

It can be quite confusing as there is not much of a directive or where to go or what you should do, so the best way is to remember that you have tables of data that have been correctly linked and you are in the design view of Power BI Desktop, and have a blank canvas, where you build reports. Reports may be tables, charts or maps.

Using your mouse expand the fields in the Sales table and place a tick in Quantity.

A chart, called a Visualization, will appear in the Central pane, showing the total of all quantities from the Sales table. This chart is in a Tile, which can be moved and resized.

Next, we would like to analyze the data and find out the Quantity ordered by each customer. Expand the Customer table and place a tick in Customer Name.

The chart refreshes, showing the customer names and quantities in a Clustered Column chart. Why did you get a chart? The first data you selected, Quantity, has a Summarization icon to its left, meaning it’s a numeric value, and can totaled 26 | P a g e

Page 28: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

or used to create formulas (called Measures) and by default, numeric values will create a chart. With the Customer Name added, the chart is now more informative.

You did not pick this chart, Power BI Desktop chose it for you, but you are running the show, so try a Clustered Bar chart, just look through the Visualizations, hover over the small icons and you will see the various names of each. Here is the same data as a Clustered Bar chart.

You may not be aware at this stage that every customer is included, and you would need to stretch the chart a very long way to see each one, but remember we would be setting certain conditions, called Filters so that we only get the data in the report on what we need, not every single record, as is happening here.

Click away from the chart and deselect it. Place a tick next to Customer Name, then Quantity.

You will get a Table visualization, which is the default when the first field selected is text, the Quantity field is numeric and by default a summary total appears.

27 | P a g e

Page 29: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

To see DAX in action, we will create a very simple one. In Excel when you want to know any calculation, you create a Formula, in Power BI Desktop, you create a Measure. Basically, they are very similar, some even follow the same format as Excel and get the same results.

Displayed on your screen is a chart and a table, showing each customer’s name and the grand total of the quantity ordered by each. Let’s say that we would like to see the chart displaying, not just the data Quantity, from the table Sales, but the Quantity with a projected 15% increase. This data is NOT available from our tables or fields, so we will create it. Select the chart.

Right click on the Sales table under Fields and click New Measure. The Formula bar at the top of the screen will show the number 1 and a default highlighted name as below. This is the start of the Measure being automatically filled in for you and represents a default column head name, the = is there to indicate anything you type to the right of this will be the DAX Measure.

Type the measure name: Projected Quantity 15% Increase followed by the = symbol

Start typing su (Power BI Desktop’s Intellisense will display suggestions)

Double-click Sum and the formula bar will complete it for you including a left bracket. A list of table names and their fields will appear in a drop down list.

28 | P a g e

Page 30: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Scroll down and find:

Sales[Quantity]) and click it. The table name and field name will now appear in the formula bar. (Note: Field names are always enclosed in square brackets [ ] )

Type * 1.15 and either press the Enter key or the tick on the left side of the formula bar to complete the measure.

(Note: You cannot use a percentage symbol %, use decimals in DAX Measures)

If there is an error in the Measure, you will not be allowed to move on, red squiggly underlining will indicate the error or errors as well as an error message displaying to try and help you to resolve the issues, as below, where Sum has been incorrectly entered, and has caused a second problem with Quantity.

If no error indicated, after a short wait the Measure name will appear on the right side of the screen, under the Sales table, as a field, but will have a small black calculator icon to its left.

Note: All measures have this symbol to indicate they are Measures and not Fields. Anytime you click on a Measure name the Measure syntax will appear at the top of the screen, you can view it, edit it or even delete it using the to the left of the Measure.

We are now ready to use the new Measure and add it to the Quantity by Customer Name chart.

Check that the chart is selected and place a tick mark next to the measure name ‘Projected Quantity - 15% Increase’ under the Sales table.

29 | P a g e

Page 31: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The chart refreshes, showing your new measure.

You have created your first DAX measure and if you are familiar with Excel formulas, it is not that much different, the main difference is that the Table name must be first and enclosed in Parentheses  ( ) whist the Field name is enclosed with square brackets [ ].

(Note: A full list of DAX measures in at the end of the manual, in Appendix 1)

Try on your own to edit the Measure to be a 25% increase in Projected Quantities, also edit the heading so it matches the edited DAX Measure. Press Enter or click and the chart will update showing the new value and title.

We are going to be using the Quantity by Customer Name to work on more DAX Measures. Remove the tick from the left of the name ‘Projected Quantity - 25% Increase’ as we do not need it, we could always bring it in again later. The title will also update automatically. Add Data labels to the chart, from the Format pane.

30 | P a g e

Page 32: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Add the new measure to the table. Sort both chart and table by Customer Name and sort Ascending. Rearrange as on the following page.

We have looked at the = Sum DAX measure, and which looks quite like the Excel =Sum formula, but there is an extremely important difference, Excel works on the individual cells, for example =Sum(A1:A10) would give you the total of the values in this range of cells.

Power BI Desktop does not work on individual cells, it works on Columns, for example each Customer has lots of individual quantities in the table Sales, but the chart is the grand total of each quantity for each Customer. Delete the table, keep the chart.

To look at how this works, from the Home tab, click Transform Data (or on other versions it may be Edit Queries. If we go to the Sales table and find Customer No. C1246 – Ronald Bradley, the go to the Sales table and using the Filter drop-down arrow and search for Customer No. C1246 – Ronald Bradley you will see all his quantities.

His grand total is 56. Using the Filter button, select Clear Filter.

Note: Power BI Desktop works with Grand Totals and not individual values.

Click the Close & Apply button to return to the Report view.

Under Filters, click Customer Name is (All) under Filters on this visual to expand it.

31 | P a g e

Page 33: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

In the Search box, Start typing: Ronald, all names starting with Ronald will appear.

Place a tick next to Ronald Bradley’s name, the chart will now display as below, showing Ronald’s grand total i.e. 56.

Delete the name from the search box and place a tick in Select all to clear the Filter.

We will create a second simple measure. Check that the chart is selected and right-click on Sales in the Fields list and select New Measure (or, after your select Sales you could use the New Measure button in the Calculations group of the Table tools tab on the Ribbon.

(Note: Because you selected Sales, the Measure is now part of Sales)

On the formula bar, type the name Average Quantity = Start to type Average, as you type Power BI Desktop’s Intellisense takes over and starts listing DAX names starting with AV. Click AVERAGE and a pop-out help box will inform you of the meaning of that highlighted Measure, as below. If the DAX is listed, you can double-click it and Power BI Desktop will complete it for you.

Double-click on it will appear with an opening parenthesis and the Intellisense will kick in again, suggesting that the next step is to find the Table and ColumnName required, and listing them as below.

32 | P a g e

Page 34: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Scroll down until you find Sales[Quantity]. Click it and it will appear in the formula bar, finish by typing a closing parenthesis ).

Press the Enter key or click the if there are no errors, continue, if an error is detected try and fix it.

On the right side of the screen remove the tick from the Quantity field and place a tick to the left of the new Measure Average Quantity.

The chart and the title update, reflecting the new Measure.

Tick the Quantity on again and view both values in the chart.

33 | P a g e

Page 35: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Now to see the data quickly and easily as a Table, right-click on the chart and select Copy / Copy Visual. Click Paste from the Home tab on the ribbon in the Clipboard group. The pasted copy is pasted on top of the chart. Drag to the right and, while still selected, click on the Table visualization to view the same data in two visualizations.

On the right side of the screen under Sales, you can see the two measures:

Sum and Average are some of the earliest formulas you learn when first using Excel.

The other two are usually Minimum and Maximum. Let’s see how we can finish off this simple introduction to DAX Measures. Click on Sales, click New Measure from the Calculations group on the Ribbon. We want to see the highest quantity for each customer. Create the new Measure as below.

34 | P a g e

Page 36: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Press the Enter key or click the if you have no errors, otherwise try and fix. The chart and title again will update, show the highest (Max) quantity for each customer. The results will be unclear, it looks like every customer had a maximum of 4 but we can do an easy sort and take a different look at the data.

Repeat the above steps and create the New Measure below, to find out the lowest (Min) total quantity per customer.

You can now check out the effect on the chart of the various DAX measures you created. Delete the Table and stretch out the chart.

Keeping the Customer Name, add Quantity, Highest Quantity, Lowest Quantity and Average Quantity to the chart

On the top right of the chart there are 3 dots … called ellipses. They list More Options. Click on the ellipses and select Sort by, select Quantity. We need to select the More Options again and select Sort Descending. The chart will now display from the highest quantity to the lowest.

Finally, we are only interested in the top 3 customers by quantity. Remove all fields from the chart, except for Customer Name and Quantity

On the right side of the screen, under Filters, in the Customer Name section, click on Basic filtering and select Top N

35 | P a g e

Page 37: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Type 3 as shown on the right and drag Quantity into the By value Box.

Click Apply Filter

The chart will now update, showing the top 3 Customers by quantity. Under Visualizations, you will see a Paint Brush, the formatting button, look for Data Labels, click them to on, you can also choose where they will display. Try using the View tab and look at the different Themes. Here is my final chart.

Note: It is very worthwhile checking through the Formatting options, where you will find all kinds of interesting options.

Click on the at the bottom of the screen to insert a new page.

We are going to create 2 new measures. To get started, click the Table Visualization and a blank table tile will display in the Central pane.

36 | P a g e

Page 38: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The new measures will be calculation measures, one to work out the Total Sales and one to work out the Total Transactions. Right click on Sales on the right in the Fields, and select New Measure and create the following measure:

Total Sales = SUMX( Sales, Sales[Quantity] * Sales[Price] )

If no errors, press the Enter key or click the The new Measure will appear under Sales.

Create the second measure:

Total No. of Sales Transactions = COUNTROWS( Sales ) If no errors, press the Enter key or click the . The new Measure will again appear under Sales.

(Note:  SUMX returns the sum of an expression, the Expression is usually a table name and field name in this formula there are two Expressions joined by the Multiply operator. The SUMX evaluates the value for each row in a table, i.e. quantity * price and displays as a grand total for each customer in the Sales table. Formulas ending in X are called DAX iterators and once you get past the slightly confusing way to write the expressions, they are a fundamental building block for any tabular model including Power BI Desktop and Power BI)

37 | P a g e

Page 39: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

We are now ready to populate the table. Place a tick next to Customer Name in the Customers table, place a tick next to Total Sales and Total No. of Sales Transactions in the Sales table, the table will look similar to below, with the customer names sorted in Ascending order:

( Note:  To format the result of a measure value as Currency, click on the Measure name in the FIELDS pane and then select the Currency symbol from the Measure Tools tab in the Ribbon’s Formatting group.

RELATED TABLES

Now we know the amount of the Total Sales for each customer, we would next like to work out the Profit. The details for costs is in the Products table, under the column Product Cost. You cannot just do a simple subtraction, because the data we need is coming from two separate tables, we need to create a more complex Measure in order to achieve our aim i.e. see the Profit we have made per customer.

38 | P a g e

Page 40: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Right click on the Sales table and select New Measure. Type the following, using Power BI Desktop’s Intellisense wherever possible to help you complete the Measure.

Total Costs = SUMX( Sales, Sales[Quantity] *

Because we need to use a column that is not in the Sales table, but in the Products Table, we are going to use a DAX function called Related, which can return a related value from another table. Start typing Rel and Power BI Desktop’s Intellisense will list all functions starting with or including Rel as below, double-click on RELATED. Related will be added to the Measure followed by an opening parenthesis.

This will activate a list of all the tables in our model and their field names.

Scroll down until you find Products[Product Cost] and double-click.

Type the closing Parenthesis.

The final Measure is as below.

If no errors, click the or press the Enter key

Note: This Measure will calculate the Quantity for each product in the Sales table which has 10,889 rows and multiply it by the Product Cost for each product from the Products table, which has 101 rows. Power BI Desktop does not calculate by cell but by column.

39 | P a g e

Page 41: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

In the Central pane, check that the table tile is selected and add the new Measure, Total Costs under Sales by placing a tick to the left. Remove the tick from

Total No. of Sales Transactions the result will be as below:

Note: The values above, i.e. Total Sales and Total Costs can easily be checked in Excel and you will see the values are correct, but it would be quite complicated as the Sales are in three separate sheets within Excel. We appended them into one table in Power BI Desktop, making them easier to work with.

Now that we now the Grand Total for each Customer’s sales and the Total Costs against those Sales, we can work out if we made a Profit or a Loss on each customer’s sales and an overall profit or loss, by subtracting the Total Costs from the Total Sales.

We need to create a New Measure called Total Profit/Loss. This Measure is called a Branch Measure, because you will be using Measures you already created earlier, to create this Measure.

Right-click on Sales and start to type the Measure as below, remember to use Intellisense to help you to complete the Measure.

If no errors, press the Enter key or click the

This is a simple measure, much like one you would create in Excel. Place a tick next to it under Sales in the Fields list.

40 | P a g e

Page 42: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The table will show exactly what we asked for i.e. a Customer Name, the Total Sales, the Total Costs for each, and whether or not it was a profit amount or a loss amount.

Note: The table would look better if you change the formatting for all numeric columns to Currency. Click on Total Costs in the Fields list and from the Ribbon, select Measure Tools, and select $ from the Formatting group.

The Total Costs will display with a currency symbol. Repeat for Total Profit/Loss.

Note: Both tiles above are sorted by Customer Name in Ascending order. Also, it is important to remember that every customer, all 800 are still included, as we move forward, we will be creating more measures to filter and select only the data we need for the final report.

Now that we know the profit for each customer and the grand total, which is also a profit (in this scenario, there were no losses), to further analyze the data we need to find what percentage each customer represents against the grand total. There is a Measure called Divide and we will now use it in our new Measure.

Right click on Sales and select New Measure and start typing as below, remember again Intellisense will help you as you type.

If no errors, press the Enter key or click the

41 | P a g e

Page 43: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Place a tick mark to add this to the table tile to get the following results.

To format the Profit Margin as a percentage, click on Profit Margin in the Fields list and from the Measure Tools, in the Formatting group, click the % symbol. The 2 decimals are default, you can decide what you prefer.

Mathematical verification of Profit Margin.

42 | P a g e

Page 44: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Drag the table to the right and click in an empty area of the Central pane. Select a Clustered Bar chart from the Visualizations. Add the County and the Total Profit/Loss fields. Sort by County in Ascending order. Add Data labels.

Note: In this section you have created measures and then utilized those measures to create Branching measures, allowing you to analyze the data in more depth. The original data tables are unchanged, all the Measures have been created in Report view and can be used again and again in the Report or to create new measures as you need them.

ADVANCED DAX

We will move on to more complex DAX measures.

The table contains every customer for every sale and cost for the 3 year period from the Excel file but does not contain a date filter, which we need to create, to enable us to do some date comparison analysis work. We created a Dates table earlier when we set the date range from 1/1/2018 to 31/12/2021. We now want to use these dates to control the data in the table.

We need some space at the top of the Central pane, drag the chart and the table down towards the bottom of the pane.

Check that neither tiles are selected. From the Visualizations click the Slicer button

43 | P a g e

Page 45: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

A blank Slicer visualization tile will appear, move it to above the table and resize it.

From the Fields list under the Dates table place a tick against Date and the following Slicer will appear, allowing you to filter the Customer table by date.

Before filtering the data, it would be good to see how many customers, you currently have and how that number changes when you Filter by date. You will need to create a New Measure to count the customer’s names. Right click on the Customer table, and select New Measure, and type it as follows in the formula bar.

Check that no Tiles are selected.

On the Visualizations click the Card icon which

will create a blank tile visualization, it is

great for displaying a single value.

Drag to under the Dates slicer tile, resize to fit both above the table tile.

44 | P a g e

Page 46: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Place a tick to the left of the measure, Count of Customers and the Card visualization will populate with the total number of customers.

Rearrange as below.

Note: For the date slicer and card slicer there are interesting Formatting options available in the Visualizations format section, using the Format icon like borders, radius to round the corners, fill color, line color etc.

You can now test the Date Slicer, by either dragging the date markers, or typing in a start date and a finish date. Here are the records for 1/1/2020 to 3/1/2020 with a bit of formatting added to make it interesting.

45 | P a g e

Page 47: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Add a Card visualization above the Chart tile and add Total/Profit Loss, format it.

Note: from the Insert tab, in the Elements group, you can add text boxes, buttons shapes and images to enhance the Report.

Organizing DAX Measures46 | P a g e

Page 48: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

On the right side of the screen under Fields, you have table names, field names and DAX measure names. For example, under Sales you have quite a few DAX measures.

The DAX measure will be hidden when you collapse a table’s group and it can be a bit of a task to try to remember where they all are. A good way is to organize the DAX measures into their own table, that way they will be together and easier to find.

There are a few steps we need to take to do this.

1. Deselect any visualization tiles on the screen2. From the Home tab, select Enter Data from the Data group3. The Create Table box will appear, type ‘Key Measures’ in the Name box.

47 | P a g e

Page 49: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

4. We do not need to not add any values to this table as we will populate it from the DAX measures in the Fields list.

5. Click Load

In the Fields list you will now have a new table name Key Measures as below:

Next, we need to take the DAX measures from their current locations and re-locate them into the Key Measures table.

1. Click on the Customers table in the Fields list and expand it. Click on Count of Customers (Do not place a tick in the box on the left). On the top-left of the screen, in the Structure group, you will see the Field name and the Home table name. Click the down arrow to the right of Home table to show a list of all tables. Click on Key Measures

2. The Count of Customers is now in the Key Measures table group.

3. Continue and move all the DAX measures you created, (they have a small black calculator icon to their left). When finished the Key Measures will be similar to the following:

48 | P a g e

Page 50: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The table we created for Key Measures is blank, it could be called a ‘dummy table’ as we only need it to organize our DAX measures, therefore, we do not need Column 1, right-click on it and delete it. Confirm the Delete.

It would be easier if the Key Measures were at the top of the Fields list. Click on the >

to the right of Fields and it will hide the pane.

Click on the <

and the pane will

expand again.

The Key Measures will be at the top with a slightly different icon to the left, indicating that there are multiple contents

Now that the Fields pane is better organized, we can take a look at the Model view where you will see that the Key Measures is now included in this view, but it is not linked to the other tables. It is a table, but it is a blank table and only being used to store our DAX Measures. (if you cannot see the entire layout click on the Fit to screen button on the bottom right of Model view.)

49 | P a g e

Model View

Page 51: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

We will now tidy up the Data view. In the Home tab, click on the Transform data button in the Queries group, you will return to your tables/queries.

On the left side of the screen the Queries will be listed. Right click on Key Measures and select Move to Group, then select New Group

The New Group dialog box will display, in the Name box, type DAX Measures.

Click OK

The Key Measures are now in their own Folder, later if you create more DAX measures they can be moved to this Folder.

In the Home tab, click the Close & Apply button in the Close group

Select the Report view icon.

50 | P a g e

Page 52: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Now we have dates and values, we are interested in comparing the data from Last Year to this year.

To get started, click the New page button

Select Table from the Visualizations. A blank table visualization tile will appear.

We want to populate this table with dates.

Expand the Dates table in the Fields and place a tick

mark next to Date. The table now has dates.

Depending upon the dates you have filtered, the following table will display

We do not want to display this default Date

Hierarchy. Under Values in the Visualizations pane, click the down arrow to the right of Date . The Date Hierarchy has been selected by default. Click Date to change it.

The date format updates to show the following table of dates, which we are going to use to calculate the Totals Sales from this year and compare them to Last Year Sales.

The date format being displayed is being picked up from the Dates table, but it can be changed to a different style, by clicking on the Data view icon on the left of the screen then clicking and expanding the Dates table in the Fields pane.

51 | P a g e

Page 53: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The Date table columns will appear. Select the column head Date and from the Column Tools change the format to dd/mm/yyyy as below.

The dates will now be easier to fit into the new table visualization tile we are creating.

Click the Report view icon to return to the Central pane which will display the updated dates.

Add the Slicer visualization to the top of the page and add change the date range to read: 01/07/2019 – 30/06/2020

Select Key Measures from the Fields, right click and select New Measure and create the new measure as below, named Total Sales This Year.

52 | P a g e

Page 54: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Select the table with the updated dates and add the Total Sales This year field from the Key Measures group.

With the correct date range from our Date Slicer now displaying and the Total Sales for this year, we can move on and create another Branched DAX, i.e. use a current DAX measure to create a new one and calculate Last Year Sales for the same period, for example what were the Total Sales on the 1July 2018 and compare them the Total Sales for 1 July 2019

Right click on Key Measures under Fields. Select New Measure. Type the name for the measure Total Sales Last Year =

The DAX measure Calculate can be used because we have a Date filter in our report.

[Total Sales] is a DAX measure we created earlier and we want to add up the total sales but not just a general calculation but using a special DAX measure that will add up the Total Sales but from the previous year, based on our date filter.

The last part of the formula is to use the Date from the Dates table.

The completed formula:

Add this new DAX measure to the table and format both columns to Currency. You will see the following results.

53 | P a g e

Page 55: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Here we can see day by day the Total Sales for the two periods, which is useful, but we can copy this table and check out the monthly figures.

Right click on the table and select Copy / Copy Visual

Click the Home tab

Click Paste

Unfortunately, the pasted copy lands on top of the current table, using the mouse, drag away into its own space on the right.

Under Visualizations, under Values, click the down arrow to the right of Date and select Date Hierarchy.

Click on the small x to the right of Day and Quarter, leaving the Year and Month to get the following results.

54 | P a g e

Page 56: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

It would now be interesting to see the monthly data in a chart form. Right-click on the Table visualization tile and select Copy / Copy Visual. From the Home tab select Paste and drag the pasted tile to the bottom of the screen and select a Clustered Column chart. Because of the dates, you will see the following chart:

We would like the monthly data to show. From the Values, under Visualizations, remove the small x to the right of Year, leaving Month.

55 | P a g e

Page 57: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

The finished page, with the Date slicer, comparative yearly data, monthly data and a monthly chart.

You can change titles, fonts, colors, border etc.

To finish this DAX session, we have enough data to calculate the difference in Sales between this year and last year. Was it higher or lower?

Click the Key Measures under Fields and click New Measure as below.

This measure is about checking if the Totals Sales For this Year is blank, then leave blank, if not then take the Total Sales This Year and minus the Total Sales Last Year

and calculate. Add this column to the table and check out the results.

56 | P a g e

Page 58: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

In July 2019 we made $26,228 less than we made in July 2018. In September 2019 we made $39,674 more in Sales than the same month in 2018. In June 2020 there were no sales, it is blank, so the DAX measure returnS a negative of $956,719 for the 2018 Sales.

Using this DAX we can branch it into other DAX measures to find the percent value.

Again, right click on Key Measures in the Fields list and select New Measure and create the follow measure, note how it is using the names of the previous DAX measures.

Add this to the table for the following results:

As you create DAX measures and then use those measures to create more measures you are starting to utilize the power of Power BI Desktop.

This session was designed to give you a general overview of Power BI Desktop from the initial loading of data to working with DAX measures.

An example of a DAX measure that creates a running total for the Total Sales57 | P a g e

Page 59: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

DAX Measure to Calculate a Running total of Total Sales, named Cumulative Sales Total with a date condition less than or equal to latest date.

58 | P a g e

Page 60: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

Appendix 1 - DAXHere are the main aggregation functions that you can use in DAX:

Function What it does for a column’s values

AVERAGE, AVERAGEA Returns the average (arithmetic mean), ignoring or taking account of text entries (see hint below)

COUNT Returns the number of cells that contain numbersCOUNTA Returns the number of non-empty cellsCOUNTBLANK Returns the number of blank cellsCOUNTROWS Returns the number of rows in a tableDISTINCTCOUNT Returns the number of different distinct valuesGEOMEAN Returns the geometric meanMAX, MAXA Returns the largest valueMEDIAN Returns the median (the halfway point)MIN, MINA Returns the smallest valuePERCENTILE.INC, PERCENTILE.ENC

Two similar functions which return the nth percentile in a set of values

STDEV.P Returns the standard deviation of the entire populationSTDEV.S Returns the sample standard deviationSUM Adds the cells’ valuesVAR.P Returns the variance of the entire population for a columnVAR.S Returns the sample population variance for a column

Common function names that you can use to sum an Expression across a table:

Function

What it does for a column’s valuesAVERAGEX Returns the average (arithmetic mean) of an expressionCOUNTX Returns the number of cells that contain numbersCOUNTAX Returns the number of non-empty cellsGEOMEANX Returns the geometric meanMAXX Returns the largest valueMEDIANX Returns the median (the halfway point)MINX Returns the smallest value (named after Minnie from The Beano)PERCENTILEX.INC, PERCENTILEX.ENC

Two similar functions which return the nth percentile in a set of values

STDEVX.P Returns the standard deviation of the entire populationSTDEVX.S Returns the sample standard deviationSUMX Adds the cells’ valuesVARX.P Returns the variance of the entire population for a columnVARX.S Returns the sample population variance for a column

59 | P a g e

Page 61: GETTING DATA AND USING TRANFORM - Prime Solutions Exercises/S2.docx · Web viewThe Power Query Editor will help you to prepare the data to go into the Power BI model and create reports,

60 | P a g e