Power BI Lesson 5: Your First DAX — SUM, COUNTROWS, DIVIDE

📎 This article includes 2 downloadable practice files ↓

⏱ 2 min read

📘 Power BI & DAX Beginner Course · Lesson 5 of 10

In this article
  1. Sales, cost, margin
  2. Counting
  3. DIVIDE, not /
  4. Variables: VAR … RETURN
  5. Test in a table visual
  6. Where beginners go wrong
  7. Practice

With a good model, most measures are short. This lesson builds the core set; check each one against the expected-results workbook.

Sales, cost, margin

Total Sales = SUMX ( Sales, Sales[Qty] * RELATED ( Products[Price] ) )
Total Cost  = SUMX ( Sales, Sales[Qty] * RELATED ( Products[Cost] ) )
Margin      = [Total Sales] - [Total Cost]
Margin %    = DIVIDE ( [Margin], [Total Sales] )

SUMX goes row by row through Sales, multiplies, then adds up: an iterator, like SUMPRODUCT. Measures can use other measures in square brackets, which keeps formulas short and consistent.

If your Sales table already has an Amount column, Total Sales = SUM ( Sales[Amount] ) is enough.

Counting

Orders              = COUNTROWS ( Sales )
Customers Who Bought = DISTINCTCOUNT ( Sales[CustomerID] )
Avg Order Value     = DIVIDE ( [Total Sales], [Orders] )

DIVIDE, not /

DIVIDE(a, b) returns blank (or a value you choose as the third argument) when b is zero, instead of an error. Use it for every ratio.

💡 Format measures as you create them: Measure tools > Format: Percentage for Margin %, Whole number with thousands separator for sales. Formats stay with the measure in every visual.

Variables: VAR … RETURN

Margin % (v) =
VAR Sales_ = [Total Sales]
VAR Cost_  = [Total Cost]
RETURN DIVIDE ( Sales_ - Cost_, Sales_ )

Variables make long measures readable and calculate each part once.

Test in a table visual

Put Products[Category] in a table with all the measures. Each row calculates for its category, and the total row calculates for everything, not by adding the rows. That’s filter context, the heart of DAX (Lesson 6).

Where beginners go wrong

Error / symptom Cause
“A single value for column cannot be determined” Column used without an aggregation in a measure; wrap it in SUM/MAX or use SUMX
RELATED error No relationship, or used from the “one” side
Infinity or NaN Used / instead of DIVIDE

Practice

Download the data model and expected results below. Load all four tables into Power BI Desktop and build this lesson’s steps; compare your numbers with the expected-results workbook.

📎 Practice files for this article

  • 📗
    Practice data model (Excel)Four tables: Sales (600 orders, FY 2024-25 and 2025-26), Products, Customers and a Calendar with FY columns.
    ⬇ XLSX · 40 KB
  • 📗
    Expected resultsThe values your measures and visuals should show.
    ⬇ XLSX · 7 KB

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong