Skip to content

Instantly share code, notes, and snippets.

@will-fong
Last active August 24, 2021 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will-fong/dab39b5ce8143102d4faad1875217f25 to your computer and use it in GitHub Desktop.
Save will-fong/dab39b5ce8143102d4faad1875217f25 to your computer and use it in GitHub Desktop.
Mastering DAX
--https://dax.do/HjDPEEQlk8ADkj
--13.10 - Sales by year
EVALUATE
SUMMARIZECOLUMNS (
'Date'[Calendar Year],
"Sales", [Sales Amount],
"Pct",
DIVIDE (
[Sales Amount],
CALCULATE ( [Sales Amount], ALL ( 'Date'[Calendar Year] ) )
),
"Position",
IF (
NOT ISBLANK ( [Sales Amount] ),
RANKX ( ALLSELECTED ( 'Date'[Calendar Year] ), [Sales Amount] )
)
)
--https://dax.do/7A7NUrculwKMu1
--13.20 - Filtering and grouping sales - Basic
EVALUATE
VAR ProductTable =
TREATAS ( { "Red", "Blue" }, 'Product'[Color] )
VAR BrandTable =
TREATAS ( { "Fabrikam", "Contoso" }, 'Product'[Brand] )
VAR Output =
SUMMARIZECOLUMNS (
'Product'[Color],
Customer[Education],
ProductTable,
BrandTable,
"Sales", [Sales Amount]
)
RETURN
Output
--13.20 - Filtering and grouping sales - Advanced
EVALUATE
VAR ProductTable =
TREATAS ( { "Red", "Blue" }, 'Product'[Color] )
VAR BrandTable =
TREATAS ( { "Fabrikam", "Contoso" }, 'Product'[Brand] )
VAR FilteredTable =
SUMMARIZECOLUMNS (
'Product'[Color],
Customer[Education],
ProductTable,
BrandTable,
"Sales", [Sales Amount]
)
VAR SalesAvg =
AVERAGEX ( FilteredTable, [Sales] )
VAR Output =
FILTER ( FilteredTable, [Sales Amount] > SalesAvg )
RETURN
Output
--https://dax.do/hd8am1kc5xgSMw
--13.30 - Using TOPN and GENERATE
EVALUATE
GENERATE (
/*
Recall values will return all years regardless if there was a sale, and summarize is used to remove nulls
*/
--VALUES ( 'Date'[Calendar Year] ),
SUMMARIZE (
'Sales',
'Date'[Calendar Year]
),
VAR SalesTopN =
TOPN (
VAR n = 3 RETURN n,
(
ADDCOLUMNS (
VALUES ( 'Product'[Product Name] ),
"SalesAmt", [Sales Amount],
"Product Not in Top N", 0
)
),
[SalesAmt]
)
VAR SalesTop =
SUMX ( SalesTopN, [SalesAmt] )
VAR SalesAll = [Sales Amount]
VAR SalesRemaining = SalesAll - SalesTop
VAR ProductOther = { ( "Other Products", SalesRemaining, 1 ) }
VAR Result =
UNION ( SalesTopN, ProductOther )
RETURN
Result
)
ORDER BY
'Date'[Calendar Year],
[Product Not in Top N]
--https://dax.do/rJo1hRVcTvrTBB/
--13.40 - Sales to top customers - Basic
EVALUATE
VAR CustomersTop =
TOPN ( VAR n = 10 RETURN n, VALUES ( Sales[CustomerKey] ), [Sales Amount] )
VAR Output =
SUMMARIZECOLUMNS (
'Date'[Calendar Year],
CustomersTop,
"Sales", [Sales Amount]
)
RETURN
Output
ORDER BY 'Date'[Calendar Year]
--13.40 - Sales to top customers - Advanced
EVALUATE
VAR CustomersTop =
TOPN ( VAR n = 10 RETURN n, VALUES ( Sales[CustomerKey] ), [Sales Amount] )
RETURN
GENERATE (
SUMMARIZE ( 'Sales', 'Date'[Calendar Year] ),
ADDCOLUMNS (
VAR SalesTopN =
CALCULATE ( [Sales Amount], CustomersTop )
VAR LabelTopN =
ROW ( "Name", "Sales of Top 10 Product", "Sales", SalesTopN )
VAR LabelOthers =
ROW (
"Name", "Sales of Product Not in Top 10",
"Sales", [Sales Amount] - SalesTopN
)
VAR SalesTable =
UNION ( LabelTopN, LabelOthers )
VAR Output = SalesTable
RETURN
Output,
"Percentage of Sales", DIVIDE ( [Sales], [Sales Amount] )
)
)
ORDER BY
'Date'[Calendar Year],
[Name] DESC
--https://dax.do/luCncYJDk1I9Ne/
--13.50 - Sales of top three colors
DEFINE
MEASURE Sales[Top3] =
SUMX (
TOPN (
3,
ADDCOLUMNS ( VALUES ( 'Product'[Color] ), "Sales", [Sales Amount] ),
[Sales]
),
[Sales]
)
EVALUATE
SUMMARIZECOLUMNS ( 'Product'[Brand], "SalesTop3", [Top3] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment