DAX Formulas
DAX Formulas
in Power BI
SUM
FORMULA
SUM Formula in Power BI
Definition: SUM is a simple aggregation function that adds up all the
values in a single column.
Example
Total_Sales = SUM(SalesData[Sales])
Use Case: Use SUM when you want to sum up all the values in a
numeric column without any complex row-by-row operations.
Example
Tax = SalesData[Total_Sales]*18/100
Use Cases: Suitable for scenarios where you need a value for each row
in the table, such as adding a new field derived from existing data (e.g.,
creating a "Total Price" column by multiplying "Quantity" and "Price").
In Power BI, both calculated columns and measures are used to perform calculations, but
they serve different purposes and behave differently. Here's a comparison of the two:
SUMX
FORMULA
SUMX Formula in Power BI
Definition: SUMX is an iterator function that performs row-by-row
calculations and then sums the results.
Example
Total_Sales2 = SUMX(SalesData,SalesData[Quantity]*SalesData[Unit Price (INR)])
Use Case: Use SUMX when you need to perform a calculation for each
row before summing, such as multiplying two columns together or
applying conditional logic.
Performance: SUMX can be slower than SUM because it performs
calculations on each row individually before summing. It's ideal for more
complex scenarios that require row context.
Difference between SUM and SUMX Formula in Power BI
In Power BI, both SUM and SUMX are used to perform summation, but they work differently
and are used in different scenarios. Here's a breakdown of the key differences:
COUNT
FORMULA
Count Formula in Power BI
Example
Number of Customers = COUNT('Table'[Customer ID])
Use Case: When you want to count the non-empty values in a column.
COUNTA
FORMULA
COUNTA Formula in Power BI
Definition: COUNTA counts the number of non-blank values in a
column, including text, numeric values, and logical values even counts
Boolean values where only Count will not count Boolean data type
Example
Number of Reviews = COUNTA('Table'[Review Status])
Use Case: When you need to count all non-blank values, regardless of the
data type (text, numbers, etc.).
COUNTBLANK
FORMULA
COUNTBLANK Formula in Power BI
Example
Blank Reveiws = COUNTBLANK('Table'[Review Points])
Use Case: When you want to count the number of blank or missing values
in a column.
COUNTROWS
FORMULA
COUNTROWS Formula in Power BI
Example
Total Records = COUNTROWS('Table')
Use Case: When you want to count the total number of rows in a table or in
a filtered table.
DISTINCTCOUNT
FORMULA
DISTINCTCOUNT Formula
Example
Unique States = DISTINCTCOUNT('Table'[State])
Use Case: When you need to count the distinct (unique) values in a
column, excluding blanks.
COUNTX
FORMULA
COUNTX Formula
Example
Count Reviews >= 5 = COUNTX('Table', IF('Table'[Review Points] >= 5, 1, BLANK()))
Example
Count True = COUNTAX(FILTER('Table','Table'[Review Status]=true),'Table'[Review Status])
Example
Maharashtra Count = COUNTROWS(FILTER('Table','Table'[State]="Maharashtra"))
Use Case: When you want to count rows based on a condition or set of
conditions.
Summary of DAX Counting Functions:
These functions provide flexibility depending on whether you're counting all rows, distinct
values, non-blank values, or values based on expressions and conditions.
Date Formulas
Extract
Day/Month/Year
Extract Day/Month/Year
Syntax
DATEDIFF(<start_date>, <end_date>, <interval>)
Example
DATEDIFF(Sales[OrderDate], Sales[ShipDate], DAY)
Create Custom
Calendar
1 . Create Custom Calendar in Power Query
Formula
= List.Dates(#date(2023,1,1),731,#duration(1,0,0,0))
Formula
= CALENDAR(MIN(SalesData[Date]),MAX(SalesData[Date]))
Formula
= CALENDARAUTO()
Formula
Sales MTD = TOTALMTD(SUM(Sales[SalesAmount]), DateTable[Date])
Sales QTD = TOTALQTD(SUM(Sales[SalesAmount]), DateTable[Date])
Sales YTD = TOTALYTD(SUM(Sales[SalesAmount]), DateTable[Date])
Formula
In Power BI, Network Days refers to the count of working days (typically
Monday through Friday) between two specified dates, excluding weekends
and optionally excluding holidays.
Networkdays DAX Formula
Network Days = NETWORKDAYS("01-01-2023","31-01-2023",1,Holidays)
Formula
Formula
Formula
SAMEPERIODLASTYEAR(<dates>)
Formula
SAMEPERIODLASTYEAR(<dates>)