Hello, young data explorer! Welcome to Module Fourteen. In this module, we will learn about DAX โ Data Analysis Expressions. DAX is a language that helps us create powerful calculations in Power BI. Think of DAX as a magic wand that can answer any question you have about your data. You can create new columns, make calculations, and build advanced measures. By the end of this module, you will be able to write your own DAX formulas and make your reports smarter!
Chidi has a magic calculator. He can ask it questions like: "What is the total sales for this year?" or "What is the average score of students?" and it answers instantly. The magic calculator is like DAX โ it can answer any question about your data. You just need to write the right formula, and the answer appears!
Definition: DAX stands for Data Analysis Expressions. It's a language used to create calculations in Power BI.
Why important: DAX allows you to ask questions and get answers from your data.
Simple explanation: It's like a magic language that turns data into answers.
Real-life example: A store uses DAX to calculate total sales.
School example: A teacher uses DAX to calculate average scores.
Home example: You use DAX to calculate total expenses.
Nigerian example: A bank uses DAX to calculate total deposits.
Illustration:
Data + DAX = Answers Sales Table + DAX = Total Sales
Mini summary: DAX is the language for creating calculations in Power BI.
Definition: Calculated columns are added to a table and evaluated row by row. Measures are calculated on the fly based on the context.
Why important: Knowing the difference helps you use DAX correctly.
Simple explanation: Calculated columns are like permanent additions. Measures are like temporary answers that change.
Real-life example: Calculated column: "Profit" = [Sales] - [Cost]. Measure: Total Sales = SUM([Sales]).
School example: Calculated column: "Total Score" = [Exam] + [Assignment]. Measure: Average Score = AVERAGE([Total Score]).
Home example: Calculated column: "Adjusted Price" = [Price] + [Tax]. Measure: Total Budget = SUM([Adjusted Price]).
Nigerian example: Calculated column: "Net Salary" = [Gross] - [Tax]. Measure: Total Payroll = SUM([Net Salary]).
Illustration:
Calculated Column: Adds data to a table (row by row). Measure: Calculates data on the fly (dynamic).
Mini summary: Calculated columns are row-level; measures are dynamic.
Definition: SUM adds all the numbers in a column.
Why important: It's the most used function for totals.
Simple explanation: It adds up numbers, just like a calculator.
Real-life example: Total Sales = SUM(Sales[Amount]).
School example: Total Marks = SUM(Grades[Score]).
Home example: Total Expenses = SUM(Budget[Amount]).
Nigerian example: Total Votes = SUM(Votes[Count]).
How to write: Measure = SUM(Table[Column]).
Illustration:
Sales[Amount] = [100, 150, 200] SUM(Sales[Amount]) = 450
Mini summary: SUM adds numbers.
Definition: AVERAGE calculates the average of a column.
Why important: To find the typical value.
Simple explanation: It adds all numbers and divides by the count.
Real-life example: Average Sales = AVERAGE(Sales[Amount]).
School example: Average Score = AVERAGE(Grades[Score]).
Home example: Average Expense = AVERAGE(Budget[Amount]).
Nigerian example: Average Rainfall = AVERAGE(Weather[Rainfall]).
How to write: Measure = AVERAGE(Table[Column]).
Illustration:
Sales[Amount] = [100, 150, 200] AVERAGE(Sales[Amount]) = 150
Mini summary: AVERAGE finds the mean.
Definition: COUNT counts the number of rows in a column.
Why important: To know how many items.
Simple explanation: It counts rows.
Real-life example: Number of Orders = COUNT(Orders[OrderID]).
School example: Number of Students = COUNT(Students[StudentID]).
Home example: Number of Expenses = COUNT(Budget[Amount]).
Nigerian example: Number of Voters = COUNT(Voters[VoterID]).
How to write: Measure = COUNT(Table[Column]).
Illustration:
Sales[Amount] = [100, 150, 200] COUNT(Sales[Amount]) = 3
Mini summary: COUNT counts rows.
Definition: MAX finds the highest value, MIN finds the lowest value.
Why important: To know the extremes.
Simple explanation: MAX gives the biggest, MIN gives the smallest.
Real-life example: Highest Sales = MAX(Sales[Amount]).
School example: Highest Score = MAX(Grades[Score]).
Home example: Highest Expense = MAX(Budget[Amount]).
Nigerian example: Maximum Rainfall = MAX(Weather[Rainfall]).
How to write: Measure = MAX(Table[Column]).
Illustration:
Sales[Amount] = [100, 150, 200] MAX = 200, MIN = 100
Mini summary: MAX finds the largest, MIN finds the smallest.
Definition: CALCULATE changes the context in which a calculation is performed.
Why important: It's the most powerful function in DAX.
Simple explanation: It's like a magnifying glass that focuses on specific data.
Real-life example: Total Sales for Year 2025 = CALCULATE(SUM(Sales[Amount]), Sales[Year] = 2025).
School example: Average Score for Math = CALCULATE(AVERAGE(Grades[Score]), Grades[Subject] = "Math").
Home example: Total Expenses for Food = CALCULATE(SUM(Budget[Amount]), Budget[Category] = "Food").
Nigerian example: Total Votes in Lagos = CALCULATE(SUM(Votes[Count]), Votes[State] = "Lagos").
How to write: Measure = CALCULATE(expression, filter).
Illustration:
CALCULATE(SUM(Sales[Amount]), Sales[Year] = 2025) โ Sum of sales only for 2025.
Mini summary: CALCULATE changes the calculation context.
Definition: FILTER returns a table that is filtered based on a condition.
Why important: It's used inside CALCULATE to apply complex filters.
Simple explanation: It's like a sieve that only keeps specific rows.
Real-life example: Total Sales for Products with Price > 100 = CALCULATE(SUM(Sales[Amount]), FILTER(Products, Products[Price] > 100)).
School example: Average Score for Students with Attendance > 90% = CALCULATE(AVERAGE(Grades[Score]), FILTER(Students, Students[Attendance] > 0.9)).
Home example: Total Expenses for Categories with Amount > 1000 = CALCULATE(SUM(Budget[Amount]), FILTER(Budget, Budget[Amount] > 1000)).
Nigerian example: Total Votes for States with Population > 1M = CALCULATE(SUM(Votes[Count]), FILTER(States, States[Population] > 1000000)).
Illustration:
FILTER(Products, Products[Price] > 100) โ Only keeps products with price > 100.
Mini summary: FILTER creates a filtered table.
Definition: TOTALYTD calculates the year-to-date total.
Why important: To track cumulative totals over time.
Simple explanation: It adds up values from the start of the year to the current date.
Real-life example: Sales YTD = TOTALYTD(SUM(Sales[Amount]), Dates[Date]).
School example: Marks YTD = TOTALYTD(SUM(Grades[Score]), Dates[Date]).
Home example: Expenses YTD = TOTALYTD(SUM(Budget[Amount]), Dates[Date]).
Nigerian example: Revenue YTD = TOTALYTD(SUM(Revenue[Amount]), Dates[Date]).
How to write: Measure = TOTALYTD(expression, dates column).
Illustration:
TOTALYTD(SUM(Sales[Amount]), Calendar[Date]) โ Total sales from Jan 1 to today.
Mini summary: TOTALYTD gives year-to-date totals.
Definition: SAMEPERIODLASTYEAR returns the same period in the previous year.
Why important: To compare performance year-over-year.
Simple explanation: It gives you the same dates but one year ago.
Real-life example: Sales Last Year = CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR(Dates[Date])).
School example: Scores Last Year = CALCULATE(AVERAGE(Grades[Score]), SAMEPERIODLASTYEAR(Dates[Date])).
Home example: Expenses Last Year = CALCULATE(SUM(Budget[Amount]), SAMEPERIODLASTYEAR(Dates[Date])).
Nigerian example: Revenue Last Year = CALCULATE(SUM(Revenue[Amount]), SAMEPERIODLASTYEAR(Dates[Date])).
Illustration:
CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR(Calendar[Date])) โ Sales for the same period last year.
Mini summary: SAMEPERIODLASTYEAR compares with the previous year.
Definition: ALL removes all filters from a table or column.
Why important: Useful for calculating percentages of totals.
Simple explanation: It ignores any filters and looks at everything.
Real-life example: % of Total Sales = DIVIDE(SUM(Sales[Amount]), CALCULATE(SUM(Sales[Amount]), ALL(Sales))).
School example: % of Total Marks = DIVIDE(SUM(Grades[Score]), CALCULATE(SUM(Grades[Score]), ALL(Grades))).
Home example: % of Total Expenses = DIVIDE(SUM(Budget[Amount]), CALCULATE(SUM(Budget[Amount]), ALL(Budget))).
Nigerian example: % of Total Votes = DIVIDE(SUM(Votes[Count]), CALCULATE(SUM(Votes[Count]), ALL(Votes))).
Illustration:
DIVIDE(SUM(Sales[Amount]), CALCULATE(SUM(Sales[Amount]), ALL(Sales))) โ Percentage of each product's sales out of total.
Mini summary: ALL removes filters.
Definition: Row context refers to a single row being evaluated. Filter context refers to the filters applied to the data.
Why important: Understanding context is key to writing correct DAX.
Simple explanation: Row context is like looking at one student's score. Filter context is like looking at all students from a particular class.
Real-life example: Row context: calculating profit for each product. Filter context: calculating total sales for a region.
School example: Row context: calculating total marks for a student. Filter context: calculating average marks for a subject.
Home example: Row context: calculating cost per item. Filter context: calculating total cost for a category.
Nigerian example: Row context: calculating tax per citizen. Filter context: calculating total tax for a state.
Illustration:
Row Context: Each row is evaluated individually. Filter Context: All rows that meet certain criteria.
Mini summary: Row context = one row; Filter context = filtered rows.
Definition: DIVIDE handles division safely, returning an alternate value if the denominator is zero.
Why important: Prevents errors in calculations.
Simple explanation: It's like a safety net for division.
Real-life example: Sales per Product = DIVIDE(SUM(Sales[Amount]), COUNT(Products[ProductID]), 0).
School example: Average per Student = DIVIDE(SUM(Grades[Score]), COUNT(Students[StudentID]), 0).
Home example: Average per Category = DIVIDE(SUM(Budget[Amount]), COUNT(Categories[CategoryID]), 0).
Nigerian example: Average per State = DIVIDE(SUM(Population[Total]), COUNT(States[StateID]), 0).
How to write: DIVIDE(numerator, denominator, alternate result).
Illustration:
DIVIDE(10, 0, 0) โ 0 DIVIDE(10, 2, 0) โ 5
Mini summary: DIVIDE safely handles division by zero.
Definition: Common patterns are frequently used DAX formulas.
Why important: They save time and are proven to work.
Simple explanation: It's like using a recipe instead of inventing a new dish.
Illustration:
Measure: Total Sales = SUM(Sales[Amount]) Measure: Sales YTD = TOTALYTD([Total Sales], Calendar[Date]) Measure: % of Total = DIVIDE([Total Sales], CALCULATE([Total Sales], ALL(Sales)))
Mini summary: Use common patterns for efficient DAX.
Definition: Tips for writing efficient and accurate DAX formulas.
Why important: Good DAX is fast, accurate, and easy to understand.
Simple explanation: It's like following rules to build a strong house.
Illustration:
1. Use measures for dynamic calculations. 2. Use CALCULATE for context changes. 3. Use DIVIDE for safe division. 4. Document your measures.
Mini summary: Follow best practices for quality DAX.
How to Create a Measure in Power BI:
How to Create a Calculated Column:
Did you know that DAX was originally developed for Power Pivot in Excel? Now it's used in Power BI, Analysis Services, and even in Excel!
Calculated Column: +----------+----------+----------+ | Product | Price | Tax | +----------+----------+----------+ | Toy | 100 | = Price*0.1 (calculated row by row) | +----------+----------+----------+ Measure: Total Sales = SUM(Sales[Amount]) (dynamic calculation)
Expression โ CALCULATE โ Result โ โ SUM(Sales) Filter: Year = 2025 โ โ โ CALCULATE(SUM(Sales), Sales[Year]=2025) โ Total Sales for 2025
| Feature | Calculated Column | Measure |
|---|---|---|
| Evaluation | Row by row | On the fly |
| Storage | Stored in table | Not stored |
| Performance | Slower | Faster |
| Use case | Row-level calculations | Aggregations |
| Function | What it does | Example |
|---|---|---|
| SUM | Adds numbers | SUM(Sales[Amount]) |
| AVERAGE | Calculates mean | AVERAGE(Sales[Amount]) |
| COUNT | Counts rows | COUNT(Sales[OrderID]) |
| MAX | Finds highest | MAX(Sales[Amount]) |
| MIN | Finds lowest | MIN(Sales[Amount]) |
Congratulations! You have completed Module Fourteen on Introduction to DAX. You learned:
You now have the skills to create powerful calculations and make your reports truly intelligent!
Match the function to its description:
| Function | Description |
|---|---|
| 1. SUM | A. Calculates the mean |
| 2. AVERAGE | B. Adds numbers |
| 3. COUNT | C. Finds the highest value |
| 4. MAX | D. Counts rows |
| 5. CALCULATE | E. Changes context |
Answers: 1-B, 2-A, 3-D, 4-C, 5-E
Scenario 1: You have a sales table with columns: Product, Amount, Date. Write a DAX measure to calculate total sales for the year 2025.
Scenario 2: You have a student grades table with columns: Student, Subject, Score. Write a DAX measure to calculate the average score for Math.
In groups, choose a dataset (e.g., sales data). Create at least five DAX measures: Total Sales, Average Sales, Max Sales, Min Sales, and Sales YTD. Present your measures and explain how they work.
Load a sample dataset into Power BI. Create a calculated column (e.g., Profit = Sales - Cost) and a measure (e.g., Total Sales = SUM(Sales)). Write at least three more measures of your choice.
Project: DAX Sales Dashboard
Build a sales dashboard using DAX. Include measures for: Total Sales, Total Sales YTD, Sales Last Year, Average Sales per Product, and % of Total Sales. Add a chart that shows sales by month. Use time intelligence functions.
Download a dataset with at least 1,000 rows. Create a data model and write at least 10 DAX measures. Include measures that use CALCULATE, FILTER, and time intelligence. Document your measures.
Write a DAX measure that calculates the moving average of sales over the last 3 months. Use CALCULATE and FILTER. Test it with sample data.
Fill-in-the-Blank: 1. DAX, 2. measure, 3. SUM, 4. CALCULATE, 5. TOTALYTD
True/False: 1-T, 2-F, 3-F, 4-T, 5-F
Matching: 1-B, 2-A, 3-D, 4-C, 5-E
In Module Fifteen, we will learn about Advanced DAX Patterns โ we will explore more complex calculations like ranking, running totals, and dynamic segmentation. You will become a true DAX master!
Before the next class, practice writing DAX measures with CALCULATE and time intelligence. Try to create at least five different measures.
ยฉ 2025 Certified Power Query for Data Analysis Expert โ Module Fourteen