13. DAX: Data Analysis Expressions
13.6 SUM, SUMX and Other DAX Aggregation Functions with Example
Quick answer: the short, self-contained version of this lesson is the guide DAX COUNT, COUNTA, DISTINCTCOUNT and COUNTROWS.
SUM vs SUMX in Power BI with example — in 30 seconds
What: SUM adds one column. SUMX goes row by row, calculates something, then adds the results.
Why: Qty × Price must be multiplied on each row first. Multiplying two totals gives a wrong answer.
Tiny example: SUMX gives ₹205 (right). SUM(Qty) × SUM(Price) gives ₹810 (wrong).
SUM vs SUMX in Power BI with example — 30 सेकंदात
काय: SUM एक column ची बेरीज करतो. SUMX row-by-row जातो, काहीतरी calculate करतो, आणि मग results ची बेरीज करतो.
का: Qty × Price प्रत्येक row वर आधी गुणायचं असतं. दोन totals गुणले तर उत्तर चुकतं.
छोटं उदाहरण: SUMX ₹205 देतो (बरोबर). SUM(Qty) × SUM(Price) ₹810 देतो (चूक).
SUM vs SUMX in Power BI with example — 30 सेकंड में
क्या: SUM एक column का जोड़ करता है. SUMX row-by-row चलता है, कुछ calculate करता है, फिर results का जोड़ करता है.
क्यों: Qty × Price हर row पर पहले गुणा करना होता है. दो totals गुणा किए तो जवाब ग़लत आता है.
छोटा उदाहरण: SUMX ₹205 देता है (सही). SUM(Qty) × SUM(Price) ₹810 देता है (ग़लत).
First look at the data (before → after):
Same before / after tables as text
Before — Order lines
| Product | Qty | Unit Price (₹) | Qty × Price |
|---|---|---|---|
| Gokul Milk | 2 | 30 | 60 |
| Misal Masala | 1 | 85 | 85 |
| Ladi Pav | 3 | 20 | 60 |
About this data: This table uses columns: Product, Qty, Unit Price (₹), Qty × Price. This table uses columns: Measure, Value. Values shown are the lesson examples already on this page (not a new dataset).
Formula / click path
Sales SUMX = SUMX(Orders, Orders[Qty] * Orders[Unit Price])
Wrong = SUM(Orders[Qty]) * SUM(Orders[Unit Price])
After — Results
| Measure | Value |
|---|---|
| Sales SUMX (row by row) | ₹205 |
| SUM(Qty) × SUM(Price) — wrong | 6 × 135 = ₹810 |
Do it step by step:
- Click Home › New measure.
- Type the SUMX formula. Press Enter.
- Make a card with Sales SUMX. It shows ₹205.
- Make the wrong measure too, only to see ₹810. Then delete it.
एक-एक step करून बघा:
- Home › New measure वर click करा.
- SUMX formula type करा. Enter दाबा.
- Sales SUMX चा card बनवा. तो ₹205 दाखवतो.
- फक्त ₹810 बघण्यासाठी चुकीचा measure पण बनवा. मग तो delete करा.
एक-एक step करके देखिए:
- Home › New measure पर click कीजिए.
- SUMX formula type कीजिए. Enter दबाइए.
- Sales SUMX का card बनाइए. वह ₹205 दिखाता है.
- सिर्फ़ ₹810 देखने के लिए ग़लत measure भी बनाइए. फिर उसे delete कीजिए.
SUM, AVERAGE, MIN, MAX
Avg Line Value = AVERAGE(Orders[Amount])
Largest Order Line = MAX(Orders[Amount])
Fastest Delivery (mins) = MIN(Orders[Delivery Time Mins])
Slowest Delivery (mins) = MAX(Orders[Delivery Time Mins])
First Order Date = MIN(Orders[Order Date])
Last Order Date = MAX(Orders[Order Date])
MIN and MAX also accept two scalar values: MAX([Total Sales], 0).
COUNT, COUNTA, COUNTROWS, DISTINCTCOUNT, COUNTBLANK
Lines With Delivery Time = COUNT(Orders[Delivery Time Mins]) -- non-blank values
Order Lines = COUNTROWS(Orders) -- number of rows (order lines)
Total Orders = DISTINCTCOUNT(Orders[Order ID]) -- unique orders
Dark Stores With Orders = DISTINCTCOUNT(Orders[Store ID])
Missing Delivery Times = COUNTBLANK(Orders[Delivery Time Mins])
COUNTAcounts non-blank values of any type.COUNTROWSis generally preferred overCOUNTwhen you just need the number of rows.DISTINCTCOUNTcounts unique values (including BLANK as one value, if present).
Iterators: SUMX, AVERAGEX (and MINX, MAXX, COUNTX)
The "X" functions take a table and an expression. They evaluate the expression for each row (row context) and then aggregate.
Gross Sales (before discount) = SUMX(Orders, Orders[Amount] + Orders[Discount])
Sales at List Price = SUMX(Orders, Orders[Quantity] * RELATED(Product[Unit Price]))
Avg Sales per Customer = AVERAGEX(VALUES(Customer[Customer ID]), [Total Sales])
Avg Items per Order = AVERAGEX(VALUES(Orders[Order ID]), [Total Quantity])
Busiest Store Orders = MAXX(VALUES(DarkStore[Store Name]), [Total Orders])
SUM vs SUMX
SUM(Orders[Amount]) is actually shorthand for SUMX(Orders, Orders[Amount]). Use SUMX when you must calculate something per row first (like Quantity × Price) and then add up. This avoids creating a calculated column.
Ravindra Bagale's Tip
Friends, pay attention: using SUM on a text column is a very common beginner error, usually because the column was never converted from text in Power Query. Fix the type at the source, not in DAX. Also remember that COUNT counts non-blank values, COUNTROWS counts rows, and DISTINCTCOUNT counts unique values. Practise, and it will feel very easy.
Ravindra Bagale's Tip – मराठी
मित्रांनो, लक्ष द्या: text column वर SUM वापरणं ही beginners ची खूप common error आहे, बहुधा कारण Power Query मध्ये तो column text मधून कधी convert केलाच नव्हता. Type source वरच दुरुस्त करा, DAX मध्ये नाही. आणि लक्षात ठेवा, COUNT non-blank values मोजतं, COUNTROWS rows मोजतं, आणि DISTINCTCOUNT unique values मोजतं. Practice करा, मग एकदम सोपं वाटेल.
Ravindra Bagale's Tip – हिंदी
दोस्तों, ध्यान दो: text column पर SUM लगाना beginners की बहुत common error है, आमतौर पर इसलिए कि Power Query में वह column कभी text से convert ही नहीं किया गया. Type source पर ही ठीक करो, DAX में नहीं. और याद रखो, COUNT non-blank values गिनता है, COUNTROWS rows गिनता है, और DISTINCTCOUNT unique values गिनता है. Practice करो, फिर बहुत आसान लगेगा.
Loading DAX practice tasks...
- Total fees (Easy): Write Total Fee = SUM of Visits[Fee].
- Average waiting time (Easy): Write Avg Wait = AVERAGE of Visits[WaitMins].
- Longest wait (Easy): Write Max Wait = largest WaitMins.
- Number of visits (Easy): Write Visit Count = COUNTROWS of Visits.
- Unique patients (Medium): Write Patients = number of different Patient names.
- Count text values (Medium): Write Insured Values = COUNTA of Visits[Insured].
- Cheapest fee (Medium): Write Min Fee = smallest Visits[Fee].
- Fees by department (Medium): The box shows a row per Doctors Department. Write Total Fee = the sum of Visits[Fee].
- Fee per waiting minute (Hard): Write Fee per Min = AVERAGEX over Visits of Fee ÷ WaitMins.
- Highest daily fee (Hard): Write Best Day Fee = the highest total fee earned on a single visit date.
Revision card
Interview card