Imagine that you are a data analyst working for a supermarket in Ibadan.
Your manager gives you a sales report and asks you some questions:
Power BI can help you answer these questions using a special formula language called DAX.
DAX allows you to create calculations and perform deeper analysis on your data.
In this module, you will learn the basics of DAX and how to use simple formulas to calculate useful information.
DAX stands for Data Analysis Expressions.
It is a formula language used in Power BI, Excel Power Pivot and Analysis Services to perform calculations and analyse data.
You can think of DAX as a way of giving Power BI instructions about what to calculate.
For example, you can tell Power BI to:
Instead of calculating everything manually, you can write a DAX formula and let Power BI perform the calculation.
DAX is like a calculator inside Power BI. You write formulas, and Power BI uses your data to produce answers.
Power BI can create charts directly from imported data. However, sometimes you need to perform calculations that are not already available in your tables.
This is where DAX becomes useful.
DAX helps you:
A supermarket has the following sales records:
| Product | Sales Amount |
|---|---|
| Rice | 500000 |
| Beans | 300000 |
| Milk | 200000 |
| Bread | 150000 |
The manager wants to know the total sales.
You can write a DAX formula to add all the sales amounts together.
Total Sales = SUM(Sales[Sales Amount])
The answer is โฆ1,150,000.
A DAX formula is an instruction that tells Power BI what calculation to perform.
Most DAX formulas contain a name, an equals sign and an expression.
Example:
Total Sales = SUM(Sales[Sales Amount])
Let's understand each part:
The table and column reference is written as Sales[Sales Amount].
DAX has many functions. A function is a ready-made instruction that performs a particular calculation.
Let's explore some beginner-friendly functions.
The SUM function adds all the numbers in a column.
Example:
Total Sales = SUM(Sales[Sales Amount])
If your sales amounts are 500,000, 300,000 and 200,000, the result will be 1,000,000.
The AVERAGE function calculates the mean of a group of numbers.
Example:
Average Score = AVERAGE(Results[Score])
If three students score 70, 80 and 90, the average is 80.
The COUNT function counts the number of non-blank values in a column containing numbers.
Example:
Number of Scores = COUNT(Results[Score])
If the Score column contains five numeric values, the result will be 5.
The COUNTA function counts non-blank values in a column. It can count text and numbers.
Example:
Number of Customers = COUNTA(Customers[Customer Name])
This calculates how many customer-name entries are not blank.
The MAX function finds the highest value in a column.
Example:
Highest Score = MAX(Results[Score])
If the scores are 60, 75, 90 and 85, the result will be 90.
The MIN function finds the smallest value in a column.
Example:
Lowest Score = MIN(Results[Score])
If the scores are 60, 75, 90 and 85, the result will be 60.
DISTINCTCOUNT counts the number of different values in a column.
Example:
Unique Customers = DISTINCTCOUNT(Sales[Customer ID])
If the same customer appears in several sales records, the customer is counted only once.
| Function | What It Does |
|---|---|
| SUM | Adds numbers together. |
| AVERAGE | Calculates the average. |
| COUNT | Counts non-blank numeric values. |
| COUNTA | Counts non-blank values. |
| MAX | Finds the highest value. |
| MIN | Finds the lowest value. |
| DISTINCTCOUNT | Counts unique values. |
A measure is a DAX calculation that produces a result when Power BI needs it in a report.
Measures are commonly used to calculate totals, averages, percentages and other summaries.
Imagine that you have imported a Sales table containing a Sales Amount column.
Follow these steps:
Total Sales = SUM(Sales[Sales Amount])
You can now drag the Total Sales measure onto a Card visual to display the total sales amount.
A calculated column is a new column created using a DAX formula.
It calculates a value for each row in a table.
Imagine that a supermarket has a table containing product sales:
| Product | Sales Amount | Cost |
|---|---|---|
| Rice | 500000 | 350000 |
| Beans | 300000 | 210000 |
| Milk | 200000 | 140000 |
The manager wants to calculate the profit for each product.
You can create a calculated column using:
Profit = Sales[Sales Amount] - Sales[Cost]
Power BI calculates the profit for every row.
| Product | Sales Amount | Cost | Profit |
|---|---|---|---|
| Rice | 500000 | 350000 | 150000 |
| Beans | 300000 | 210000 | 90000 |
| Milk | 200000 | 140000 | 60000 |
Profit = Sales[Sales Amount] - Sales[Cost]
Measures and calculated columns are both created using DAX, but they perform different jobs.
| Measure | Calculated Column |
|---|---|
| Calculates a result when used in a report. | Calculates a value for each row. |
| Responds to report filters and selections. | Values are calculated during data refresh or model processing. |
| Commonly used for totals and averages. | Commonly used to create new row-level information. |
| Does not store a separate value for every row. | Stores calculated values in the model. |
Suppose you want to calculate the total profit for a supermarket.
A measure could be:
Total Profit = SUM(Sales[Sales Amount]) - SUM(Sales[Cost])
Suppose you want to calculate the profit for each individual sales record.
A calculated column could be:
Profit = Sales[Sales Amount] - Sales[Cost]
DAX uses mathematical operators to perform calculations.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 10 + 5 = 15 |
| - | Subtraction | 10 - 5 = 5 |
| * | Multiplication | 10 * 5 = 50 |
| / | Division | 10 / 5 = 2 |
| = | Equal to or formula assignment | Profit = 100 - 40 |
| > | Greater than | Score > 50 |
| < | Less than | Score < 50 |
If a supermarket sells a product for โฆ50,000 and the cost is โฆ30,000:
Profit = Sales[Sales Amount] - Sales[Cost]
The profit is โฆ20,000.
You can calculate profit as a percentage of sales using:
Profit Percentage = DIVIDE([Total Profit], [Total Sales], 0)
Format the measure as a percentage in Power BI.
The DIVIDE function is useful because it handles division by zero more safely than the ordinary division operator.
The IF function allows Power BI to make a decision based on a condition.
It works like asking a question:
Imagine that a school wants to classify students according to their examination scores.
Students who score 50 or above will receive a Pass result. Students who score below 50 will receive a Fail result.
You can create a calculated column:
Result Status = IF(Results[Score] >= 50, "Pass", "Fail")
Power BI checks each student's score and displays the appropriate result.
| Student | Score | Result Status |
|---|---|---|
| Ada | 85 | Pass |
| John | 45 | Fail |
| Mary | 70 | Pass |
CALCULATE is one of the most important DAX functions.
It evaluates an expression after changing or applying filter conditions.
Let's use a supermarket example.
Your sales table contains different products. Your manager wants to know the total sales made from rice only.
You can write:
Rice Sales =
CALCULATE(
SUM(Sales[Sales Amount]),
Sales[Product] = "Rice"
)
Power BI calculates the total sales where the product is Rice.
CALCULATE is especially useful when you want a measure to answer a specific business question.
Filter context means the filters and selections that affect a calculation in Power BI.
For example, imagine that your sales report contains records for January, February and March.
If you select January using a slicer, a Total Sales measure can show January's sales only.
If you select February, the same measure can show February's sales.
The measure does not need to be rewritten every time you select a different month.
| Month | Sales |
|---|---|
| January | 500000 |
| February | 300000 |
| March | 400000 |
Create this measure:
Total Sales = SUM(Sales[Sales Amount])
When you place Month and Total Sales in a chart, Power BI can calculate sales separately for each month.
When you select January in a slicer, the measure can display โฆ500,000, assuming the slicer is connected to the sales data correctly.
You are working as a junior data analyst for a supermarket in Ibadan.
Your manager wants you to calculate total sales, total costs and profit.
Use this data:
| Product | Sales Amount | Cost | Quantity |
|---|---|---|---|
| Rice | 500000 | 350000 | 50 |
| Beans | 300000 | 210000 | 30 |
| Milk | 200000 | 140000 | 40 |
| Bread | 150000 | 90000 | 75 |
| Cooking Oil | 250000 | 180000 | 25 |
Use this formula:
Total Sales = SUM(Sales[Sales Amount])
Total Cost = SUM(Sales[Cost])
Total Profit = [Total Sales] - [Total Cost]
Total Quantity = SUM(Sales[Quantity])
Average Sales = AVERAGE(Sales[Sales Amount])
Profit Percentage =
DIVIDE([Total Profit], [Total Sales], 0)
Format Profit Percentage as a percentage.
Always use the correct table and column names in your formulas.
Make sure that columns used for mathematical calculations contain suitable numeric data.
Remember that measures calculate results based on the current report context, while calculated columns create values for individual rows.
Column references in DAX use square brackets.
Correct example:
SUM(Sales[Sales Amount])
Use DIVIDE when you need safer division.
Profit Percentage = DIVIDE([Total Profit], [Total Sales], 0)
Always compare your calculations with the original data to make sure your results are correct.
| Term | Meaning |
|---|---|
| DAX | Data Analysis Expressions, the formula language used in Power BI. |
| Function | A ready-made instruction that performs a calculation. |
| Measure | A calculation evaluated in the current report context. |
| Calculated Column | A new column calculated for each row using a formula. |
| SUM | Adds values together. |
| AVERAGE | Calculates the mean of a group of numbers. |
| COUNT | Counts non-blank numeric values. |
| MAX | Finds the highest value. |
| MIN | Finds the smallest value. |
| IF | Returns different results depending on a condition. |
| CALCULATE | Evaluates an expression using modified filter conditions. |
| Filter Context | The filters and selections that affect a calculation. |
1. What does DAX stand for?
2. What is DAX used for in Power BI?
3. Which DAX function adds values together?
4. Which function calculates the average of numbers?
5. What is a measure?
6. What does a calculated column do?
7. Which DAX function finds the highest value?
8. What is the purpose of the IF function?
9. Which function is useful for division that handles division by zero?
10. What does CALCULATE do?
In this module, you learned how to use DAX to perform calculations in Power BI.
You discovered that:
You have completed Module 7 of Introduction to Power BI.
You can now create basic DAX formulas to answer business questions and perform calculations.
In the next module, you will learn how to combine charts, cards, slicers and other visuals to create reports that people can explore.
You will discover:
Get ready to build reports that allow people to explore their data!