Code Model
Code Model
* Complex summarization
-- Revenue =
SUMX(
Sales,
Sales[Order Quantity] * Sales[Unit Price] * (1 - Sales[Unit Price Discount
Pct])
)
# LEARN DAX
https://learn.microsoft.com/en-us/dax/related-function-dax
-- RELATED
--- FILTER( 'InternetSales_USD',
RELATED('SalesTerritory'[SalesTerritoryCountry])<>"United States")
** IMPORTANT NOTE: If your formula needs to reference columns in other tables, and
model relationships exist between the tables, use the RELATED function for the one-
side relationship or the RELATEDTABLE function for the many-side relationship.
-- Discount =
SUMX(
Sales,
Sales[Order Quantity]
* (
RELATED('Product'[List Price]) - Sales[Unit Price]
)
)
Revenue Avg =
AVERAGEX(
Sales,
Sales[Order Quantity] * Sales[Unit Price] * (1 - Sales[Unit Price Discount
Pct])
)
+ Revenue YoY % =
DIVIDE(
[Revenue]
- CALCULATE(
[Revenue],
SAMEPERIODLASTYEAR('Date'[Date])
),
CALCULATE(
[Revenue],
SAMEPERIODLASTYEAR('Date'[Date])
)
)
IF(<logical_test>, <value_if_true>[, <value_if_false>])
*** IN ***
ANZ Revenue =
CALCULATE(
[Revenue],
Customer[Country-Region] IN {
"Australia",
"New Zealand"
}
)
** DAX variables
Revenue YoY % =
VAR RevenuePriorYear =
CALCULATE(
[Revenue],
SAMEPERIODLASTYEAR('Date'[Date])
)
RETURN
DIVIDE(
[Revenue] - RevenuePriorYear,
RevenuePriorYear
)
The RANKX DAX function is a special iterator function you can use to calculate
ranks. Its syntax is as follows:
*** The ALL function is used to return all rows in a model table or values in one
or more columns, and it ignores all filters.
**** It's important to understand how filter context works. It guides you in
defining the correct formula for your calculations. As you write more complex
formulas, you'll identify times when you need to add, modify, or remove filters to
achieve the desired result.
# Modify filter context
Revenue Red =
CALCULATE(
[Revenue],
FILTER(
'Product',
'Product'[Color] = "Red"
)
)
## Remove filters
----***----
Revenue Red =
CALCULATE(
[Revenue],
KEEPFILTERS('Product'[Color] = "Red")
)
Revenue Shipped =
CALCULATE (
[Revenue],
USERELATIONSHIP('Date'[DateKey], Sales[ShipDateKey])
)
**** the commission rate is 10 percent of revenue for all countries/regions except
the United States. In the United States, salespeople earn 15 percent commission
Sales Commission =
[Revenue]
* IF(
HASONEVALUE('Sales Territory'[Country]),
IF(
VALUES('Sales Territory'[Country]) = "United States",
0.15,
0.1
)
)
*** Three other functions that you can use to test filter state are:
Customer Segment =
VAR CustomerRevenue = SUM(Sales[Sales Amount])
RETURN
IF(CustomerRevenue < 2500, "Low", "High")
Customer Segment =
VAR CustomerRevenue = CALCULATE(SUM(Sales[Sales Amount]))
RETURN
IF(CustomerRevenue < 2500, "Low", "High")
Sales Commission =
SUMX(
VALUES('Sales Territory'[Region]),
CALCULATE(
[Revenue]
* IF(
VALUES('Sales Territory'[Country]) = "United States",
0.15,
0.1
)
)
)
----> se hien thi cot duoc gia tri cot total, above DAX, total row is BLANK
********* https://learn.microsoft.com/en-us/dax/dax-glossary
********* IMPORTANT