Daily DAX : Day 376 TRUNC
Power BI DAX: TRUNC Function
Overview
The TRUNC function in DAX (Data Analysis Expressions) truncates a number to an integer by removing the decimal part. It does not round the number — it simply chops off the fractional portion.
Syntax
TRUNC(number, [digits])
- number (Required): The numeric value you want to truncate.
- digits (Optional): Number of decimal places to keep. Default is 0.
Example:
TRUNC(12.789) → 12
TRUNC(-8.95) → -8 (truncates toward zero)
TRUNC(45.678, 2) → 45.67
Key Behavior
- Truncates toward zero: Positive and negative numbers are cut off at the decimal point.
- Not the same as rounding: Use
ROUNDif you need rounding. - Useful for grouping or extracting integer parts without rounding bias.
Use Cases
| Use Case | Description | Example |
|---|---|---|
| Extract Integer Part | Get whole number from a decimal value. | Integer Sales = TRUNC([Total Sales]) |
| Bucket Values | Group data into integer-based buckets (e.g., age groups, price tiers). | Price Tier = TRUNC([Price], 0) * 10 |
| Avoid Rounding Bias | Prevent rounding from skewing averages or totals. | Truncated Score = TRUNC([Score], 1) |
| Time Calculations | Strip fractional days from datetime differences. | Whole Days = TRUNC([End Date] - [Start Date]) |
TRUNC vs INT vs FLOOR
| Function | 12.7 | -12.7 | Behavior |
|---|---|---|---|
TRUNC(x) |
12 | -12 | Truncates toward zero |
INT(x) |
12 | -13 | Truncates toward negative infinity |
FLOOR(x, 1) |
12 | -13 | Rounds down to nearest multiple |
Practical Example
Scenario: You have sales data with decimal values, and you want to analyze only the whole dollar amounts.
Whole Dollar Sales = TRUNC([Total Sales])
This removes cents, ensuring no rounding affects your grouping or aggregation.
Use TRUNC when you need clean integer extraction without rounding.
Comments
Post a Comment