DAX: Revenue, Cost and Profit

After having set up a basic data model and date table, now it is time to start analyzing the data by writing DAX measures. I will start by creating a new table to store the measures and then write the DAX formulas for Total Sales, Total Cost, Total Profits and Profit Margin.

1: Creating a table to store the DAX measures

After opening the DK Retailer .pbix file, I navigate to the Enter data button on the ribbon. This will open the Create Table window. I leave the column and row empty and under Name I type “Main Measures” and click on Load.

x 
Create Table 
Columnl 
Name: 
Main Measures

The table will appear in the Fields pane on the right of the canvas. It has an empty column and row.

2: Calculating Total Sales

Now it’s time for our first DAX measure. Let’s start by calculating Total sales/Revenue. In order to do that we need to first look at the Sales data and find out whether we have the necessary information to calculate Revenue. I go to the Data pane and select the Sales table from Fields.

Order ID 
010030 
010091 
010237 
010370 
010395 
010413 
010488 
010534 
010545 
010556 
010568 
010612 
010629 
010708 
Product ID 
PD12028 
PD12051 
PD12080 
PD12011 
PD12085 
PD12000 
PD12028 
PD12077 
PD1207g 
PD120g7 
PD12051 
PD1200s 
PD12011 
PD12087 
Location ID 
OK124 
OK14g 
OKIsg 
OKISI 
OKIog 
OKIS7 
o:« 75 
OKIgs 
OK128 
0<101 
OKIgs 
OK141 
OK14S 
OK157 
Sales Person ID 
91017 
91017 
91017 
91017 
91017 
91017 
91017 
91017 
91017 
91017 
91017 
91017 
91017 
91017 
Customer ID 
C1495 
Cl 707 
C1578 
c141g 
Cl 402 
C1345 
cllsg 
c147g 
C1695 
c1010 
C1532 
C1181 
C1034 
C1297 
Purchase Date Quantity • 
1 2016 
SO jonuar 2016 
decemöer 2016 
15 2015 
20 novemöer 201 S 
20 2015 
29 septemöer 201 S 
Z 2016 
decemöer 2017 
& jonuar 2016 
17. 2016 
24 2016 
1 2015 
9 februar 2015

As it can be seen above, the Sales table contains information for each transaction. Each transaction refers to one product and the corresponding quantity sold. However, there is no Sales Price in the Sales table. The Sales Price is saved in the Products table as can be seen below.

_!roduct D 
PD12000 
PD12001 
PD12002 
PD1200s 
PD12004 
PD12005 
PD1200s 
PD12007 
PD12008 
PD1200g 
PD12010 
Product Name 
Product I 
Product 2 
Product 3 
Product 4 
Product 5 
Product 6 
Product 7 
Product 8 
Product g 
Product 10 
Product Il 
Product Description Original Sale Price • 
Cost Current Price 
Wine - Wyndham Estate Bin 777 
The Pop Shoppe - Root Beer 
Squid 05 - Thailand 
Papa vas 
Onions - Green 
Onions - Dried, Chopped 
Cornstarch 
Truffe Shells - White Chocolate 
Fuji Apples 
Rice - Wild 
Onions - Cooking 
2241 kr 
951 kr 
847 kr 
178S kr 
1278 kr 
684 kr 
SIS kr 
1812 kr 
sog kr 
561 kr 
kr 
208 kr 
420 kr 
Isos kr 
2241 kr 
951 kr 
847 kr 
178S kr 
1278 kr 
684 kr 
SIS kr 
1812 kr 
VAT E] 
560,25 kr 
237,75 kr 
211,75 kr 
S19,s kr 
78,75 kr 
146 kr

One of the main caveats of Power BI and DAX is that all the measures refer to columns and rows and not individual cells as is the case with Excel. Even though the columns needed are in separate tables, through the power of the data model we can relate these columns by writing the appropriate DAX measures.

To calculate Total Sales, we need to use a so-called iterating function. It is called an iterating function because it evaluates each row in a table. I go ahead and click on the New measure button in the Calculations group on the ribbon.

There I type in the following DAX measure:

Name 
Measu re 
S' Format 
Data category Uncategorized 
Fome table Vain Veasures 
Structure 
X 1 Total Sales 
sumx( sales, 
Sales [Quantity] 
New Quick 
measure measure 
* RELATED( Products[Current Price) ) )

This measure takes the Quantity column in the Sales table and then it multiplies with the Current Price column in the related table Products. It goes through each row in the two tables and returns the aggregation we need, in this case a multiplication and then sums it all up at the end.

3 Calculating Total Costs

The process for calculating Total Costs is similar to the previous step. Again, I click on the New measure button and type in the following measure:

Home 
X cut 
Copy 
Paste 
Format painter 
Clipbærd 
Insert Modeling 
View Help 
External Tools 
Dataverse Recent 
Table tools 
Measure tools 
Get Excel 
data workbook 
Power BI 
datasets 
SQL Enter 
Server data 
Transform 
data 
Refresh 
New Text 
visual box 
More 
visuals 
New Quick 
measure measure 
Sensitivity 
(preview) v 
Sensitivity 
Publish 
X 1 Total Costs = 
SIJMXQ Sales, Sales (Quantity] 
* RELATED( 
sources v 
ProductsCCost] ) j

4 Calculating Profit and Profit Margin

To calculate Profit we need to subtract the costs from the revenue and with DAX measures it is very easy to do so by referring to the already created measures. Again, I click on New measure and type in the following Total Sales followed by the minus sign and Total Costs. Notice how the color changes to purple.

Name 
Measu re 
S' Format 
: Tots-_ Sales: 
Data category Uncategorized 
Fome table Vain Veasures 
Structure 
X I Total Profits 
Auto 
Costs] 
New Quick 
measure measure

5 Formatting the measures

Before calculating the Profit Margin I want to show another way of writing a measure. First of all, we need to make sure that the three measures are in the Main Measures Table. In the Fields pane I can see that I have the three measures.

Visualizations 
Va Iues 
> 
o 
Fields 
P Search 
> Customers 
> Dates 
> Locations 
Main Measures 
Column' 
Measure 
Total Costs 
Total Sales 
> Products 
Sales 
> Salespersons 
Add data fields here 
Drill through 
Cross-report 
Off O 
Keep all filters 
Add drill-through fields here

However, I want this table to be placed on top of the other tables to differentiate it. To do that, I first need to delete the Column 1 inside it. I right-click on Column 1 and click on Delete from model.

Giulio Stefanica 
p Search 
filters on this 
Add data fields here 
Filters on all 
Add data fields here 
Visualizations 
Va Iues 
Add data fields h 
Drill through 
Cross-report 
Off O 
Keep all filters 
Add drill-througfr 
> 
o 
Check 
Fields 
P Search 
> Customers 
> Dates 
> Locations 
Main Measures 
Create hierarchy 
New measure 
New column 
New quick measure 
Rename 
Delete from model 
Hide 
Delete from model 
View hidden 
Unhide all 
Collapse all 
Expand all 
New group 
Add to filters 
Add to drill through 
vofits 
ales

By doing so, the Main Measures table has now been sent to top. The icon also changed automatically to one with a calculator confirming that this is a table containing measures.

Visualizations 
Va Iues 
Add data fields here 
Drill through 
> 
Fields 
P Search 
Main Measures 
Total Costs 
Total Profits 
Total Sales 
Customers 
Dates 
Locations 
Produ Cts 
> 
Sales 
> Salespersons

Now it’s time to format the measures and choose how many decimal places the numbers should have. I click on the measure and then under Measure tools, I choose Currency and two decimal places for all three measures.

Home 
Insert 
Modeling 
View 
Help 
External Tools 
Table tools 
Measure tools 
Name 
Total Sales 
S' Format Currency 
$ % 408 
Data category Uncategorized 
Home table Main Measures 
Structure 
New Qu ick 
measure measure 
X 1 Total Sales = 
sumx( sales, 
Sales [Quantity] 
* RELATED( Products[Current Price) ) )

6 Write DAX from Table

Another way of writing a DAX measure is by right-clicking in one of the tables from the Fields pane. This means that the new measures will be automatically stored in that table. So, I go ahead and create the measure for Profit Margins. Here I use DIVIDE and add a 0 for Alternate Result. At the end I format this to a percentage with two decimal places.

7 Create a Table

Lastly, let’s put all these measures inside a table. From the Visualizations pane, I click on Table. In the Values I drag the measures one by one. The resulting table shows the Revenue, Costs, Profits and Profit Margins for all the data.

DAX Measures used in this video:

Total SalesSUMX( Sales,
Sales[Quantity] * RELATED( Products[Current Price] ) )
Total CostsSUMX( Sales, Sales[Quantity] * RELATED( ‘Product'[Cost] ) )
Total Profits[Total Sales] – [Total Costs]
Profit MarginsDIVIDE( [Total Profits], [Total Sales], 0 )