---
# System prepended metadata

title: DAX **samples

---

# DAX **samples

## 1. CALCULATE(Expression, SetFilter1, SetFilter2,...); CALCULATE =SUM+FILTER =SUMIF
* =CALCULATE(SUM(Sales[Margin]), Sales[Year]=2021)
* =CALCULATE([Sales per day],Sales[Year]=2021,Sales[ProductKey]=313)
* =CALCULATE( [Total Sales],Sales[TransType]=1||Sales[TransType]=3)
* Product Sales above selected list price = CALCULATE([Total Sales],Filter(Products[ListPrice]>=[MinListThreshold]))

## 2.All(table name[column1], table name[column2],....)-----Remove a filter   ---pivot table母體裡面被選擇的all
*[NetSales-All products] = CALCULATE([NetSales],All[Sales[ProductKey]])
*[Selected Product Pct]= [NetSales]/[NetSale-All Products]-----Add a measure

## 3.ALLEXCEPT(table name,[column1 to leave alone],table name,[column2 to leave alone])
## 4.Allselected---pivot table 母體裡面所有樣本。 
*[NetSales for all selected months] = CALCULATE([NetSales],AllSelected[Sales[Months]])
*[Pct of AllSelected Months]= [NetSales]/[NetSales for AllSelected Months]-----Add a measure
## 5.FILTER(table name,欄位)
*Transaction for hightest price = CALCULATE(COUNTROWS(Sales),FILTER(Products,Products[ListPrice] = MAX(Products[ListPrice])))
## 6.COUNTAX(),COUNTX(),MINX(),AVERAGEX(),MAXX(),SUMX(裡面選VALUES()或FILTER()),RANKX--X就是跨表格，自設計
*[Customer Sales Rank ]=  RANKX(ALL(Customers[FullName],[TotalSales])------RANKX

*[Max Single Country Sales] = MAXX(VALUES(Sales[Country],[TotalSales]))----MAXX 挑選每個國家裏面最大銷售額

*=SUMX(VALUES(Products[ModelName],[SalesPerDay]))----

*= SUMX(FILTER(InternetSales, InternetSales[SalesTerritoryID]=5),[Freight])----
## 7.SWITCH(TRUE(), [條件1]<10,"A",[條件2]<5,"B","C")

> ### DISTINCTCOUNT(), 




