You are on page 1of 21

Power BI Class Six

James Stratton

November 20, 2021


Plan for Today Harder formulas in DAX Measures Finishing up

Plan for Today

Yesterday: calculate columns in DAX using simple formulas


Today:
§ Calculate columns in DAX using harder formulas
§ Calculate measures in DAX

Data: same as last time

1 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

Changing data types

2 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

Changing data types

We want words to have data type text, and numbers to have data
type whole number or decimal number
If a number has data type text, then you won’t be able to make
visualizations with it!

3 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

OR in DAX
We have a column for whether a farmer uses a generator
We also have a column for whether a farmer uses solar energy
What if we want to look at farmers who use either a generator or
solar energy?
In DAX, the symbol || means ‘OR’
§ You type this by pressing shift and the key just above the ‘Enter’ key
§ If you ever have to Google this, it’s called the ‘double pipe’ symbol

So our formula is . . .
SolarOrGenerator “ IF(‘My Data’[Does farmer use solar energy?] = “Yes”
|| ‘My Data’[Does farmer use generator?] = “Yes”, “Uses solar or
generator’, “Does not use solar or generator”)

4 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

OR in DAX

Now you try!


I want to make a new column called ‘BigFarmer’. A farmer is a big
farmer if their total revenue or total agri cost is above 10 million
MMK

5 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

AND in DAX

What if we want to look at farmers who use both a generator and


solar energy?
In DAX, the symbol && means ‘OR’
So our formula is . . .
SolarAndGenerator “ IF(‘My Data’[Does farmer use solar energy?] =
“Yes” && ‘My Data’[Does farmer use generator?] = “Yes”, “Uses both
solar and generator’, “Does not use both solar and generator”)

6 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

AND in DAX

Remember: we created BigFarmer


A farmer is a BigFarmer if total revenue is above 10 million MMK or
total agri cost is above 10 million MMK
Now create a new column, BigFarmer2
We want a farmer to be a BigFarmer2 is the farmer has both total
revenue and total agri cost above 10 million MMK

7 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

VLOOKUP in DAX

What is VLOOKUP in Excel?


When would you want to use VLOOKUP?

8 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

LOOKUPVALUE in DAX

The data we have at the moment has branches


Imagine that instead of branches, we want regions
Luckily, the sheet I sent today has an index table
The index table tells us which branches are in which regions
How would we want to do this in Excel?

9 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

LOOKUPVALUE in DAX

Power BI does not have VLOOKUP, but it has a very similar formula
called LOOKUPVALUE
LOOKUPVALUE is a bit harder to learn, but is actually easier to use
than VLOOKUP

10 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

LOOKUPVALUE in DAX
Upload the index table of regions and branches into Power BI
§ Thanks to Khin Maung Swe for sending this!

Power BI will suggest a relationship between the tables

We can use this relationship to tell the region that matches up to


each branch
11 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

LOOKUPVALUE in DAX
To do a LOOKUPVALUE, we need three columns
§ An index table with what we want to search
§ An index table with the result we want
§ A data table with what we want to search

In Power BI, the formula for LOOKUPVALUE is . . .


NEWCOLUMN = LOOKUPVALUE(’Index Table’[Result], ’Index
Table’[Search], ’Data Table’[Search])
So, in this case, the formula is . . .
Region “ LOOKUPVALUE(’Regions and Branches’[Region], ’Regions and
Branches’[BranchName], ’My Data’[BranchName])
Afterwards, make a graph which shows average revenue by region
If you have time, add a drill-down to branch!
12 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

LOOKUPVALUE in DAX

Now see if you can do this yourself!


The original data only has product types
What if we want crops instead?

13 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

LOOKUPVALUE in DAX

Upload the products and crops sheet into Power BI


Can you see that Power BI is not using the first row as headers?
To fix this . . .
§ Go to ‘Data’
§ Click ‘Edit queries’
§ Click ‘Products and crops’
§ Click ‘Use first row as headers’
§ Click ‘Close and apply’

14 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

LOOKUPVALUE in DAX

Now Power BI knows that the data are related

15 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

LOOKUPVALUE in DAX

The sheet ‘My Data’ is a data table with a list of product types
The sheet ‘Products and Crops’ is an index table with a list of
product types and crops
Then use LOOKUPVALUE to get to a list of crops
Remember, the formula in Power BI is . . .
NEWCOLUMN = LOOKUPVALUE(’Index Table’[Result], ’Index
Table’[Search], ’Data Table’[Search])
Afterwards, make a graph which shows average revenue, with crops
on the axis

16 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

Introduction to Measures

We just learnt how to use DAX to create a new column


Now we will learn to create a measure
A measure is just a single number
We create a measure using DAX, like what we just learnt

17 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

Creating a measure
Imagine that every client’s income grows at 5% per year
How much do we predict total client revenue will be next year? What
about the year after?
We want to make a graph like . . .

18 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

Dashboard

Can you arrange what we made into a Dashboard?


You can share the Dashboard with a public link

19 / 20
Plan for Today Harder formulas in DAX Measures Finishing up

Tomorrow

This is the end of the lessons I have planned!


We will spend some time doing practice
We will learn more while we are practising!

20 / 20

You might also like