0% found this document useful (0 votes)
5 views

Code Model

Uploaded by

vutuanthanh.da
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Code Model

Uploaded by

vutuanthanh.da
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

## ITERATOR FUNCTION

* 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]
)
)

** Higher grain summarization

Revenue Avg =
AVERAGEX(
Sales,
Sales[Order Quantity] * Sales[Unit Price] * (1 - Sales[Unit Price Discount
Pct])
)

-- SOME BASIC DAX FUNCTIONS

+ Revenue YoY % =
DIVIDE(
[Revenue]
- CALCULATE(
[Revenue],
SAMEPERIODLASTYEAR('Date'[Date])
),
CALCULATE(
[Revenue],
SAMEPERIODLASTYEAR('Date'[Date])
)
)
IF(<logical_test>, <value_if_true>[, <value_if_false>])

DIVIDE(<numerator>, <denominator>[, <alternate_result>])

*** 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:

RANKX(<table>, <expression>[, <value>[, <order>[, <ties>]]])

*** 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.

Product Quantity Rank =


IF(
HASONEVALUE('Product'[Product]),
RANKX(
ALL('Product'[Product]),
[Quantity],
,
,
DENSE
)
)

----> Total product not Ranked

**** 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

CALCULATE(<expression>, [[<filter1>], <filter2>]…)

Revenue Red or Blue = CALCULATE([Revenue], 'Product'[Color] IN {"Red", "Blue"})

Revenue Expensive Products = CALCULATE([Revenue], 'Product'[List Price] > 1000)

Revenue High Margin Products =


CALCULATE(
[Revenue],
FILTER(
'Product',
'Product'[List Price] > 'Product'[Standard Cost] * 2
)
)

Revenue Red =
CALCULATE(
[Revenue],
FILTER(
'Product',
'Product'[Color] = "Red"
)
)

## Remove filters

Revenue Total Region = CALCULATE([Revenue], REMOVEFILTERS('Sales Territory'))

Revenue % Total Region =


VAR CurrentRegionRevenue = [Revenue]
VAR TotalRegionRevenue =
CALCULATE(
[Revenue],
REMOVEFILTERS('Sales Territory')
)
RETURN
DIVIDE(
CurrentRegionRevenue,
TotalRegionRevenue
)

Revenue % Total Group =


VAR CurrentRegionRevenue = [Revenue]
VAR TotalGroupRevenue =
CALCULATE(
[Revenue],
REMOVEFILTERS(
'Sales Territory'[Region],
'Sales Territory'[Country]
)
)
RETURN
DIVIDE(
CurrentRegionRevenue,
TotalGroupRevenue
)

----***----

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:

ISFILTERED - Returns TRUE when a passed-in column reference is directly filtered.


ISCROSSFILTERED - Returns TRUE when a passed-in column reference is indirectly
filtered. A column is cross-filtered when a filter that is applied to another
column in the same table, or in a related table, affects the reference column by
filtering it.
ISINSCOPE - Returns TRUE when a passed-in column reference is the level in a
hierarchy of levels.

Revenue % Total Country =


VAR CurrentRegionRevenue = [Revenue]
VAR TotalCountryRevenue =
CALCULATE(
[Revenue],
REMOVEFILTERS('Sales Territory'[Region])
)
RETURN
IF(
ISINSCOPE('Sales Territory'[Region]),
DIVIDE(
CurrentRegionRevenue,
TotalCountryRevenue
)
)

Customer Segment =
VAR CustomerRevenue = SUM(Sales[Sales Amount])
RETURN
IF(CustomerRevenue < 2500, "Low", "High")

----> chi ra ket qua 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

You might also like