DAX Snippet Library
Common DAX patterns you can copy and paste into Power BI. Search by keyword or filter by category.
What it does
- ·Twenty-plus ready-to-paste DAX patterns
- ·Search by keyword (e.g. "ytd", "rank", "switch")
- ·Filter by category — time intelligence, ranking, filtering, and more
- ·Each snippet ships with a one-click copy button
21 snippets
Year-to-Date (YTD)
Cumulative measure from the start of the year through the current date.
Sales YTD = TOTALYTD ( [Total Sales], 'Date'[Date] )
Year-over-Year Change
Compares current period vs same period last year.
Sales YoY % = VAR _curr = [Total Sales] VAR _prev = CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) ) RETURN DIVIDE ( _curr - _prev, _prev )
Rolling 12-Month Sales
Trailing 12 months of sales — useful for moving averages.
Sales R12M =
CALCULATE (
[Total Sales],
DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -12, MONTH )
)Month-to-Date (MTD)
Cumulative value from start of the current month.
Sales MTD = TOTALMTD ( [Total Sales], 'Date'[Date] )
Previous Year
Same metric one year back, useful for variance.
Sales PY = CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
Rank by Sales (Dense)
Dense rank for products by total sales.
Product Rank =
RANKX (
ALL ( 'Product'[Product Name] ),
[Total Sales],,
DESC,
DENSE
)Top N Products
Returns sales only for the top N products, blank otherwise.
Sales Top 10 =
VAR _rank =
RANKX ( ALL ( 'Product'[Product Name] ), [Total Sales],, DESC, DENSE )
RETURN
IF ( _rank <= 10, [Total Sales], BLANK () )Percent of Total
Each row's share of the grand total.
% of Total =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALL ( 'Product' ) )
)Calculate with Filter
Apply a fixed filter to a measure.
Sales Online = CALCULATE ( [Total Sales], 'Sales'[Channel] = "Online" )
ALL — Remove Filters
Strip all filters from a table or column.
Total Across All = CALCULATE ( [Total Sales], ALL ( 'Product' ) )
KEEPFILTERS
Add a filter without overriding the existing context.
Sales Premium = CALCULATE ( [Total Sales], KEEPFILTERS ( 'Product'[Tier] = "Premium" ) )
Switch True
Cleaner alternative to nested IFs.
Status =
SWITCH ( TRUE (),
[Sales] >= 1000000, "High",
[Sales] >= 100000, "Medium",
[Sales] > 0, "Low",
"No Sales"
)Distinct Count
Count of unique customer IDs.
Active Customers = DISTINCTCOUNT ( 'Sales'[CustomerID] )
Average per Customer
Average sales amount per customer (not per row).
Avg Sales per Customer = DIVIDE ( [Total Sales], DISTINCTCOUNT ( 'Sales'[CustomerID] ) )
Sum with Filter
Sum a column conditionally.
Total Refunds = CALCULATE ( SUM ( 'Sales'[Amount] ), 'Sales'[Type] = "Refund" )
Date Table (DAX)
Generate a calendar date table.
Date =
ADDCOLUMNS (
CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2030, 12, 31 ) ),
"Year", YEAR ( [Date] ),
"Month", FORMAT ( [Date], "MMM" ),
"Month Number", MONTH ( [Date] ),
"Quarter", "Q" & QUARTER ( [Date] ),
"Year-Month", FORMAT ( [Date], "YYYY-MM" )
)Is Current Month
Boolean flag — useful for slicers and dynamic titles.
Is Current Month =
IF (
YEAR ( 'Date'[Date] ) = YEAR ( TODAY () )
&& MONTH ( 'Date'[Date] ) = MONTH ( TODAY () ),
"Current Month",
"Other"
)Fiscal Year (July Start)
Compute fiscal year when fiscal calendar starts in July.
Fiscal Year =
"FY" & IF (
MONTH ( 'Date'[Date] ) >= 7,
YEAR ( 'Date'[Date] ) + 1,
YEAR ( 'Date'[Date] )
)Conditional Color
Return a hex color based on a condition (use in conditional formatting).
Color Status =
SWITCH ( TRUE (),
[Sales YoY %] > 0.05, "#2A9D8F",
[Sales YoY %] > 0, "#E9C46A",
"#E76F51"
)Dynamic Title
Card visual title that reacts to slicer selections.
Dashboard Title =
"Sales for "
& SELECTEDVALUE ( 'Region'[Region], "All Regions" )
& " — " & FORMAT ( TODAY (), "MMM YYYY" )Concatenate Multi-Select
Join multiple selected values with a separator.
Selected Regions =
CONCATENATEX (
VALUES ( 'Region'[Region] ),
'Region'[Region],
", "
)