Power BI DAX Functions Every Analyst Should Know

Loading

DAX (Data Analysis Expressions) is a powerful formula language used in Power BI, Excel Power Pivot, and SQL Server Analysis Services (SSAS) to create custom calculations and analyze data. Here are some essential DAX functions that every analyst should know:


1. Aggregation Functions

  • SUM: Adds up all the numbers in a column.
  Total Sales = SUM(Sales[Amount])
  • AVERAGE: Calculates the average of values in a column.
  Avg Sales = AVERAGE(Sales[Amount])
  • MIN/MAX: Returns the minimum or maximum value in a column.
  Max Sales = MAX(Sales[Amount])
  • COUNT/COUNTA/COUNTROWS: Counts the number of rows or non-blank values.
  Total Orders = COUNTROWS(Orders)

2. Logical Functions

  • IF: Returns one value if a condition is true and another if false.
  Sales Category = IF(Sales[Amount] > 1000, "High", "Low")
  • AND/OR: Combines multiple conditions.
  High Priority = IF(AND(Sales[Amount] > 1000, Sales[Region] = "West"), "Yes", "No")
  • SWITCH: Evaluates multiple conditions and returns a value for the first true condition.
  Region Group = SWITCH(Sales[Region], "North", "Group 1", "South", "Group 2", "Other")

3. Filter Functions

  • FILTER: Returns a table filtered based on a condition.
  High Sales = CALCULATE(SUM(Sales[Amount]), FILTER(Sales, Sales[Amount] > 1000))
  • ALL: Removes filters from a table or column.
  Total Sales All Regions = CALCULATE(SUM(Sales[Amount]), ALL(Sales[Region]))
  • ALLEXCEPT: Removes all filters except those on specified columns.
  Sales by Region = CALCULATE(SUM(Sales[Amount]), ALLEXCEPT(Sales, Sales[Region]))

4. Time Intelligence Functions

  • TOTALYTD/ TOTALQTD/ TOTALMTD: Calculates year-to-date, quarter-to-date, or month-to-date totals.
  YTD Sales = TOTALYTD(SUM(Sales[Amount]), 'Date'[Date])
  • SAMEPERIODLASTYEAR: Compares values with the same period in the previous year.
  Sales LY = CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR('Date'[Date]))
  • DATESBETWEEN: Returns dates between a specified start and end date.
  Sales Last 7 Days = CALCULATE(SUM(Sales[Amount]), DATESBETWEEN('Date'[Date], TODAY() - 7, TODAY()))

5. Text Functions

  • CONCATENATE: Combines text strings.
  Full Name = CONCATENATE(Customer[FirstName], Customer[LastName])
  • LEFT/RIGHT/MID: Extracts a substring from a text string.
  First Initial = LEFT(Customer[FirstName], 1)
  • FIND/SEARCH: Finds the position of a substring within a text string.
  Position = FIND(" ", Customer[FullName])

6. Relationship Functions

  • RELATED: Fetches a value from a related table.
  Product Category = RELATED(Products[Category])
  • RELATEDTABLE: Returns a table of related rows.
  Orders for Product = COUNTROWS(RELATEDTABLE(Orders))

7. Mathematical Functions

  • DIVIDE: Safely divides two numbers and handles division by zero.
  Profit Margin = DIVIDE(Sales[Profit], Sales[Revenue])
  • ROUND: Rounds a number to a specified number of digits.
  Rounded Sales = ROUND(Sales[Amount], 2)

8. Table Functions

  • SUMMARIZE: Creates a summary table.
  Sales Summary = SUMMARIZE(Sales, Sales[Region], "Total Sales", SUM(Sales[Amount]))
  • ADDCOLUMNS: Adds calculated columns to a table.
  Sales with Tax = ADDCOLUMNS(Sales, "Tax", Sales[Amount] * 0.1)

9. Evaluation Functions

  • CALCULATE: Modifies the context of a calculation.
  West Sales = CALCULATE(SUM(Sales[Amount]), Sales[Region] = "West")
  • CALCULATETABLE: Similar to CALCULATE but returns a table.
  High Sales Table = CALCULATETABLE(Sales, Sales[Amount] > 1000)

10. Information Functions

  • ISBLANK: Checks if a value is blank.
  Is Missing = IF(ISBLANK(Sales[Amount]), "Missing", "OK")
  • ISERROR: Checks if a value is an error.
  Is Valid = IF(ISERROR(Sales[Amount] / Sales[Quantity]), "Invalid", "Valid")

11. Advanced Functions

  • RANKX: Ranks values in a table.
  Sales Rank = RANKX(ALL(Sales[Product]), SUM(Sales[Amount]))
  • X Functions (ITERATORS): Functions like SUMX, AVERAGEX, and MAXX iterate over a table and perform calculations.
  Total Sales by Product = SUMX(Sales, Sales[Quantity] * Sales[Price])

12. Context Manipulation

  • EARLIER: Refers to an earlier row context in nested calculations.
  Running Total = 
  CALCULATE(
      SUM(Sales[Amount]), 
      FILTER(Sales, Sales[Date] <= EARLIER(Sales[Date]))
  )

13. Date and Time Functions

  • DATEDIFF: Calculates the difference between two dates.
  Days to Deliver = DATEDIFF(Orders[OrderDate], Orders[DeliveryDate], DAY)
  • EOMONTH: Returns the last day of the month.
  End of Month = EOMONTH(Sales[Date], 0)

By mastering these DAX functions, analysts can unlock the full potential of Power BI and create powerful, dynamic reports and dashboards. Practice and experimentation are key to becoming proficient in DAX!

Leave a Reply

Your email address will not be published. Required fields are marked *