You are on page 1of 178

ADVANCED

DAX
for Business Intelligence
With Power BI Expert Aaron Parry

*Copyright 2020, Excel Maven & Maven Analytics, LLC


COURSE STRUCTURE

This is a project-based course, for students looking to build expert-


level DAX skills through hands-on demos & assignments

Course resources include:


Downloadable PDF ebook to serve as a helpful reference when you’re
offline or on the go (or just need a refresher!)

Quizzes and Assignments to test and reinforce key concepts throughout the
course, with detailed step-by-step solutions

Interactive, hands-on demos to keep you engaged, with downloadable


project files that you can use to follow along from home
*Copyright 2020, Excel Maven & Maven Analytics, LLC
COURSE OUTLINE

Address the core DAX concepts you should already know,


1 Prerequisite Skills Review including evaluation context, filter flow, measures, etc.

Introduce the Maven Roasters course project, and build the


2 Introducing the Course Project data model that we’ll analyze throughout the course

Understand the DAX formula and storage engines, data types,


3 The DAX Engines VertiPaq encoding & compression methods, etc.

Review helpful tips for formatting queries, adding


4 Tips & Best Practices comments, handling errors, and using DAX variables

Explore common scalar functions, including rounding,


5 Scalar Functions information, conversion, and logical functions

Review CALCULATE modifiers, context transition,


6 Advanced CALCULATE interactions with tables, etc.

*Copyright 2020, Excel Maven & Maven Analytics, LLC


COURSE OUTLINE

Create calculated tables, review common filter functions,


7 Table & Filter Functions explore ways to create new datasets, etc.

Create calculated joins between physical and virtual


8 Calculated Table Joins tables

Explore expanded tables, physical & virtual relationships,


9 Relationship Functions common relationship functions, etc.

Explore iterator cardinality, nested iterators, context


10 Iterator Functions transition, RANKX, calculation granularity, etc.

Build date tables with DAX, compare custom time periods,


11 Advanced Time Intelligence manage fiscal calendars, etc.

Introduce the performance analyzer, common optimization


12 SNEAK PEEK: Performance Tuning techniques, and performance tuning tools

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SETTING EXPECTATIONS

1 This course is for users who already have basic DAX skills
• It is strongly recommended that you complete our Up & Running with Power BI Desktop course, or have a
solid understanding of measures, calculated columns, evaluation context, and common DAX functions

2 What you see on your screen may not always match mine
• Power BI features are updated frequently (often monthly), so tools and interface options may change over time
• I’m using Windows 10 on a PC/Windows machine (note that Power BI Desktop is currently unavailable for Mac)

3 Our goal is to help you build expert-level DAX skills


• DAX is simple to learn at a basic level, but advanced skills require a deep understanding of storage & calculation
engines, table & filter functions, relationships, iterators, context transition, and more

4 This course focuses on using DAX for Business Intelligence


• We’ll take time to explore the inner-workings of the DAX engines, but our primary focus will be applying DAX to
real-world projects & case studies; we’re here to teach you how to become an analyst, not a programmer

*Copyright 2020, Excel Maven & Maven Analytics, LLC


HELPFUL DAX RESOURCES

DAX Formatter (daxformatter.com) by sqlbi.com is a great tool for


cleaning and formatting your DAX code, with options to
customize based on regional settings or personal preferences

www.Docs.Microsoft.com/en-us/dax DAX Guide (dax.guide) by sqlbi.com is DAX Studio is an open source tool
is the official Microsoft DAX reference a great resource to learn and explore that allows you to write, execute and
guide, and a great resource for DAX functions, category groups, analyze DAX queries, as well as
exploring DAX functions product compatibility, and more troubleshoot and optimize your code

*Copyright 2020, Excel Maven & Maven Analytics, LLC


POWER BI OPTIONS & SETTINGS
PREVIEW FEATURES DATA LOAD REGIONAL SETTINGS

1) In the “Preview Features” tab, 2) In the “Data Load” tab, deselect the 3) In the “Regional Settings” tab, make
deselect any active features while “Update Relationships”, “Autodetect new sure to use the “English (United States)”
you are taking this course relationships after data is loaded” and “Auto locale for import
date/time” options NOTE: You may need to update setting in both
the Current File and Global sections

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PREREQUISITE SKILLS REVIEW

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PREREQUISITE SKILLS REVIEW

This section reviews the core concepts you should already be familiar with,
including data modeling fundamentals (cardinality, filter flow, etc.) and
basic DAX (operators, evaluation context, common measures, etc.)

TOPICS WE’LL REVIEW:

Data & Lookup Tables Primary & Foreign Keys Relationship Cardinality Filter Flow

DAX Operators Common Functions Columns & Measures Evaluation Context

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DATA TABLES VS. LOOKUP TABLES
Models generally contain two types of tables: data (or “fact”) tables, and lookup (or “dimension”) tables
• Data tables contain measurable values or metrics about the business (quantity, revenue, pageviews, etc.)
• Lookup tables provide descriptive attributes about each dimension in your model (customers, products, etc.)

This Calendar Lookup table provides additional attributes about each date (month, year, weekday, quarter, etc.)

This Product Lookup table provides additional attributes about each product (brand, name, retail price, etc.)

This Data Table contains “quantity” values, and connects


to lookup tables via the “date” and “product_id” columns
*Copyright 2018, Excel Maven & Maven Analytics, LLC
PRIMARY VS. FOREIGN KEYS

These columns are foreign keys; they These columns are primary keys; they uniquely identify each
contain multiple instances of each row of a table, and match the foreign keys in related data tables
value, and are used to match the
primary keys in related lookup tables

*Copyright 2018, Excel Maven & Maven Analytics, LLC


RELATIONSHIP CARDINALITY

Cardinality refers to the uniqueness of values in a column


• For our purposes, all relationships in the data model should
follow a “one-to-many” cardinality; one instance of each
primary key, but potentially many instances of each foreign key

In this case, there is only ONE instance of each ProductKey in the Products
table (noted by the “1”), since each row contains attributes of a single product
(Name, SKU, Description, Retail Price, etc)
There are MANY instances of each ProductKey in the Sales_Data table (noted
by the asterisk *), since there are multiple sales associated with each product

*Copyright 2018, Excel Maven & Maven Analytics, LLC


FILTER FLOW

Here we have two data tables (Sales_Data and Returns_Data),


connected to Territory_Lookup

Note the filter directions (shown as arrows) in each relationship;


by default, these will point from the “one” side of the relationship
(lookups) to the “many” side (data)
• When you filter a table, that filter context is passed along to all
related “downstream” tables (following the direction of the arrow)
• Filters cannot flow “upstream” (against the direction of the arrow)

PRO TIP:
Arrange your lookup tables above your data tables in your model as a visual reminder that filters flow “downstream”

*In some cases filters may default to “two-way” depending on your Power BI Desktop settings *Copyright 2018, Excel Maven & Maven Analytics, LLC
FILTER FLOW (CONT.)
In this case, the only valid way filter both Sales and Returns data by
Territory is to use the TerritoryKey field from the Territory_Lookup
table, which is upstream and related to both data tables
• Filtering using TerritoryKey from the Sales table yields incorrect
Returns values, since the filter context cannot flow upstream to
either one of the other tables

• Similarly, filtering using TerritoryKey from the Returns table yields


incorrect Sales data; in addition, only territories that registered
returns are visible in the table (even though they registered sales)

3) Filtering using TerritoryKey from


1) Filtering using TerritoryKey from 2) Filtering using TerritoryKey from the Returns_Data table
the Territory_Lookup table the Sales_Data table
*Copyright 2018, Excel Maven & Maven Analytics, LLC
DATA MODEL BEST PRACTICES

A well-designed model is critical and


ideally should:
✓ Use a star schema with one-to-many (1:*) relationships

✓ Contain relationships with one-way filters (vs. bidirectional)

✓ Contain tables that each serve a specific purpose,


including data (fact) tables and lookup (dim) tables

✓ Only include the data you need for analysis (no redundant
or unnecessary records or fields)

✓ Split out individual date and time components from


DateTime fields

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DAX OPERATORS
Arithmetic Comparison
Meaning Example Meaning Example
Operator Operator
+ Addition 2+7 = Equal to [City]=“Boston”

- Subtraction 5–3 > Greater than [Quantity]>10

* Multiplication 2*6 < Less than [Quantity]<10

/ Division 4/2 >= Greater than or equal to [Unit_Price]>=2.5

^ Exponent 2^5 <= Less than or equal to [Unit_Price]<=2.5

<> Not equal to [Country]<>”Mexico”

Text/Logical Operator Meaning Example

& Concatenates two values to produce one text string [City] & “ “ & [State]

&& Create an AND condition between two logical expressions ([State]=“MA”) && ([Quantity]>10)

|| (double pipe) Create an OR condition between two logical expressions ([State]=“MA”) || ([State]=“CT”)

IN Creates a logical OR condition based on a given list (using curly brackets) ‘Store Lookup’[State] IN { “MA”, “CT”, “NY” }

*Head to www.msdn.microsoft.com for more information about DAX syntax, operators, troubleshooting, etc.
*Copyright 2020, Excel Maven & Maven Analytics, LLC
COMMON DAX FUNCTION CATEGORIES

MATH & STATS LOGICAL TEXT FILTER DATE & TIME


Functions Functions Functions Functions Functions
Basic aggregation Functions for returning Functions to manipulate Lookup functions based Basic date and time
functions as well as information about values text strings or control on related tables and functions as well as
“iterators” evaluated at in a given conditional formats for dates, times filtering functions for advanced time
the row-level expression or numbers dynamic calculations intelligence operations

Common Examples: Common Examples: Common Examples: Common Examples: Common Examples:
• SUM • IF • CONCATENATE • CALCULATE • DATEDIFF
• AVERAGE • IFERROR • FORMAT • FILTER • YEARFRAC
• MAX/MIN • AND • LEFT/MID/RIGHT • ALL • YEAR/MONTH/DAY
• DIVIDE • OR • UPPER/LOWER • ALLEXCEPT • HOUR/MINUTE/SECOND
• COUNT/COUNTA • NOT • PROPER • RELATED • TODAY/NOW
• COUNTROWS • SWITCH • LEN • RELATEDTABLE • WEEKDAY/WEEKNUM
• DISTINCTCOUNT • TRUE • SEARCH/FIND • DISTINCT
• FALSE • REPLACE • VALUES Time Intelligence Functions:
Iterator Functions: • REPT • EARLIER/EARLIEST • DATESYTD
• SUBSTITUTE • HASONEVALUE •
• SUMX DATESQTD
• TRIM • HASONEFILTER •
• AVERAGEX DATESMTD
• UNICHAR • ISFILTERED •
• MAXX/MINX DATEADD
• USERELATIONSHIP •
• RANKX DATESINPERIOD
• COUNTX

*Note: This is NOT a comprehensive list (does not include trigonometry functions, parent/child functions, information functions, or other less common functions)
*Copyright 2020, Excel Maven & Maven Analytics, LLC
CALCULATE

CALCULATE() Evaluates a given expression or formula under a set of defined filters

=CALCULATE(Expression, [Filter1], [Filter2],…)

Name of an existing measure, or a DAX List of simple Boolean (True/False) filter expressions
formula for a valid measure (Note: these require simple, fixed values; you cannot
Examples:
create filters based on other measures)
• [Total Orders] Examples:
• SUM(Returns[ReturnQuantity]) • Territory_Lookup[Country] = “USA”
• Calendar[Year] > 1998

PRO TIP:
CALCULATE works just like SUMIF or COUNTIF in Excel, except it can evaluate measures based on ANY
sort of calculation (not just a sum, count, etc.); it may help to think of it like “CALCULATEIF”

*Copyright 2018, Excel Maven & Maven Analytics, LLC


CALCULATE (EXAMPLE)

Here we’ve defined a new measure named “Bike Returns”, which


evaluates the “Total Returns” measure when the CategoryName
in the Products table equals “Bikes”

HEY THIS IS IMPORTANT!


Wait, why do we see the same repeating values when
CALCULATE modifies and overrules any competing filter context!
we view a matrix with different categories on rows?
In this example, the “Clothing” row has filter context of
Shouldn’t these cells have different filter contexts for CategoryName = “Clothing” (defined by the row label) and
Accessories, Clothing, Components, etc? CategoryName= “Bikes” (defined by the CALCULATE function)
Both cannot be true at the same time, so the “Clothing” filter is
overwritten and the “Bikes” filter (from CALCULATE) takes priority

*Copyright 2018, Excel Maven & Maven Analytics, LLC


CALCULATE CREATES NEW FILTER CONTEXT

CALCULATE If the measure being evaluated contains a CALCULATE


Filters modified by CALCULATE function, filter context is overwritten between Step 1 & Step 2
[CategoryName] = “Bikes”

STEP 1 STEP 2 STEP 3


Filter context is Filters flow “downstream” to Measure formula evaluates
detected & applied all related tables against the filtered table
Product Table

Bikes Product Table

Bikes

Count of rows in the AW_Returns_Data

Products[CategoryName] = “Accessories”
table, filtered down to only rows where
the product category is “Bikes”
= 342
1 1

Product Table AW_Returns_Data


*
Accessories * Bikes
AW_Sales_Data

Bikes

*Copyright 2018, Excel Maven & Maven Analytics, LLC


CALCULATED COLUMNS

Calculated columns allow you to add new, formula-based columns to tables


• No “A1-style” references; calculated columns refer to HEY THIS IS IMPORTANT!
entire tables or columns As a rule of thumb, use calculated
columns when you want to “stamp”
• Calculated columns generate values for each row, which static, fixed values to each row in a
are visible within tables in the Data view table (or use the Query Editor!)
DO NOT use calculated columns for
• Calculated columns understand row context; they’re great aggregation formulas, or to calculate
for defining properties based on information in each row, fields for the “Values” area of a
but generally useless for aggregation (SUM, COUNT, etc.) visualization (use measures instead)

PRO TIP:
Calculated columns are typically used for filtering data, rather than creating numerical or aggregated values

*Copyright 2020, Excel Maven & Maven Analytics, LLC


MEASURES

Measures are DAX formulas used to generate new calculated values


• Like calculated columns, measures reference entire tables or
columns (no A1-style or “grid” references)

• Unlike calculated columns, measure values aren’t visible within HEY THIS IS IMPORTANT!
As a rule of thumb, use measures
tables; they can only be “seen” within a visualization like a (vs. calculated columns) when a
chart or matrix (similar to a calculated field in an Excel pivot) single row can’t give you the
answer (in other words, when you
• Measures are evaluated based on filter context, which means need to aggregate)
they recalculate when the fields or filters around them change
(like when new row or column labels are pulled into a matrix or
when new filters are applied to a report)

PRO TIP:
Use measures to create numerical, calculated values that can be analyzed in the “values” field of a report visual

*Copyright 2018, Excel Maven & Maven Analytics, LLC


RECAP: CALCULATED COLUMNS VS. MEASURES

CALCULATED COLUMNS MEASURES


• Values are calculated based on information from each • Values are calculated based on information from any
row of a table (has row context) filters in the report (has filter context)
• Appends static values to each row in a table and • Does not create new data in the tables themselves
stores them in the model (which increases file size) (doesn’t increase file size)
• Recalculate on data source refresh or when changes • Recalculate in response to any change to filters within
are made to component columns the report
• Primarily used as rows, columns, slicers or filters • Almost always used within the values field of a visual

Calculated columns “live” in tables Measures “live” in visuals

*Copyright 2018, Excel Maven & Maven Analytics, LLC


EVALUATION CONTEXT

Filter & row context tells DAX exactly how to evaluate measures and calculated
columns in your data model (this is key for understanding advanced DAX topics)

FILTER CONTEXT ROW CONTEXT

• Filter context filters the tables in your data model • Row context iterates through the rows in a table
• DAX creates filter context when dimensions are • DAX creates row context when you add calculated
added to rows, columns, slicers & filters in a report columns to your data model
• CALCULATE can be used to systematically create or • Iterator functions (SUMX, RANKX, etc.) use row
modify existing filter context context to evaluate row-level calculations
• Filter context always travels (propagates) from the • Row context doesn't automatically propagate through
ONE side to the MANY side of a table relationship table relationships (need to use RELATED or
RELATEDTABLE functions)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


COURSE PROJECT

*Copyright 2020, Excel Maven & Maven Analytics, LLC


INTRODUCING THE COURSE PROJECT

THE You’ve just been hired as the lead Business Intelligence Analyst for Maven Roasters*, a
SITUATION small-batch coffee chain based in New York City

All you’ve been given is a collection of raw csv files containing sales and inventory records,
THE along with details about the company’s products, customers, stores and employees.
BRIEF Your goal is to use Power BI and DAX to answer key questions about the Maven Roasters
business, including sales trends, inventory, top products, budgets and more.

• Load the raw Maven Roasters data into Power BI


THE
• Create table relationships to build a data model
OBJECTIVES • Use DAX to analyze key questions about the business

*This data is adapted from IBM #Cognos Analytics sample data and is for informational purposes only . These samples are provided “as is” without warranty of any kind. The example companies, organizations, products,
domain names, email addresses, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, person, place, or event is intended or should be inferred.
*Copyright 2020, Excel Maven & Maven Analytics, LLC
THE MAVEN ROASTERS DATA MODEL

Table Name Table Type Definition


This table contains transaction data including date,
Sales by Store Fact store, employee, quantity sold, price, etc.
This table contains details about daily pastry stock level,
Food Inventory Fact quantity used, and waste
Calendar Date Contains date, month, week, quarter and year details

Customer ID, home store, name, email, loyalty details,


Customers Lookup birthdate, gender
Contains details for each product like type, group,
Products Lookup category, unit of measure, wholesale price, retail price,
tax exempt, discount and new product
Details for each store location, address, phone number
Stores Lookup and store type
Details for each staff member, their name, position,
Employees Lookup location and start date

*Copyright 2020, Excel Maven & Maven Analytics, LLC


THE DAX ENGINES

*Copyright 2020, Excel Maven & Maven Analytics, LLC


THE DAX ENGINES

DAX is powered by two internal engines (formula engine & storage engine)
which work together to compress & encode raw data and evaluate DAX queries
• Although these engines operate entirely behind the scenes, knowing how they work will help you
build advanced skills by understanding exactly how DAX thinks

TOPICS WE’LL REVIEW:

Internal DAX Engines Query Evaluation Data & Storage Types

Columnar Structures Compression & Encoding VertiPaq Relationships

*Copyright 2020, Excel Maven & Maven Analytics, LLC


FORMULA & STORAGE ENGINES

Internal DAX There are two distinct engines that work together to process every DAX query:
Engines
the Formula Engine and the Storage Engine

Query Evaluation
FORMULA ENGINE STORAGE ENGINE
Data & Storage • Receives, interprets and executes all • Compresses and encodes raw data, and only
Types DAX requests communicates with the formula engine (doesn’t
understand the DAX language)
• Processes the DAX query then generates
Columnar a list of logical steps called a query plan • Receives a query plan from Formula Engine,
Structures executes it, and returns a datacache
• Works with the datacache sent back
from the storage engine to evaluate the • NOTE: There are two types of storage engines,
Compression & DAX query and return a result based on the type of connection you’re using:
Encoding
1. VertiPaq is used for data stored in-memory
(connected to Power BI via import mode)
VertiPaq
Relationships 2. DirectQuery is used for data read directly
from the source (i.e. Azure, PostgreSQL, SAP)
We’ll focus on the VertiPaq
storage engine in this course

*Copyright 2020, Excel Maven & Maven Analytics, LLC


QUERY EVALUATION IN DEPTH

Internal DAX
Engines
DAX QUERY: MEASURE OUTPUT:
Sales to Females:
Storage
Query Evaluation Engine
$912,184
Storage engine executes the
Data & Storage
plan and sends a datacache
Types back to the formula engine

𝒇 𝒙 DAX query is sent to


the Formula Engine
Columnar
Structures

Formula engine Formula engine runs the DAX


interprets the query query against the datacache,
Compression & Formula Formula and evaluates the result
and sends a query plan
Encoding Engine to the storage engine Engine

VertiPaq
Relationships

*Copyright 2020, Excel Maven & Maven Analytics, LLC


QUERY EVALUATION IN DEPTH

Internal DAX
Engines
DAX QUERY: MEASURE OUTPUT:
Sales to Females:
Storage
Query Evaluation Engine
$912,184
Data & Storage Sure! Here’s the data you’ll
Types need to evaluate that query
DAX here! Aaron would like
me to evaluate this query
Columnar Great! Hey Aaron, the result
Structures for that measure is $912,184!
Got it. Hey storage
engine, can you grab
Compression & Formula some data for me? Formula
Encoding Engine Engine

VertiPaq
Relationships

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DATA & STORAGE TYPES

Internal DAX
Engines
DAX uses 6 data types to store values:
DAX Data Type Power BI Data Type Storage Type Example

Query Evaluation Integer Whole Number 64-bit Value Max: 9,223,372,036,854,775,807


Double-precision floating-
Decimal Decimal Number
point value
64-bit precision
Data & Storage
Fixed Decimal Fixed Decimal Number
Types Currency Number (Stored as Integer)
317.9899

DateTime DateTime, Date, Time Floating-point number 1/1/2020 12:00p = 43830.50


Columnar
Structures Boolean True/False True/False True/False

String Unicode String 16-bit characters “Maven Analytics” = “MAVEN ANALYTICS”


Compression &
Encoding

HEY THIS IS IMPORTANT!


VertiPaq Data Types represent how values are stored by the DAX storage engine
Relationships
Formatting represents how values appear to end users (%, date, $, etc.)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


VERTIPAQ COLUMNAR DATA STRUCTURE

Internal DAX
Engines VertiPaq uses a columnar data structure, which stores data as individual columns
(rather than rows or full tables) to quickly and efficiently evaluate DAX queries
Query Evaluation

Data & Storage


Types

Columnar
Structures 1 2 3 4

Compression &
Encoding

VertiPaq
Relationships

*Copyright 2020, Excel Maven & Maven Analytics, LLC


VERTIPAQ COMPRESSION & ENCODING

Internal DAX
Engines The goal of compression and encoding is to reduce the amount of memory needed
to evaluate a DAX query.
Query Evaluation Based on a sample of data, one (or more) of the following methods will be used:

Data & Storage 1. Value Encoding


Types
• Mathematical process used to reduce the number of
bits needed to store integer values
Columnar
Structures 2. Hash Encoding (aka Dictionary Encoding) HEY THIS IS IMPORTANT!
The actual storage algorithms
• Identifies the distinct string values and creates a new
are proprietary, so not all
Compression & table with indexes details are available 
Encoding
3. Run Length Encoding (RLE)
VertiPaq • Reduces the size of a dataset by identifying repeated
Relationships values found in adjacent rows

*Copyright 2020, Excel Maven & Maven Analytics, LLC


VALUE ENCODING

Internal DAX
Engines Value Encoding uses a mathematical process to determine relationships between
the values in a column, and convert them into smaller values for storage
Query Evaluation
• Value encoding only works for integer values (including currency), and cannot be applied to
strings or floating-point values

Data & Storage City ID City ID


Types
This is what 10014 This is how the 14
you see data is stored
10106 106
Columnar
Structures 10215 215
10007 7

Compression & 10002 2


Encoding 10021 21
10036 36
VertiPaq
Relationships
• Max = 10215 • Max = 215
• 14 bits needed • 9 bits needed (36% reduction!)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


HASH ENCODING

Internal DAX
Engines
Hash Encoding builds a “dictionary” of distinct items in a column, assigns a unique
integer value (index) to each item, and stores the data using the index values rather
than the full text strings
Query Evaluation
• With hash encoding, storage requirements are defined by the number of unique items in the
column (cardinality), NOT by the length of the string values themselves
Data & Storage
Types
Product Index Row Product Index
This is what Coffee Beans This is how the 0 1 Coffee Beans 0
Columnar you see data is stored
Structures
Tea 1 2 Tea 1
Bakery 2 3 Bakery 2
Bakery 2 4 Encoding dictionary
Compression &
Encoding Coffee Beans 0 5
Coffee Beans 0 6
VertiPaq Bakery 2 7
Relationships
Tea 1 8

*Copyright 2020, Excel Maven & Maven Analytics, LLC


RUN LENGTH ENCODING

Internal DAX
Engines Run Length Encoding (RLE) reduces the size of a column by replacing duplicate
rows with a table containing each distinct value and the count of instances
Query Evaluation • NOTE: RLE only works when the same value is repeated in consecutive rows, but the VertiPaq
engine automatically sorts data on import and refresh to find the optimal compression

Data & Storage


Types This is what City Zip This is how the City Zip Count
you see 59716 data is stored
59716 3
Columnar 59716 3 instances 02215 2
Structures
59716 05672 4
02215 One row per distinct value
Compression &
2 instances
Encoding 02215
05672

VertiPaq 05672
4 instances
Relationships
05672
05672

*Copyright 2020, Excel Maven & Maven Analytics, LLC


RLE + HASH ENCODING

Internal DAX
Engines
Columns can have both Run Length and either Hash (Dictionary) or Value encoding
• Compression type is determined by cardinality, number of repeat values, row count, and data type

Query Evaluation
City Zip City Index Row
City Index Zip City Index Count
59716 0 1
0 59716 0 2
Data & Storage 59716 0 2
Types 1 05672 1 5
05672 1 3
2 90210 2 4
05672 1 4
Columnar 3 83353 3 1
Structures 05672 1 5
Hash Encoding RLE Encoding
05672 1 6
Compression & 05672 1 7
Encoding
90210 2 8
This is how the “City Zip” data is
90210 2 9 encoded and stored by VertiPaq
VertiPaq
Relationships 90210 2 10
90210 2 11
83353 3 12

*Copyright 2020, Excel Maven & Maven Analytics, LLC


VERTIPAQ RELATIONSHIPS

Internal DAX VertiPaq has a special way of mapping relationships between columns in your data
Engines
model, which allows it to evaluate complex, multi-column queries
• NOTE: This is NOT the same as creating table relationships in your data model; this is essentially
Query Evaluation a blueprint that VertiPaq uses to map pairs of primary & foreign keys across related tables

* 1 Sales - Product - Line


Data & Storage
Types Product ID Qty Product ID Product Index Line Product ID
9 22 9 Coffee Beans 0 1 9 1
Columnar 79 24 57 Tea 1 2 79 3
Structures 83 7 79 Bakery 2 3 83 4
9 31 83 Merchandise 3 4 9 1
Compression & 9 42 88 Coffee 4 5
Encoding 9 1
57 26 Product Lookup Table
57 2
57 29
VertiPaq 57 2
Relationships 9 37 This is how VertiPaq stores a relationship
between Sales & Product tables 9 1
88 21
88 5
Sales Table

*Copyright 2020, Excel Maven & Maven Analytics, LLC


VERTIPAQ RELATIONSHIPS

Internal DAX
Engines
STEP 3: Return a datacache
containing a filtered sales table
STEP 2: Use relationship to find and sends to formula engine
Query Evaluation all rows where product line = 1

Sales - Product - Product ID Qty


Product ID Line 9 22
Data & Storage
9 1 9 31
Types
STEP 1: Search for “Coffee Beans”
79 3 9 42
in Product dictionary to find line
number from product lookup 83 4 9 37
Columnar
Structures 9 1
Product Index Line
9 1
Coffee Beans 0 1
Tea 1 2 57 2
Compression & STEP 4: Formula engine evaluates
Encoding 57 2 the [Quantity Sold] measure
Bakery 2 3
9 1 against datacache
Merchandise 3 4
88 5
VertiPaq
Relationships
Coffee 4 5
[Quantity Sold] = 132

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SUMMARY

DAX uses two engines: the Formula Engine & Storage Engine
• The Formula Engine interprets & evaluates DAX, and the Storage Engine compresses & encodes data

There are two types of storage engines: VertiPaq & DirectQuery


• VertiPaq is used for in-memory storage and DirectQuery is used for direct connections to external
sources; both create datacaches and send them to the formula engine for DAX query evaluation

VertiPaq stores data using a columnar database setup


• Data is stored in individual columns that can be accessed quickly, but queries that call multiple
columns may require more complex logic to produce a datacache

Raw data is compressed & encoded to optimize processing


• Data can be compressed using Value, Hash (Dictionary), or Run Length encoding (RLE), based on
cardinality, repeat values, row count, and data type

*Copyright 2020, Excel Maven & Maven Analytics, LLC


TIPS & BEST PRACTICES

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DAX TIPS & BEST PRACTICES

In this section we’ll cover some common techniques and best practices for working efficiently
with DAX, including shortcuts, comments, measure tables, and variables

TOPICS WE’LL COVER: COMMON USE CASES:

• Saving time with keyboard shortcuts


Shortcuts Formatting
• Adding comments to help others understand exactly
what each line of your DAX code is doing
Evaluation Order Comments
• Using error checking functions for testing & QA
• Adding variables to troubleshoot code, enhance
Measure Tables Error Handling readability and improve DAX performance

Variables

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DAX SHORTCUTS

Shortcuts Action Keyboard Shortcut


Insert Line Below Shift + Enter

Formatting Insert Line Above Control + Shift + Enter


Select Current Line Control + I

Evaluation Order Cut Current Line Control + X


Paste Cut Line Control + V

Comments Duplicate Current Row Shift + Alt +


Go to Line Control + G

Measure Tables Enlarge Code Type Control + Mouse Scroll

Error Handling
PRO TIP:
If you use a square bracket “[“ when you call a measure, you’ll avoid having to filter
Variables through function names that also start with the same letter(s) as your measure name

*For more keyboard shortcuts visit: https://docs.microsoft.com/en-us/power-bi/transform-model/desktop-formula-editor


*Copyright 2020, Excel Maven & Maven Analytics, LLC
FORMATTING BEST PRACTICES

Shortcuts Formatting is KEY for readability. Can you tell what this code does in 10 seconds?

Formatting

Evaluation Order

Comments
10
1
2
3
4
5
6
7
8
9
Ready?
Go!Up!
Time’s
Time’s Up!

Measure Tables

Error Handling

Variables
PRO TIP
Use shift + enter to split out and indent each component of your DAX formulas to make them more human
readable (TIP: try using daxformatter.com to quickly format your code)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


EVALUATION ORDER

Shortcuts Evaluation order is the process by which DAX evaluates the parameters in a function
• Individual functions typically evaluate from left-to-right, starting with the first
Formatting parameter (followed by the second, third, etc.)
• Nested functions evaluate from the inside-out, starting with the innermost function
Evaluation Order and working outward from there

Non-nested: Nested:
Comments
1 2 3
=IF(LogicalTest, ResultIfTrue, [ResultIfFalse]) =SUMX(
Measure Tables FILTER(
FILTER( ‘Table’,
RELATED( ‘Table’[Column]), 1
Error Handling
RELATED( ‘Table’[Column]), 2
‘Table’[Column]) 3
Variables

NOTE: The CALCULATE function evaluates using its own unique set of rules (more on this later!)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


EVALUATION ORDER (EXAMPLE)

Shortcuts Non-Nested

Formatting

Evaluation Order

Nested
Comments

Measure Tables

Error Handling

Variables

*Copyright 2020, Excel Maven & Maven Analytics, LLC


COMMENTING YOUR CODE

Shortcuts Comments can help other users interpret your Comment Type Marker
code, and can be particularly helpful for complex Single Line Comment -- or //

Formatting
queries with multiple lines, nested functions, etc. Multi Line Comment /* … */

Evaluation Order

Comments

Measure Tables

Error Handling

PRO TIP
Variables Avoid putting comments at the end of your DAX query (below the last closing parenthesis), as they can be
missed or omitted by users and formatting tools

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PRO TIP: DEDICATED MEASURE TABLES

Shortcuts Creating a separate table to contain your DAX measures is a great way to stay
organized (you can even group your measures into folders within the table!)
Formatting

Enter Data to create a new table


Evaluation Order

Comments
Add a placeholder value in the first row of the
table (can be anything you want)
Measure Tables
Name the table (i.e. “Measure Table”)

Error Handling
Load the table to your data model

Variables

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ERROR HANDLING

Shortcuts
Error handling functions can be used to help identify missing data, and can be
particularly useful for quality assurance and testing
Formatting Returns a value if the first expression is an error and
IFERROR() the value of the expression itself otherwise
=IFERROR(Value,ValueIfError)

Evaluation Order ISBLANK() Checks to see if a value is blank, returns True or False =ISBLANK(Value)

Comments

Measure Tables

Error Handling

PRO TIP:
Variables
VertiPaq can’t optimize IFERROR and ISBLANK functions, so avoid using them in your
operational code (a better use is for temporary QA & testing)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DAX VARIABLES

Shortcuts Variables (VAR) are DAX expressions which can be reused multiple times within a query, and are
commonly used for two key purposes:
Formatting • Readability: Variables make complex code more human readable
• Performance: Variables are only evaluated once no matter how often they are used within a query
Evaluation Order

What they CAN do: What they CANNOT do:


Comments

• Simplify and streamline DAX code • Start with a number


Measure Tables
• Improve efficiency by eliminating • Include spaces or special characters (except
redundant expressions underscores “_”)
Error Handling • Evaluate in the order they’re written • Share the name of another table in the model
(variables can only reference previously • Be accessed outside the query in which they
declared variables within the query) are declared
Variables
• Store either table or scalar values • Contain only certain keywords reserved for
DAX (SUM, Date, CALCULATE, etc.)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CREATING & USING VARIABLES

Shortcuts
DAX queries which use variables must include two key components: the declaration
expression and the return expression:
Formatting

VAR <name> = <expression> RETURN <result_expression>


Evaluation Order

Comments The declaration expression (VAR) is where you


declare a new variable, assign a name, and
write an expression to define the variable
Measure Tables

Error Handling

Variables
The return expression (RETURN) is where
you evaluate the rest of your DAX query, and
reference any previously declared variables

*Copyright 2020, Excel Maven & Maven Analytics, LLC


VARIABLE EVALUATION ORDER

Shortcuts
Variables are “locked in” as soon as the DAX engine reads them; this means you cannot
modify how a variable is defined later in your query (i.e. through a CALCULATE function)
Formatting

Evaluation Order Total quantity sold to Female customers = 283,427

Comments

Measure Tables

Error Handling

Variables

= 1,305,637 Variable can’t be modified = 283,427


after it’s been defined

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PRO TIP: VARIABLES TO TEST & DEBUG DAX

Shortcuts Variables can be a helpful tool for testing or debugging your DAX code

Formatting

Evaluation Order In this example we’re able to:


• Use variables to define individual
components of a larger, more complex
Comments
measure
• Use the RETURN expression to quickly
Measure Tables
check the output of each variable
• Identify which specific component is the
Error Handling root cause in the case of an error

Variables

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SCALAR FUNCTIONS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SCALAR FUNCTIONS

Scalar functions return a single value, rather than a column or table; common examples
include aggregation, conversion, rounding, and logical functions

TOPICS WE’LL COVER: COMMON USE CASES:

Aggregation Rounding • Aggregating a column of values into a single number


(i.e. average customer age, maximum product price,
Functions Functions
sum of revenue, count of orders, etc.)

Information Conversion • Converting fields into desired formats (i.e. text to


dates, integers to currency, etc.)
Functions Functions
• Evaluating logical tests and returning values for TRUE
Logical and FALSE responses
Functions

*Copyright 2020, Excel Maven & Maven Analytics, LLC


COMMON SCALAR FUNCTIONS

AGGREGATION ROUNDING INFORMATION CONVERSION LOGICAL


Functions Functions Functions Functions Functions

Functions that can be used Functions that can be used Functions that can be used Functions that are used to Functions for returning
to dynamically aggregate to round values to different to analyze the data type or force a specific data type information about values in
values within a column levels of precision output of an expression conversion a conditional expression

Common Examples: Common Examples: Common Examples: Common Examples: Common Examples:
• SUM • FLOOR • ISBLANK • CURRENCY • IF
• AVERAGE • TRUNC • ISERROR • INT • AND
• MAX • ROUNDDOWN • ISLOGICAL • FORMAT • OR
• MIN • MROUND • ISNONTEXT • DATE • NOT
• COUNT • ROUND • ISNUMBER • TIME • TRUE/FALSE
• COUNTA • CEILING • ISTEXT • DATEVALUE • SWITCH
• DISTINCTCOUNT • ISO.CEILING • VALUE • COALESCE
• PRODUCT • ROUNDUP
• ITERATOR (“X”) • INT
FUNCTIONS • FIXED

*Copyright 2020, Excel Maven & Maven Analytics, LLC


AGGREGATION FUNCTIONS

SUM() Evaluates the sum of a column =SUM(ColumnName)


Aggregation
Functions Returns the average (arithmetic mean) of all
AVERAGE() the numbers in a column
=AVERAGE(ColumnName)

Rounding Returns the largest value in a column or =MAX(ColumnName) or


Functions MAX() between two scalar expressions =MAX(Scalar1, [Scalar2])

Returns the smallest value in a column or =MIN(ColumnName) or


Information MIN() between two scalar expressions =MIN(Scalar1, [Scalar2])
Functions
Counts the number of cells in a column that
COUNT() contain numbers
=COUNT(ColumnName)
Conversion
Functions Counts the number of distinct or unique values
DISTINCTCOUNT() in a column =DISTINCTCOUNT(ColumnName)

Logical Counts the number of rows in the specified


Functions COUNTROWS() table, or a table defined by an expression
=COUNTROWS(Table)

PRO TIP:
For large datasets (1M+ rows) using COUNTROWS & VALUES
may put less strain on the DAX engines than DISTINCTCOUNT
*Copyright 2018, Excel Maven & Maven Analytics, LLC
PRO TIP: SUM VS. SUMX

There are several cases where DAX evaluates a basic query using a more complex
method behind the scenes. The simplified query is often called “syntax sugar”

• For example, SUM is read internally as SUMX


How it’s written: How it’s interpreted by DAX:

1 Total Sales = 1 Total Sales =


2 SUM(‘Sales by Store’[quantity_sold]) 2 SUMX(
3 ‘Sales by Store’
4 ‘Sales by Store’[quantity_sold])

HEY THIS IS IMPORTANT!


This is how all aggregation functions are processed internally by DAX (SUM, AVERAGE, MAX, etc.)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ROUNDING FUNCTIONS
Rounds a number down to the nearest
INT() integer
=INT(Number)
Aggregation
Functions Rounds a number to a specific number of
ROUND() digits
=ROUND(Number, NumberOfDigits)

Rounding ROUNDUP() Rounds a number up, away from zero =ROUNDUP(Number, NumberOfDigits)
Functions

ROUNDDOWN() Rounds a number down, toward zero =ROUNDDOWN(Number NumberOfDigits)


Information
Functions
MROUND() Rounds a number to the desired multiple =MROUND(Number, Multiple)

Conversion Truncates a number to an integer by


Functions TRUNC() removing the decimal part of the number
=TRUNC(Number, [NumberOfDigits])

Rounds number down to specified number


Logical
FIXED() of decimals and returns result as text
=FIXED(Number, [Decimals], [No.Commas])
Functions
Rounds a number up, to the nearest
CEILING() integer or nearest unit of significance
=CEILING(Number, Significance)

Rounds a number down, toward zero, to


FLOOR() the nearest multiple of significance
=FLOOR(Number, Significance)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ROUNDING FUNCTION EXAMPLES

Aggregation
Decimal Value: 3.12438 Time value: 9:34:14 AM
Functions

INT ( 3.12438) = 3 MROUND ( 9:34:14, ”0:15”) = 9:30:00 AM


Rounding
Functions
Useful as a component in DAX Rounds the minute component up
statements, like calendar tables or down based on the multiple

Information
Functions ROUND ( 3.12438, 2) = 3.12
ROUNDUP ( 3.12438, 2) = 3.13 FLOOR ( 9:34:14, “0:15”) = 9:30:00 AM
Conversion ROUNDDOWN ( 3.12438, 2) = 3.12 Rounds the minute component
Functions down to the nearest multiple
Useful to specify the precision
of a number, like customer age

Logical
Functions FIXED ( 3.12438, 2) = 3.12 CEILING ( 9:34:14, ”0:15”) = 9:45:00 AM

Useful when you want to Rounds the minute component up to


convert a number to text the nearest multiple

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: ROUNDING FUNCTIONS

MAVEN
ROASTERS Jean LeBean (CEO)
3:15 PM KEY OBJECTIVES:
CHANNELS (12)
1. Add a new column in your
Hey there! Hoping you could do me a quick
favor - I’m having trouble figuring out the Customers table to calculate
actual ages of our customers, and I think it age
may be a rounding issue...
2. Use a rounding function to
make sure that ages are
Would you mind digging into our customer defined properly and
MESSAGES (23) records to see what you can figure out? formatted as whole
numbers

Jean LeBean 1

*Copyright 2020, Excel Maven & Maven Analytics, LLC


INFORMATION FUNCTIONS

Checks whether a value is blank, and returns =ISBLANK(Value)


Aggregation ISBLANK() TRUE or FALSE
Functions

Checks whether a values is an error, and


ISERROR() =ISERROR(Value)
Rounding returns TRUE or FALSE
Functions

Checks whether a value is a logical value =ISLOGICAL(Value)


Information
ISLOGICAL() (TRUE or FALSE), and returns TRUE or FALSE
Functions

Checks whether a value is a number, and


ISNUMBER() =ISNUMBER(Value)
Conversion
returns TRUE or FALSE
Functions

Checks whether a value is not text (blank


ISNONTEXT() =ISNONTEXT(Value)
cells are not text), and returns TRUE or FALSE
Logical
Functions
Checks whether a value is text, and returns
ISTEXT() =ISTEXT(Value)
TRUE or FALSE

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CONVERSION FUNCTIONS

Evaluates the argument and returns the


Aggregation CURRENCY() result as a currency data type
=CURRENCY(Value)
Functions

Converts a value to text in the specified


Rounding
FORMAT() number format =FORMAT(Value, Format)
Functions

Information
DATE() Returns the specified date in datetime format =DATE(Year, Month, Day)
Functions

Converts hours, minutes, and seconds given


TIME() as numbers to a time in datetime format =TIME(Hours, Minute, Second)
Conversion
Functions

Converts a date in the form of text to a date


DATEVALUE() in datetime format =DATEVALUE(DateText)
Logical
Functions
Converts a text string that represents a
VALUE() number to a number
=VALUE(Text)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: CONVERSION FUNCTIONS

MAVEN
ROASTERS Mark Brewer (CFO)
12:39 PM KEY OBJECTIVES:
CHANNELS (12)
1. Create a new calculated
Jean and I were having a chat and could use
your help with a couple items: column to convert
Transaction_Date to yyyy-
mm-dd format
1. Can you add a column to the calendar table
to format the dates like “2020-08-30”? 2. Use a conversion function
to modify the [Cost]
MESSAGES (8) measure and ensure that
2. Could you please make sure that [Cost] values are always displayed
values are always formatted as currency in as currency (without simply
our reports? changing the format)

Mark Brewer 1

*Copyright 2020, Excel Maven & Maven Analytics, LLC


BASIC LOGICAL FUNCTIONS

Aggregation
Functions
Checks if a given condition is met, and
IF() returns one value if the condition is TRUE, =IF(LogicalTest, ResultIfTrue, [ResultIfFalse])
and another if the condition is FALSE
Rounding
Functions

Checks whether both arguments are TRUE,


Information
AND() and returns TRUE if both arguments are =AND(Logical1, Logical2) Note: Use the
Functions
TRUE, otherwise returns FALSE && and ||
operators if you
Conversion want to include
Functions Checks whether one of the arguments is more than two
OR() TRUE to return TRUE, and returns FALSE if =OR(Logical1, Logical2) conditions!
both arguments are FALSE
Logical
Functions

*Copyright 2018, Excel Maven & Maven Analytics, LLC


SWITCH

Aggregation SWITCH() Evaluates an expression against a list of values and returns one of multiple possible expressions
Functions

=SWITCH(Expression, Value1, Result1, …, [Else])


Rounding
Functions
Any DAX expression that returns a List of values produced by the Value returned if the
single scalar value, evaluated expression, each paired with a result expression doesn’t match
Information to return for rows/cases that match any value argument
multiples times
Functions
Examples: Examples:

Conversion • Calendar[Month_ID] =SWITCH( Calendar[Month_ID],


Functions • Product Lookup[product_group] 1, “January”,
2. “February”, etc.

Logical
Functions
PRO TIP
SWITCH (TRUE) is a common Pattern to replace nested IF statements

*Copyright 2020, Excel Maven & Maven Analytics, LLC


COALESCE

Aggregation Returns the first argument that does not evaluate to BLANK. If all arguments
Functions COALESCE() evaluate to BLANK, BLANK is returned.

Rounding
Functions =COALESCE(Expression1, Expression2, […])

Information
Functions
Any value or expression that returns a scalar value
Examples:
Conversion
Functions • COALESCE(
SUM(‘Sales by Store’[quantity_sold]), PRO TIP:
0)
COALESCE replaces the IF + ISBLANK
Logical • COALESCE( pattern, makes your code more readable,
Functions [Yesterday Customer Revenue], and can be optimized by the engines
“-”)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: LOGICAL FUNCTIONS

MAVEN
ROASTERS Darren A. Xu (Manager)
1:32 PM KEY OBJECTIVES:
CHANNELS (12)
1. Create a calculated column
Hey there! I’m looking at my customer sales
and want to see it by quarter/year. I really using variables and
need a column in the calendar table that SWITCH(TRUE()) to create a
shows the quarter and the year together… single column containing both
quarter & year (i.e. Q1-2020,
Q2-2020, etc.)
Mind quickly setting that up ?
MESSAGES (13)

Darren Xu 1

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ADVANCED CALCULATE

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ADVANCED CALCULATE

CALCULATE is a powerful DAX function which is commonly used to modify filter context; in this
section, we’ll explore advanced topics like expanded tables, context transition, evaluation
order and modifiers

TOPICS WE’LL COVER: COMMON USE CASES:


• Simplifying measures by adding new filter context that
Expanded Tables Context Transition would normally use multiple nested functions
• Modifying filter context to override preexisting filters
using modifiers like ALL, KEEPFILTERS or REMOVEFILTERS
Evaluation Order Modifiers
• Creating calculated inputs for measures like % of total, %
of category, etc.

*Copyright 2020, Excel Maven & Maven Analytics, LLC


EXPANDED TABLES

An expanded table consists of the base table (which is visible to the user), along with
columns from any related table connected via a 1-to-1 or many-to-1 relationship
Expanded Tables
Product Lookup Table Store Lookup Table
Product ID Name Category Store ID Address Manager
1 Brazilian - Organic Coffee beans 3 32-20 Broadway 6
Context
Transition 2 Old Time Diner Coffee beans 4 604 Union Street 11
3 Espresso Roast Coffee beans 5 100 Church Street 16
4 Primo Roast Coffee beans 6 122 E Broadway 21 Related fields from the Product
Evaluation Order 5 Columbian Medium Coffee beans 7 224 E 57th Street 26 and Store tables are accessible
1 1
to DAX as part of the
“expanded” Transactions table

* * As you begin to write more


Modifiers Trans ID Product ID Store ID Quantity Name Category Address Manager complex DAX & CALCULATE
1 4 4 32 Primo Roast Coffee beans 604 Union Street 11 statements, understanding
expanded tables is critical!
2 2 3 17 Old Time Diner Coffee beans 32-20 Broadway 6
3 3 7 113 Espresso Roast Coffee beans 224 E 57th Street 26
4 3 3 14 Espresso Roast Coffee beans 32-20 Broadway 6
5 1 4 55 Brazilian - Organic Coffee beans 604 Union Street 11

Transactions Table

*Copyright 2020, Excel Maven & Maven Analytics, LLC


EXPANDED TABLES

Expanded Tables Expanded tables in Models always go from the


MANY side to the ONE side of the relationship

Context • Expanded tables contain all the columns of the


Transition
original table plus all the columns on the one-side

• Understanding expanded tables is useful because


Evaluation Order it provides a clear understanding of how filter
context propagates in DAX measures

Modifiers • Once a filter is applied to a column, all expanded


tables containing that column are also filtered

*Maven Roasters Data Model

*Copyright 2020, Excel Maven & Maven Analytics, LLC


EXPANDED TABLES (EXAMPLE)

3
2

Expanded Tables

1
In this case, the expanded
Context version of the AW_Sales
Transition
table contains all fields from
the product, subcategory,
and category lookup tables
Evaluation Order

Modifiers

*Adventure Works data model

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CONTEXT TRANSITION (CALCULATED COLUMNS)

Context Transition is the process of turning row context into filter context
• By default, calculated columns understand row context but not filter context
Expanded Tables
• To create filter context at the row-level, you can use CALCULATE

Context
Transition
If we add a column to calculate the sum of
quantity_sold, we get repeating totals since
there is no filter context, and we can’t
Evaluation Order determine a unique value per row

To solve this, we can generate filter context at


Modifiers the row-level, using CALCULATE with SUMX

1 Sum of Quantity =
2 CALCULATE(
3 SUM(
4 ‘Table’[quantity_sold])
5 )

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CONTEXT TRANSITION (MEASURES)

Consider the following formula, defined as both a measure and a calculated column:

1 Sum of Quantity Sold =


Expanded Tables
2 SUM(‘Table’[quantity_sold])

Context
Transition

Evaluation Order

As a measure, DAX automatically evaluates a CALCULATE &


Modifiers SUMX measure (shown below), to create the filter context
needed to produce the correct values
As a calculated column, we see the same total
repeated on each row (no filter context) 1 Sum of Quantity Sold =
2 CALCULATE(
3 SUMX(
4 ‘Table’,
5 ‘Table’[quantity_sold])
6 )

*Copyright 2020, Excel Maven & Maven Analytics, LLC


EVALUATION ORDER (EXAMPLE)

[Customer Sales] is evaluated LAST,


using the modified filter context

Expanded Tables store_id = 3 is evaluated FIRST


3
1
2
Context Product_group = “Whole
Transition Bean/Teas” is evaluated SECOND

[Customer Sales] is evaluated LAST, Both measures evaluate the same, since the
Evaluation Order using the modified filter context ALL modifier evaluates first and is overwritten
by the store_id and product_group filters

Modifiers store_id & product_group 4


filters are evaluated next, and
2
overwrite the ALL modifier 3
1
HEY THIS IS IMPORTANT!
DAX engines combine multiple filter
ALL Modifier is evaluated FIRST
arguments into a single filter context

*Copyright 2020, Excel Maven & Maven Analytics, LLC


COMMON CALCULATE MODIFIERS

Modifiers are used to alter the way CALCULATE creates filter context, and are added
as filter arguments within a CALCULATE function
Expanded Tables
• Modifiers are typically used to change filter context, access inactive table relationships,
or change the way filters propagate (i.e. one-way to bidirectional)
Context
Transition

Modify Use Change Filter


Evaluation Order Filters Relationships Propagation
Common Examples: Common Examples: Common Examples:
Modifiers • ALL • USERELATIONSHIP • CROSSFILTER
• ALLSELECTED
• ALLNOBLANKROW
• ALLEXCEPT
• KEEPFILTERS
• REMOVEFILTERS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CALCULATE REVIEW (BOOLEAN & TABLE FILTERS)

=CALCULATE(Expression, [Filter1], [Filter2],…)


Expanded Tables
CALCULATE filter expressions accept both boolean & table functions (individually or
at the same time!), but all filter arguments are automatically converted into a table
Context
Transition

Evaluation Order DAX interprets this as a table!


• Any time you use write a function that contains a
logical statement (IN, >,<, =, etc.) you’re creating a
Modifiers table (internally processed with FILTER & ALL)

And this too, obviously

*Copyright 2020, Excel Maven & Maven Analytics, LLC


REMOVEFILTERS

REMOVEFILTERS() Clears filters from the specified tables or columns

Expanded Tables
=REMOVEFILTERS ( TableNameorColumnName, [ColumnName], […] )

Context
Transition Table or column that you want to (Optional) Repeatable column names that allow you
clear the filters from to specifically remove filters from individual columns
within the base table
Examples:
Evaluation Order
• CALCULATE( Examples:
[Customer Sales], • CALCULATE(
REMOVEFILTERS( [Customer Sales],
‘Store Lookup’)) REMOVEFILTERS(
Modifiers
‘Store Lookup’[store_id]))

PRO TIP:
REMOVEFILTERS is an alias for ALL, but can only be used as a CALCULATE modifier (not as a table function)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


KEEPFILTERS

Expanded Tables KEEPFILTERS() Does not remove an existing column or table filter for an individual CALCULATE expression

Context =KEEPFILTERS(Expression)
Transition

Evaluation Order CALCULATE or CALCULATETABLE


✓ Allows you to control which specific filters are
function expression or filter
applied to a calculation
Examples:
Modifiers ✓ KEEPFILTERS does not actually modify existing filters,
• CALCULATE(
but adds new filter context (think inner join)
[Measure],
KEEPFILTERS(‘Table’[ID]=“Value”))

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: ADVANCED CALCULATE

MAVEN
ROASTERS Karen Cupps (Manager)
8:32 AM KEY OBJECTIVES:
CHANNELS (12)
1. Create a matrix containing
Okay, so I’m trying to create a table to show
my store sales (Store 3) broken down by product group & store id on
product group and compared against the rows
other two stores (5 & 8). Could you help?
2. Use KEEPFILTERS to create
measures that show sales for
I’d also like to see store sales within each each of the 3 stores
MESSAGES (5) product group as a percentage of the total, to
3. Use REMOVEFILTERS with
see which stores are the biggest sales drivers
within the group. Thanks! variables to create a single
measure for % of Store Sales
Karen Cupps 1

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PRO TIP: COMMON CALCULATE TOTAL PATTERNS
Cumulative Total Percent of Total

Overall Total

*Copyright 2020, Excel Maven & Maven Analytics, LLC


TABLE & FILTER FUNCTIONS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


TABLE & FILTER FUNCTIONS

Table and Filter functions return columns or tables rather than scalar values, and can be used
to either generate new data or serve as table inputs within DAX measures

TOPICS WE’LL COVER: COMMON USE CASES:


• Returning columns or tables which are subsets of
Calculated Tables Filtering Tables existing data sources
• Adding new columns of data to an existing table

Manipulating Tables Generating Data • Filtering out totals from measure results
• Reducing the number of rows returned from a table
• Generating new data from scratch

*Copyright 2020, Excel Maven & Maven Analytics, LLC


COMMON TABLE & FILTER FUNCTIONS

Filter Data Add Data Create Data


Functions used to return filtered tables or Functions used to specify or add columns Functions used to generate new rows,
filter results of measures based on existing data in the model columns & tables from scratch

Common Examples: Common Examples: Common Examples:


• ALL • SELECTCOLUMNS • ROW
• FILTER • ADDCOLUMNS • DATATABLE
• DISTINCT • SUMMARIZE • GENERATESERIES
• VALUES • { } Table Constructor
• ALLEXCEPT
• ALLSELECTED

*Copyright 2020, Excel Maven & Maven Analytics, LLC


REVIEW: CALCULATED TABLES

DAX functions with table arguments can typically accept either physical tables (i.e.
‘Sales by Store’) or calculated, virtual tables (with functions like FILTER, VALUES, etc.)
Calculated Tables

Filtering Tables =SUMX(Table, Expression)

Manipulating
Tables
1 Total Sales =
2 SUMX(
1 Total Sales = FILTER(
2 SUMX( 3 ‘Sales by Store’,
Generating Data 4 ‘Sales by Store’[quantity_sold]) > 3
3 ‘Sales by Store’,
4 ‘Sales by Store’[quantity_sold]) 5 ),
6 ‘Sales by Store’[quantity_sold]) )

*Copyright 2020, Excel Maven & Maven Analytics, LLC


REVIEW: FILTER & ALL

FILTER() Returns a filtered table, based on one or more filter expressions


Calculated Tables

=FILTER(Expression, [Filter1], [Filter2],…)


Filtering Tables • FILTER is both a table function and an iterator
• Often used to reduce the number of rows to scan

Manipulating
Tables
ALL() Returns all the rows in a table, or all the values in a column, ignoring any filters

Generating Data
=ALL(Table or ColumnName, [ColumnName1],…)
• ALL is both a table filter and a CALCULATE modifier
• Removes initial filter context
• Does not accept table expressions (only physical table references)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


REVIEW: FILTER (EXAMPLE)
STEP 1 Measure is written

Calculated Tables =
STEP 3 DAX evaluates [Customer
Filtering Tables Sales] against the filtered
table (quantity sold > 3)

Manipulating
Tables

Generating Data STEP 2 FILTER creates a virtual, calculated table

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DISTINCT

Returns a single column table of unique values when a column name is given.
DISTINCT() If a table is supplied, DISTINCT returns all unique combinations of values.

Calculated Tables

=DISTINCT(ColumnName or TableExpression)
Filtering Tables

The column you want to extract The table you want to extract unique
Manipulating
Tables unique values from combinations of values from
Examples: Examples:
• ‘Sales by Store’[CustomerID] • ‘Sales by Store’
Generating Data • ‘Sales by Store’[UnitPrice] • ‘Food Inventory’

PRO TIP:
Trying to build a relational model from a single, blended table? Use DISTINCT to create
new lookup/dimension tables by extracting unique values from fields in your data table!

*Copyright 2020, Excel Maven & Maven Analytics, LLC


VALUES

Calculated Tables Returns a single column table of unique values when a column name is given. If a table
VALUES() name is supplied, VALUES returns the entire table (including duplicates) plus a blank row

Filtering Tables
=VALUES(TableName or ColumnName)
Manipulating
Tables

The table you want to pull all columns The column you want to extract
and all rows from (not unique) unique values from
Generating Data
Examples: Examples:
• ‘Sales by Store’ • ‘Sales by Store’[CustomerID]
• ‘Food Inventory’ • ‘Sales by Store’[UnitPrice]

*Copyright 2020, Excel Maven & Maven Analytics, LLC


VALUES VS. DISTINCT: THE BLANK ROW
Product Lookup
In this example we have a Product Lookup table
Product ID Product containing three product IDs (12, 13, 14)
12 Coffee
Calculated Tables
13 Tea
But our fact table (‘Sales by Store’) contains additional
14 Pastry product IDs NOT included in the Product Lookup (15, 16)

Filtering Tables blank blank


1
• Instead of throwing an error when a value is missing from
a lookup table, the DAX engine adds a blank row (which
Manipulating
Sales by Store will appear in visuals when missing values are present)
Tables *
Product ID Quantity • Different table functions handle the presence of this
12 2 blank row differently; for example, VALUES will always
Generating Data
show the blank row but DISTINCT will not
13 6
14 1
14 3 PRO TIP:
15 5 If you think you might have missing values in your
lookup tables (or aren’t sure), use VALUES to check
16 6

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: VALUES & DISTINCT BLANK ROW

MAVEN
KEY OBJECTIVES:
ROASTERS Jean LeBean (CEO)
3:08 PM 1. Check if the Ginger Scone
CHANNELS (12) product exists in the data
Hey – I’m looking at our product sales and I’m model
not seeing the new Ginger Scone that we
added for Fall. I know we’ve been selling them, 2. Use counting functions plus
but can’t find them in the report... DISTINCT & VALUES to create
a view that shows the blank
row
Would you mind digging into our product
MESSAGES (23) records to see what you can figure out? 3. Add a new visual to show the
Adam Songs 1 missing product id when the
blank product is selected
Jean LeBean 1
4. Update the data model to
include the new product
Hint: Product Lookup (Updated)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SELECTEDVALUE

Returns a value when there’s only one value in a specified


SELECTEDVALUE() column, otherwise returns an (optional) alternate result

Calculated Tables
=SELECTEDVALUE(ColumnName, [AlternateResult])

Filtering Tables The column you want to return a single value from (Optional) The value returned when there is either no
Examples: value or more than one value in the specified column
(if omitted, blank is returned)
• ‘Customer Lookup’[customer_first_name]
Manipulating • ‘Product Lookup’[product] Examples:
Tables • “-”
• “NA”

Generating Data

Because the expression doesn’t evaluate to


a single (scalar) result, nothing is returned.
In this case, only when a single retail price
can be determined is a result returned.

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PRO TIP: SELECTEDVALUE “SYNTAX SUGAR”

SELECTEDVALUE is another example of “syntax sugar” in DAX


Calculated Tables
• The DAX engine internally processes SELECTEDVALUE as a combination of IF,
HASONEVALUE, and VALUES:
Filtering Tables

How it’s written: How it’s interpreted:


Manipulating 1 SELECTEDVALUE =
Tables 1 SELECTEDVALUE =
2 IF(
2 SELECTEDVALUE(
3 HASONEVALUE(
3 Customer[customer_first-name],
4 Customer[customer_first-name]),
4 “-”
5 VALUES(
Generating Data 5 )
6 Customer[customer_first-name]),
7 “-”
8 )

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ALLEXCEPT

Removes all report context filters in the table except the filters
ALLEXCEPT() applied to the specified columns in the query

Calculated Tables
=ALLEXCEPT(TableName, [ColumnName],[ColumnName], […])

Filtering Tables Name of an existing table (the use of table Additional column references within the same
expressions is not allowed here) referenced table or a table that is on the one-side of the
relationship (adding additional columns is optional)
Examples:
Manipulating
‘Sales by Store’ Examples:
Tables
• ‘Sales by Store’[product_group]
‘Returns’ • ‘Sales by Store’[product_category]

‘Calendar’[Transaction_Date]

FILTER(
Generating Data ‘Sales by Store’,
‘Sales by Store’[store_id] IN {2,5,7,9,10})

PRO TIP:
ALLEXCEPT is typically used as a CALCULATE modifier and not a stand-alone table function

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ALLEXCEPT (EXAMPLE)

Here we’re using ALLEXCEPT as a


CALCULATE modifier to remove all
Calculated Tables
initial filter context from the ‘Sales by
Store’ table, except for filters on the
following columns:
Filtering Tables • ‘Calendar’[Transaction_Date]
• ‘Store Lookup’[sales_outlet_id]

Manipulating
Tables

Generating Data
The measure above returns
Customer Sales by store ID
and Date, but ignores filter
context created by other
fields (i.e. Product Category)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: ALLEXCEPT

KEY OBJECTIVES:
MAVEN
ROASTERS Adam Songs (Head Barista)
10:13 AM
1. Create a matrix with the store
CHANNELS (12)
id, product group and customer
Hey there! Can you please look at the sales name on rows, filtered based
for 2018 to our top 10 customers (excluding on Adam’s request
non-members) at store 8?
2. Use ALLEXCEPT to create a
I’m curious how much of our sales were measure that only accepts
driven by those customers, in total and as a % filters for the date, store ID
of all our sales during that period product group and customer
MESSAGES (1)
Adam Songs 1 3. Create a % of store-level sales
Would love to share this insight with Mark measure to calculate the
and Jean in our next roundtable! percent of sales by the top 10
customers at the store level
Hint: Try REMOVEFILTERS + KEEPFILTERS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ALLSELECTED

Returns all rows in a table or values in a column, ignoring filters


ALLSELECTED() specified in the query but keeping any other existing filter context

Calculated Tables
=ALLSELECTED(TableNameOrColumnName, [ColumnName],[ColumnName], […])

Filtering Tables Name of a table or column that you want to Additional column references within the
remove filters from (NOTE: This input is required same referenced table (adding additional
unless being used as a CALCULATE modifier) columns is optional)
Manipulating
Examples: Examples:
Tables
• ALLSELECTED • ‘Sales by Store’[product_group]
• ‘Sales by Store’ • ‘Sales by Store’[product_category]
• ‘Sales by Store’[product_group]
Generating Data

HEY THIS IS IMPORTANT!


ALLSELECTED respects existing filter context except row and column filters within
a visual. This function can be used to obtain visual totals or subtotals in queries.

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ALLSELECTED (EXAMPLE)

This measure calculates sales


Calculated Tables (quantity * price), in a modified filter
context based on external filters

Filtering Tables

Manipulating
Tables

Generating Data

When we select specific product groups in a report slicer (Beverages,


Food & Merchandise) ALLSELECTED respects the filter context from the
selection (slicer) but not from the rows and columns of the visual.

This function is commonly used to obtain visual totals or subtotals in


queries

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: ALLSELECTED

MAVEN KEY OBJECTIVES:


ROASTERS Lisa Latte (Manager)
10:47 AM 1. Using the Food Inventory
CHANNELS (12) table, create two measures
Good morning! I’m hoping you could help me with a for Total Baked (quantity
quick food inventory analysis.
start of day) and Total Sold
(quantity sold)
Could you send me a visual to show the quantity
baked, quantity sold and % of total? 2. Create measures for % of
Total Baked and % of Total
I’d love to be able to update this view for any Sold, totaling 100% based on
MESSAGES (5) combination of product types, to better understand
which products are baked and sold most often. any selected product types

Jamie Toast 3. Build a visual to compare the


Jean LeBean % of All Baked to % of Total
Lisa Latte 1 Baked. Which food item is
baked the most?

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SELECTCOLUMNS

Returns a table with selected columns from the table plus any new columns
SELECTCOLUMNS() specified by the DAX expressions
Calculated Tables
=SELECTCOLUMNS(Table, Name, Expression, […] )

Filtering Tables
Any DAX expression that returns a Name of the new column to be Any expression that returns a scalar
table added, must be wrapped with value (i.e. column reference, integer or
double quotes. Repeatable string). Repeatable
Manipulating Examples:
Tables Examples: Examples:
• ‘Sales by Store’
• ‘Food Inventory’ • “Employee Name & ID” • ‘Employees’[ID]

• FILTER( • “Employee Full Name” • [First Name] & “ “ & [Last Name]
Generating Data ‘Employees’,
‘Employees’[ID] IN {2,5,7,9,10})

PRO TIP:
SELECTCOLUMNS is an iterator function that’s useful when you
need to reduce the number of columns in a table for calculation

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ADDCOLUMNS

Returns a table with selected columns from the table plus any new columns
ADDCOLUMNS() specified by the DAX expressions
Calculated Tables
=ADDCOLUMNS(Table, Name, Expression, […] )

Filtering Tables
Any DAX expression that returns a Name of the new column to be Any expression that returns a scalar
table added, must be wrapped with value (i.e. column reference, integer or
double quotes. Repeatable string). Repeatable
Manipulating Examples:
Tables Examples: Examples:
• ‘Sales by Store’
• ‘Food Inventory’ • “Employee Name & ID” • ‘Employees’[ID]

• FILTER( • “Employee Full Name” • [First Name] & “ “ & [Last Name]
Generating Data ‘Employees’,
‘Employees’[ID] IN {2,5,7,9,10})

HEY THIS IS IMPORTANT!


ADDCOLUMNS & SELECTCOLUMNS are nearly identical and behave similarly with an exception, SELECTCOLUMNS
starts with a blank table whereas ADDCOLUMNS starts with the entire original table and tacks on columns

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SUMMARIZE

SUMMARIZE() Creates a summary of the input table grouped by the specified columns

Calculated Tables
=SUMMARIZE(Table, GroupBy_ColumnName, [Name], [Expression])

Depreciated and
Filtering Tables not recommended
Any DAX expression that returns a table of Name of an existing column to be used
data to create summary groups based on
Manipulating the values found in the column.
Tables Examples: Cannot be an expression
• ‘Sales by Store’
Examples:
• FILTER(
Generating Data ‘Sales by Store’, • ‘Sales by Store’[store_id]
‘Sales by Store’[product_id] = 16)
• ‘Customer Lookup’[customer_id]

HEY THIS IS IMPORTANT!


SUMMARIZE isn’t an aggregation function. Instead it returns all
unique combinations of values based on the columns selected

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: SUMMARIZE

MAVEN
ROASTERS Darren A. Xu (Manager) KEY OBJECTIVES:
9:37 AM
CHANNELS (12) 1. Use SUMMARIZE to create
Hey! I’ve noticed that pastries haven’t been a table that only shows the
selling well at store 5, and I’m wondering how
the unsold inventory impacts revenue. days with unsold pastries
2. Include columns to
Looking to share these results with Mark on calculate amount sold,
Tuesday. Would you be able to pull the amount unsold, and total
numbers and join that call?
MESSAGES (9) lost revenue
Darren Xu 1 3. Build a matrix to visualize
the results for Darren and
Lisa Latte
Mark

*Copyright 2020, Excel Maven & Maven Analytics, LLC


GENERATING ROWS & TABLES OF DATA

In this section, we’ll review four methods of manufacturing data from scratch:
Calculated Tables
Returns a single row table with new columns
ROW() =ROW(Name, Expression, […] ] )
specified by the DAX expression(s)
Filtering Tables
DATATABLE() Returns a table containing new, static data =DATATABLE(Name, Type, […] ], Data )

Manipulating Returns a single column table containing


Tables GENERATESERIES() sequential values, based on a given increment
=GENERATESERIES(StartValue, EndValue,
[IncrementValue] )

Generating Data
Uses curly brackets to return a table = { scalarExpr1, scalarExpr2, … }
Table Constructor { } containing one or more columns and records OR
= { ( scalarExpr1, scalarExpr2, … ),
( scalarExpr1, scalarExpr2, … ),
}

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ROW

Returns a table with a single row containing values that


ROW() result from the expressions given to each column
Calculated Tables
=ROW(Name, Expression, [ Name, Expression, […] ] )

Filtering Tables
Name of the new column, enclosed in double Any DAX expression that returns a single scalar value
quotes. Repeatable.
Manipulating
Examples:
Tables Examples:
• [Customer Sales]
• “Profit” • SUM(
• “Cost” ‘Food Inventory’[quantity_start_of_day]
Generating Data

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DATATABLE

DATATABLE() Returns a table with a fixed set of values


Calculated Tables
=DATATABLE(Name, Type, [Name, Type], […] ], Data )

Filtering Tables
Column name (in quotes) Column data type Data points to add to table, called and
separated with curly brackets ({ })
Examples: Examples:
Manipulating
• “Test Number” • STRING Examples:
Tables
• “Value” • DOUBLE • {“First”, 1},
• INT • {“Second”, 2},
• CURRENCY • {“Third”, 3}
Generating Data

HEY THIS IS IMPORTANT!


DATATABLE can only accept fixed data inputs (no table or column references, expressions or calculations)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


GENERATESERIES

GENERATESERIES() Returns a one column table populated with sequential values


Calculated Tables

=GENERATESERIES(StartValue, EndValue, [IncrementValue] )

Filtering Tables

Start & End value for the series Value used to increment between Start &
Manipulating (can be positive or negative) End value (must be non-zero and
Tables positive)
Examples:
• 0, 1, 10.25, 50.234, etc. Examples:
• -1, -10.75, -45.5, etc. • .25, 1, 1.7745, 5, etc.
Generating Data

PRO TIP:
GENERATESERIES is a great way to build a custom range of data to be used as a parameter input

*Copyright 2020, Excel Maven & Maven Analytics, LLC


TABLE CONSTRUCTOR

The table constructor { } can be used to build tables containing one or more columns
or rows, based on fixed values or DAX expressions which return scalar values
Calculated Tables • NOTE: Column headers can’t be defined using table constructor syntax

Filtering Tables 1 Single column with multiple rows: 3 Multi-row, multi-column table:

Manipulating
Tables

Generating Data 2 Single row with multiple columns:

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: GENERATING DATA

MAVEN
ROASTERS Jean LeBean (CEO) KEY OBJECTIVES:
3:15 PM
CHANNELS (12) 1. Use DATATABLE to
Mark and I are working on some forecasts, recreate the table that
and I need a new table showing target sales Jean needs for her
for April 2019, by product group.
forecasts
Below is a screen shot of what I’m looking for
-- thank you!

MESSAGES (23)

Jean LeBean 1

Mark Brewer 1

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CALCULATED TABLE JOINS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CALCULATED TABLE JOINS

Calculated table joins are used to combine two or more tables of data; common examples
include CROSSJOIN, UNION, EXCEPT and INTERSECT

TOPICS WE’LL COVER: COMMON USE CASES:


• Blending or combining data across multiple tables
CROSSJOIN UNION
• Creating advanced calculations like new vs. returning
users or repeat purchase behavior
EXCEPT INTERSECT • Querying tables to troubleshoot errors or better
understand relationships in a data model

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CROSSJOIN
Product of two sets, forming a new
set containing all ordered pairs

CROSSJOIN() Returns a table that contains the cartesian product of the specified tables

CROSSJOIN
=CROSSJOIN(Table, Table, […] ] ) Product Product Product Product Store
Category Group Category Group ID
Coffee Beverages Coffee Beverages 3
UNION Tea Beverages Tea Beverages 3
List of table expressions to include in the crossjoin
Bakery Food Bakery Food 3
Examples: Chocolate Food Chocolate Food 3

• ‘Product Lookup’ Coffee Beverages 5


EXCEPT
• VALUES( Tea Beverages 5
‘Sales by Store’[store_id)) CROSSJOIN Bakery Food 5
Chocolate Food 5
INTERSECT Coffee Beverages 8
Store ID
3 Tea Beverages 8
Bakery Food 8
IMPORTANT NOTES: 5
8 Chocolate Food 8
✓ Column names must all be different in all table arguments
✓ The # of rows returned equals the product of rows in all tables Resulting table contains 12 rows (4*3)
✓ The # of columns returned equals the sum of columns in all tables and 3 columns (2+1)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


UNION

UNION() Combines or ”stacks” rows from two or more tables sharing the same column structure

CROSSJOIN =UNION(Table, Table, […] )


Date ABC XYZ 123
5/1/19 A X 1 Date ABC XYZ 123
5/1/19 B Y 2 5/1/19 A X 1
UNION
Accepts any DAX expression for two (or more) 5/1/19 C Z 3 5/1/19 B Y 2
tables with identical column structure Date
5/1/19 ABC
C XYZ
Z 123
3

Examples: UNION 5/1/20 A X 1


EXCEPT
• ‘Sales Target 2019’, 5/1/20 B Y 2
‘Sales Target 2020’ Date ABC XYZ 123 5/1/20 C Z 3
• ‘Sales Target 2019’, DATATABLE( ) 5/1/20 A X 1
INTERSECT UNION stacks tables together,
5/1/20 B Y 2
just like append!
5/1/20 C Z 3
IMPORTANT NOTES:
✓ All tables must contain the same number of columns
✓ Columns are combined by position in their respective tables
✓ Column names are determined by the first table expression
✓ Duplicate rows are retained

*Copyright 2020, Excel Maven & Maven Analytics, LLC


EXCEPT

EXCEPT() Returns all rows from the left table which do not appear in the right table

CROSSJOIN

=EXCEPT (LeftTable, RightTable)


UNION
The left and right tables that will be used for joining
(NOTE: First table must be a table within the data model) LEFT RIGHT

EXCEPT
Example (churned customers):
=EXCEPT(‘Customers 2019’​,‘Customers 2020’)

Resulting table contains rows which


ONLY appear in the left table
INTERSECT

IMPORTANT NOTES:
✓ Both tables must contain the same number of columns
✓ Columns are compared based on positioning in their respective tables
✓ Column names are determined by the left table
✓ The resulting table does NOT retain relationships to other tables (can’t be used as an expanded table)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


INTERSECT

INTERSECT() Returns all the rows from the left table which also appear in the right table

CROSSJOIN
=INTERSECT(LeftTable, RightTable )

The left and right tables that will be used for joining
UNION
(NOTE: First table must be a table within the data model) LEFT RIGHT
Example (previous month active customers):
EXCEPT LeftTable: RightTable:
VALUES(‘Sales’[Customer ID]) CALCULATETABLE(
VALUES(​’Sales’[Customer ID]), Resulting table contains rows
DATEADD(‘Calendar’[Date],-1, MONTH)) which appear in BOTH tables
INTERSECT

IMPORTANT NOTES:
✓ Order matters! (T1, T2) may have a different result set than (T2, T1)
✓ Columns are compared based on positioning in their respective tables
✓ Duplicate rows are retained
✓ Column names are determined by the left table
✓ The resulting table does NOT retain relationships to other tables (can’t be used as an expanded table)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: CALCULATED TABLE JOINS

MAVEN
ROASTERS Mark Brewer (CFO)
12:56 PM KEY OBJECTIVES:
CHANNELS (12)
1. Use CALCULATETABLE &
I’m looking at some of the revenue and profit
number and was hoping that you could tell me INTERSECT to look at
a little bit about our returning customers returning customers between
from 11/4/2018 – 11/17/2018 (weeks 45 & 11/4/2018 & 11/17/2018
46) and what their purchase behavior is like?
2. Add additional columns for
Revenue and Profit
MESSAGES (10)
I’d love to get an idea of revenue and profit for 3. Determine the total Revenue
Darren Xu these customers and Profit for repeat
Jamie Toast customers on week 46
Jean LeBean

Lisa Latte
Mark Brewer 1

*Copyright 2020, Excel Maven & Maven Analytics, LLC


RELATIONSHIP FUNCTIONS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


RELATIONSHIP FUNCTIONS

Relationship functions allow you to access fields within DAX measures or calculated columns
through either physical or virtual relationships between tables

TOPICS WE’LL COVER: COMMON USE CASES:


• Defining calculated columns or measures using fields
Physical vs. Virtual
RELATED from related tables
Relationships
• Handling relationships between calendars and multiple
date fields (transaction date, stock date, due date, etc.)
RELATEDTABLE USERELATIONSHIP
• Traversing inactive relationships in the model, or
modifying relationship filter behavior
CROSSFILTER TREATAS • Defining calculated, virtual relationships when physical
relationships aren’t possible due to table granularity

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PHYSICAL VS. VIRTUAL RELATIONSHIPS

There are two key types of table relationships: PHYSICAL and VIRTUAL
Physical vs. Virtual • Physical relationships are manually created, and visible in your data model
Relationships
• Virtual relationships are temporary, and defined using DAX expressions

RELATED

RELATEDTABLE
These are physical relationships:
• Visible links between tables (typically 1:* cardinality)
USERELATIONSHIP
• Can be active or inactive
• Can be accessed using DAX functions like RELATED,
CROSSFILTER RELATEDTABLE or USERELATIONSHIP
• Best way to connect tables (but not always possible)

TREATAS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PHYSICAL VS. VIRTUAL RELATIONSHIPS

There are two key types of table relationships: PHYSICAL and VIRTUAL
Physical vs. Virtual • Physical relationships are manually created, and visible in your data model
Relationships
• Virtual relationships are temporary, and defined using DAX expressions

RELATED

RELATEDTABLE
This is a virtual relationship:
• Defined using DAX expressions
USERELATIONSHIP
• Used when a physical relationship doesn’t exist, or
cannot be created directly
CROSSFILTER • Often used to connect tables with different levels of
granularity (i.e. daily sales vs. monthly budgets/goals)
• Can be accessed using DAX functions like TREATAS
TREATAS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


RELATED

Physical vs. Virtual


Relationships RELATED() Returns a value from a related table in the data model

RELATED
=RELATED(ColumnName)

RELATEDTABLE

Name of the column you want to retrieve values PRO TIP


from (must reference a table on the “one” side of The RELATED function doesn’t
USERELATIONSHIP
a many-to-one relationship) really perform an operation, it
just “opens the door” to access
Examples:
columns from an expanded table
CROSSFILTER
• ‘Product Lookup’[current_cost]
• ‘Customer Lookup’[home_store]

TREATAS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


RELATEDTABLE

Physical vs. Virtual


RELATEDTABLE() Returns a related table, filtered so that it only includes the related rows
Relationships

=RELATEDTABLE(Table)
RELATED

RELATEDTABLE Physical table you want to retrieve rows from (must reference
a table on the “many” side of a many-to-one relationship)

NOTE: RELATEDTABLE is commonly used with aggregators like


HEY THIS IS IMPORTANT!
USERELATIONSHIP
COUNTROWS, SUMX, AVERAGEX, etc. RELATEDTABLE is a shortcut for
CALCUATETABLE (with no logical
Examples: expression) and performs a context
CROSSFILTER transition from row context to filter
• COUNTROWS(
RELATEDTABLE(‘Food Inventory’)) context, in order to return only the rows
which satisfy the filter condition(s)
TREATAS • SUMX(
RELATEDTABLE(‘Food Inventory’),
[Quantity Sold] * [Retail Price])

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: RELATEDTABLE

KEY OBJECTIVES:
MAVEN
ROASTERS Lisa Latte (Manager)
1:47 PM 1. Add a calculated column to the
Customer Lookup table and
CHANNELS (12)
create a variable for “Total Sales”
Hey there! Would it be possible to add a new
field to our customer records to label high 2. Add another variable called
value customers? “AllCustomers” to count total
customers

I’m thinking we could flag customers as “high 3. Add another variable that defines
value” if they have purchased more than the average sales
MESSAGES (13) average customer. Let me know! 4. Use RELATEDTABLE to create a
Darren Xu
variable that computes sales at
Jamie Toast the customer level
Jean LeBean
5. Return “High” if customer-level
Lisa Latte 1
sales are above the average.
Otherwise, return “Low”

*Copyright 2020, Excel Maven & Maven Analytics, LLC


USERELATIONSHIP

Specifies an existing relationship to be used in the evaluation of a DAX expression,


Physical vs. Virtual USERELATIONSHIP() defined by naming, as arguments, the two columns that serve as endpoints
Relationships

RELATED =USERELATIONSHIP(ColumnName1, ColumnName2)

RELATEDTABLE
Foreign (or primary) key of the Primary (or foreign) key of the HEY THIS IS IMPORTANT!
relationship relationship
USERELATIONSHIPS can only
USERELATIONSHIP Examples: Examples: be used in functions which
accept a filter parameter
• Food Inventory[Baked_Date] • Calendar[Transaction Date] (CALCULATE, TOTALYTD, etc.)
CROSSFILTER • Calendar[Transaction Date] • Food Inventory[Baked_Date]

TREATAS
PRO TIP:
If you have multiple date columns connected to a single calendar table, USERELATIONSHIP is a great way to
force measures to use inactive relationships without having to manually activate them in your model

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CROSSFILTER

Specifies cross filtering direction to be used for the duration of the DAX expression.
Physical vs. Virtual
Relationships
CROSSFILTER() The relationship is defined by naming the two columns that serve as endpoints

RELATED =CROSSFILTER(LeftColumnName, RightColumnName2, CrossFilterType )

RELATEDTABLE
The two columns you want to use. Left
column is typically the “many” side and
right column is typically the “one” side Specifies the direction of the CROSSFILTER

USERELATIONSHIP Examples: Examples:


• OneWay, Both, None
• 'Sales by Store'[customer_id]
CROSSFILTER • ‘Customer Lookup'[customer_id]

TREATAS
PRO TIP
Instead of bi-directional relationships, use CROSSFILTER to enable two-way filtering ONLY in specific cases

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: CROSSFILTER

MAVEN
ROASTERS Darren A. Xu (Manager) KEY OBJECTIVES:
9:19 AM
CHANNELS (12) 1. Create a measure called
How easy would it be to figure out the “Customers who Purchased” that
average order value for customers who make uses COUNTROWS & CROSSFILTER
purchases each month, broken down by to calculate the number of
different product groups? customers who made a purchase
in a given time period
Hint: Think about filter direction
I’d love to use that data to see if there are any
MESSAGES (28) interesting patterns or trends! 2. Create a measure that calculates
the average order value for
Darren Xu 1
“Customers who Purchased”
Jean LeBean
3. Create a matrix that that shows
Lisa Latte the previous two measures broken
down by Darren’s request

*Copyright 2020, Excel Maven & Maven Analytics, LLC


TREATAS

Applies the result of a table expression to filter columns in an unrelated table


TREATAS() (essentially creating a new, virtual relationship)
Physical vs. Virtual
Relationships
=TREATAS(TableExpression, ColumnName, [ColumnName], […])
RELATED

A table expression which generates the set of The list of output columns (cannot be an expression)
columns to be mapped. Table expression must be NOTE: The number of columns specified must match
RELATEDTABLE
based on physical table in data model. the number of columns in the table expression and
Examples: be in the same order
USERELATIONSHIP
• TREATAS( Examples:
VALUES(‘Calendar’[Year_ID)…
• ‘Target Sales’[Year]
• TREATAS(
CROSSFILTER • ‘Target Sales’[Year],
SUMMARIZE(‘Calendar’,
‘Calendar’[Year_ID], ‘Calendar[Month]… ‘Target Sales’[Month]

TREATAS
PRO TIP:
Use physical relationships (or USERELATIONSHIP functions) whenever possible, and only rely on
TREATAS if you are unable to create a direct relationship between tables

*Copyright 2020, Excel Maven & Maven Analytics, LLC


TREATAS (EXAMPLE)

In this case, we can’t create physical relationships


Physical vs. Virtual
Relationships to Target Sales since it’s at a different level of
granularity than the other tables in the model
RELATED

Target Sales is at the Month & Year level


RELATEDTABLE

Both Sales by Store and Calendar are at the Date (daily) level

USERELATIONSHIP

TREATAS allows us to create virtual, summarized


CROSSFILTER
versions of our tables to match the granularity
that we need to form a valid relationship
TREATAS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: SALES TARGETS (TREATAS)

MAVEN KEY OBJECTIVES:


ROASTERS Jean LeBean (CEO)
2:52 PM
1. Based on the Target Sales
CHANNELS (12)
Hi! I have a call with the board this Thursday
Union table, use TREATAS and
and need to share where our sales landed create measures for Bean/Tea,
against March and April targets. Beverage, Merchandise &
Food sales goals
Could you please pull those numbers for me? Bonus: Use SUMMARIZE with TREATAS

2. Create % of goal measures


MESSAGES (8)
that compare quantity sold to
Darren Xu
the goal amount
Jamie Toast
Jean LeBean 1 3. Add the above measures to a
matrix broken down by store
& target months

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ITERATOR FUNCTIONS

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ITERATORS

Iterator functions allow you to loop through the same expression on every row of a table in
order to evaluate a single scalar value (i.e. max, min, average) or derive a new table

TOPICS WE’LL COVER: COMMON USE CASES:

• Aggregating a column into a single value (i.e. average


Iterator Review Iterator Cardinality customer age, max product price, count of orders, etc.)
• Returning a table of data (i.e. ADDCOLUMNS and
CONCATENATEX AVERAGEX SELECTCOLUMNS)

RANKX

*Copyright 2020, Excel Maven & Maven Analytics, LLC


REVIEW: BASIC ITERATORS

SUMX() Returns the sum of an expression evaluated for each row in a table
Iterator Review
=SUMX(Table, Expression)

Iterator
Cardinality Aggregation to apply to
Table in which the expression will be Expression to be evaluated for
calculated rows
evaluated
each row of the given table
Examples:
CONCATENATEX Examples:
Examples:
• SUMX
• ‘Sales by Store’
• COUNTX • [Total Orders]
• FILTER( Sales,
AVERAGEX • AVERAGEX RELATED( • ‘Sales’[RetailPrice] * ‘Sales’[Quantity]
• RANKX ‘Products’[Category])=“Clothing”)
• MAXX/MINX

RANKX
PRO TIP:
Imagine the function adding a temporary new column to the table, calculating the value in
each row (based on the expression) and then applying the aggregation to that new column

*Copyright 2018, Excel Maven & Maven Analytics, LLC


ITERATOR CARDINALITY

Iterator cardinality is the number of rows in the table(s) being iterated; the more
Iterator Review
unique rows, the higher the cardinality (this is different from relationship cardinality)

Sales by Store
Iterator Index Date Price Product ID Quantity When iterators reference a single table, cardinality is simply
Cardinality the number of unique rows (in this case 912,424)
1 1/1/2017 $18 1 2
2 1/1/2017 $18 2 6 When using nested iterators, cardinality depends on
CONCATENATEX 3 1/1/2017 $14.75 3 1 whether you are using physical or virtual relationships:
… … … … … • For physical relationships, cardinality is defined as the
912,422 4/30/2019 $10 10 1 max number of unique rows in the largest table
912,423 4/30/2019 $3.75 79 2 • For virtual relationships, cardinality is defined as the
AVERAGEX
number of unique rows in each table multiplied together
912,424 4/30/2019 $3.75 36 1

RANKX

HEY THIS IS IMPORTANT!


When using nested iterators, only the innermost “X” function can be optimized by the DAX engine.
Nested iterators aren’t always a bad thing, but they can have significant performance implications

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ITERATOR CARDINALITY (CONT.)

Physical relationship
Iterator Review
• Relationship between ‘Product Lookup’
and ‘Sales by Store’

Iterator
• ‘Product Lookup’ contains 88 rows
Cardinality • ‘Sales by Store’ contains 907,841 rows

Cardinality = 907,841
CONCATENATEX

Virtual relationship
AVERAGEX
• No physical relationship between
‘Product Lookup’ and ‘Sales by Store’
• ‘Product Lookup’ contains 88 rows
RANKX
• ‘Sales by Store’ contains 907,841 rows

Cardinality = 79,890,008

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CONCATENATEX

Iterator Review Evaluates an expression for each row of the table and returns the concatenation of
CONCATENATEX() those values in a single string, separated by a delimiter

Iterator
Cardinality =CONCATENATEX( Table, Expression, [Delimiter], [OrderBy_Expression], [Order])

CONCATENATEX
Table or table expression that Column that contains values to Optional arguments:
contains the rows you want concatenate or an expression that
• Delimiter: Used with concatenated expression
to return returns a value
• Examples: “,” “&” “-” “;” etc.
AVERAGEX
Examples: Examples: • OrderBy Expression: Expression used to sort the table
• Examples: Product Lookup[Product Category]
• ‘Product Lookup’ • ‘Product’[Category]
• CONCATENATEX( • ‘Employee’[Name] • Order: Order of results applied
RANKX • Examples: ASC, DESC
VALUES(‘Employee Lookup’)… • [Customer Sales]
• 7

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PRO TIP: ADDING DYNAMIC LABELS

Iterator Review

Iterator
Cardinality

CONCATENATEX The Selected Product Category measure


uses CONCATENATEX to capture the
selections from the slicer...

AVERAGEX

Product Category slicer is


RANKX filtered to Coffee, Coffee Beans
& Drinking Chocolate
...which can be displayed as a dynamic label
using a card to show selected items

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: CONCATENATEX

MAVEN
ROASTERS Darren A. Xu (Manager)
2:50 PM KEY OBJECTIVES:
CHANNELS (12)
1. Create a visual for Store 5
Hey there! I’m hoping to look into Store 5
sales by employee, to see which employees that shows customer sales
account for largest percentage of total sales. by store and employee id
2. Create a measure to show %
of Total Sales for Store 5
Could you please create a visual that shows
Sales and % of Total Sales, with the ability to 3. Use CONCATENATEX to
MESSAGES (17) filter for any combination of Employee IDs? define a measure that shows
Darren Xu 1 the employee name(s)
selected and the % of Total
Sales for Store 5

*Copyright 2020, Excel Maven & Maven Analytics, LLC


AVERAGEX

Iterator Review AVERAGEX() Calculates the average (arithmetic mean) of a set of expressions evaluated over a table

=AVERAGEX(Table, Expression)
Iterator
Cardinality

Table, or table expression, that The expression that you want to evaluate
CONCATENATEX contains the rows to evaluate
Examples:
Examples:
• [Customer Sales]
• ‘Calendar’ • SUM[quantity_sold]
AVERAGEX
• ‘Product Lookup’

RANKX
HEY THIS IS IMPORTANT!
AVERAGE & AVERAGEX do NOT count days with zero sales when computing an average. To evaluate an
average over a date range that includes dates with no sales, use DIVIDE & COUNTROWS instead

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PRO TIP: MOVING AVERAGES

Iterator Review
Here we’re using MAX, FILTER & ALL
to create a 30-day rolling time period
(based on the latest transaction date)
Iterator Once the rolling window is defined,
Cardinality
we can use AVERAGEX to calculate
the average sales over that period
CONCATENATEX • Note that we could also use
SUMX or COUNTX to calculate
the rolling total/count

AVERAGEX

PRO TIP:
Use a parameter to create a
RANKX dynamic, user-defined period!

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: MOVING AVERAGES

MAVEN
ROASTERS Mark Brewer (CFO)
3:33 PM KEY OBJECTIVES:
CHANNELS (12)
1. Create a matrix to show total
Could you get me a visual showing our total and daily average profit by
and average profit, trended monthly from Jan month, for Jan 2018 – Apr 2019
2018 through Apr 2019?
2. Use GENERATESERIES &
SELECTEDVALUE to create a
Barry’s Beans closed their shop last month, parameter with increments of 7
and I’d like to track our profits to see the days over a 9-week period
MESSAGES (15)
impact. Could you please include a weekly
moving average that I can adjust as needed? 3. Create a measure using
Darren Xu AVERAGEX and the parameter
Jamie Toast you defined to calculate the
moving average profit

Mark Brewer 1

*Copyright 2020, Excel Maven & Maven Analytics, LLC


RANKX

RANKX() Returns the ranking of a number in a list of numbers for each row in the table argument

Iterator Review
=RANKX(Table, Expression, [Value], [Order], [Ties])

Iterator Optional arguments:


Cardinality
Table or DAX expression An expression that returns a scalar • Value: Any DAX expression that returns a single scalar
that returns a table value, evaluated at each row of the table value whose rank is to be found (by default, the value in the
current row is used)
CONCATENATEX Examples: Examples:
• Order: Specifies how to rank (low-high vs. high-low)
• ALL( • [Customer Sales] • Examples: ASC or DESC
‘Product Lookup’[Product]) • SUM( • Ties: Determines how ties and following ranks are treated:
‘Sales by Store’[quantity_sold]) • SKIP (default): Skips ranks after ties
AVERAGEX
• DENSE: Shows the next rank, regardless of ties

SKIP: DENSE:
PRO TIP:
RANKX
If your “Total” row is showing a rank of 1, use IF &
HASONEVALUE with RANKX to exclude it from the rank!

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: RANKX

MAVEN
ROASTERS Jamie Toast (Head Roaster)
9:13 AM KEY OBJECTIVES:
CHANNELS (12)
Hey there, I’m curious to see our profits for
1. Use variables and RANKX
the best-selling products in each category. to create a single measure
for Top 5 Products
Would you be able to show me a visual at the 2. Create a slicer for Product
product category level, only including profit Category to be able to
for the top 5 products within each category?
analyze the top 5 across
MESSAGES (23)
different categories
Darren Xu 1
Jamie Toast
Jean LeBean

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ADVANCED TIME INTELLIGENCE

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ADVANCED TIME INTELLIGENCE

DAX offers a range of powerful time intelligence functions, which allow you to build custom
calendars, define dynamic date ranges, and compare performance across specific periods

TOPICS WE’LL COVER: COMMON USE CASES:


• Building custom calendar tables (rather than using
Automatic Date Tables Calendar Functions default, auto-generated versions)
• Calculating period-over-period, moving average, or
running total calculations
Time Intelligence
Date Formatting • Managing non-traditional time periods like week-
Functions
based retail calendars (5-4-5 or 5-4-4)
Week-Based
Calculations

*Copyright 2020, Excel Maven & Maven Analytics, LLC


AUTOMATIC DATE TABLES

Automatic Date By default, Power BI automatically creates a hidden date table for any table that
Tables contains a Date or DateTime column on the one-side of a relationship
• Auto-generated calendars include all dates through the end of the year, regardless
Calendar
Functions of the actual date range in the table

Date Formatting

Time Intelligence
Functions

Week-Based
Calculations

Automatically creates a hidden date table containing all these columns

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PROS & CONS: AUTOMATIC DATE TABLES

Automatic Date
PROS: CONS:
Tables

• Automatically generated • Hidden from view, cannot be modified/customized


Calendar
Functions • Enables (some) time intelligence • Generated for every date field across every
functionality by default lookup/dimension table (bloats model size)
• Simplifies data model creation • Can’t be enabled or disabled at the table-level
Date Formatting
and management
• Hierarchies aren’t automatically generated (if grouped by
• Does not require an advanced month, would summarize that month across ALL years)
Time Intelligence understanding of DAX
Functions • Each automatic date table can only filter the table it
corresponds to (cannot traverse table relationships)

Week-Based
Calculations
PRO TIP:
Turn OFF the auto date/time feature in Power BI Desktop and either import a date
dimension table or create your own using CALENDAR functions (more on this soon!)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DATE TABLE REQUIREMENTS

Automatic Date
Tables
If you import or create your own date table, it must meet these requirements:

✓ Must contain all the days for all years represented in your fact tables
Calendar
Functions ✓ Must have at least one field set as a Date or DateTime datatype
✓ Cannot contain duplicate dates or datetime values
Date Formatting ✓ If using a time component within a date column, all times must be
identical (i.e. 12:00)
✓ Should be marked as a date table (not required but a best practice)
Time Intelligence
Functions

HEY THIS IS IMPORTANT!


Week-Based
Calculations If Time is present in your date field, split the time component into a new column
(this adheres to relationship requirements and decreases column cardinality)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CALENDAR

Automatic Date
Tables
CALENDAR() Returns a table with one column of all dates between start and end date

Calendar =CALENDAR(StartDate, EndDate)


Functions

Start and End dates of your calendar table. Can be explicitly


Date Formatting assigned or defined using DAX functions (i.e. MIN/MAX)

Examples:
Time Intelligence • CALENDAR(
Functions DATE( 2019,01,01), DATE(2020,12,31))
• CALENDAR(
DATE( YEAR ( MIN(Sales by Store[Transaction Date] )), 1, 1),
Week-Based
Calculations DATE( YEAR ( MAX(Sales by Store[Transaction Date] )), 12, 31)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


CALENDARAUTO

Automatic Date Returns a table with one column of dates based on a fiscal year end month. The
Tables CALENDARAUTO() Range of dates is calculated automatically based on data in the model

Calendar =CALENDARAUTO(FiscalYearEndMonth)
Functions

An integer from 1 to 12 that represents the last month of the fiscal year.
Date Formatting Can be explicitly assigned or created using DAX functions (i.e. MIN/MAX)

Examples:
Time Intelligence • CALENDARAUTO(6)
Functions
• Calendar Table =
VAR MinYear = YEAR( MIN( ‘Sales by Store’[Transaction Date]))
Week-Based VAR MaxYear= YEAR( MAX( ‘Sales by Store’[Transaction Date]))
Calculations RETURN
FILTER( CALENDARAUTO(),
YEAR( [Date] ) >= MinYear &&
YEAR( [Date] ) <= MaxYear )

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PRO TIP: BUILDING A REUSABLE DATE TABLE

Automatic Date
Tables

Calendar
Functions

This code can be repurposed and


recycled across multiple data models!
Date Formatting

Time Intelligence
Functions

Week-Based
Calculations

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DATE FORMATTING

Automatic Date
Use the FORMAT function to specify date/time formatting. Common examples include:
Tables

Code Description Example


Calendar dddd Display the full-day name Sunday, Monday, etc.
Functions
ddd Display the short-day name Sun, Mon, Tue, etc.

Date Formatting mmmm Show full month name July, August, etc.

mmm Show abbreviated month name Jul, Aug, Sept, etc.

Time Intelligence mm Display the month as a two-digit number 08, 09, 10, 11, etc.
Functions
q Display the quarter of the year 1, 2, 3, 4

Week-Based yyyy Show the year as a four-digit number 2018, 2019, 2020, etc.
Calculations

*Head to https://docs.microsoft.com/en-us/dax/custom-date-and-time-formats-for-the-format-function for more


information about custom date and time formats and a complete list of user-defined date/time formats
*Copyright 2020, Excel Maven & Maven Analytics, LLC
ASSIGNMENT: DATE FORMATTING

MAVEN
KEY OBJECTIVES:
ROASTERS Mark Brewer (CFO)
3:15 PM 1. Add columns to the calendar
CHANNELS (12) table to capture the weekday
Hey, I’m wondering what portion of our sales number and name
come in on weekends vs. weekdays. Could you
help with a quick analysis? 2. Add a binary Y/N column to
flag weekend dates

Jean and I would love to see a breakdown of 3. Use a Matrix visual to show
weekend vs. weekday sales, shown as a the percent of total sales for
MESSAGES (23) percent of total. Thanks! weekdays vs weekends

Jean LeBean

Lisa Latte
Mark Brewer 1

*Copyright 2020, Excel Maven & Maven Analytics, LLC


COMMON TIME INTELLIGENCE FUNCTIONS

Automatic Date
Tables Time Intelligence functions allow you to define and compare custom time periods:

Calendar
Functions Performance-to-Date Time Period Shift Running Total

Functions commonly used to calculate Functions commonly used to compare Functions commonly used to calculate
Date Formatting performance through the current date performance between specific periods running totals or moving averages

Common Examples: Common Examples: Common Examples:


Time Intelligence
Functions • DATESYTD • SAMEPERIODLASTYEAR • DATESINPERIOD
• DATESQTD • DATEADD
• DATESMTD • PARALLELPERIOD
Week-Based • PREVIOUSYEAR
Calculations (QUARTER/MONTH/DAY)
• NEXTYEAR
(QUARTER/MONTH/DAY)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


REVIEW: COMMON PATTERNS

Use these common time intelligence patterns for YTD, period-over-period, or running total calculations:

Performance = CALCULATE(Measure, DATESYTD(Calendar[Date]))


To-Date Use DATESQTD for Quarters or DATESMTD for Months

Previous = CALCULATE(Measure, DATEADD(Calendar[Date], -1, MONTH))


Period
Select an interval (DAY, MONTH, QUARTER, or YEAR) and the
# of intervals to compare (i.e. previous month, rolling 10-day)
Running = CALCULATE(Measure,
Total DATESINPERIOD(Calendar[Date], MAX(Calendar[Date]), -10, DAY))

PRO TIP:
To calculate a moving average, use a running total calculation and divide by the number of intervals

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PARALLELPERIOD

Returns a column of dates from a parallel period, by shifting the dates specified
Automatic Date PARALLELPERIOD() forward or backward in time based on a given interval (month/quarter/year)
Tables

=PARALLELPERIOD(Date, NumberOfIntervals, Interval)


Calendar
Functions

The name of a column containing dates Number of Intervals (positive or negative); Interval value can only be:
Date Formatting
or a one column table containing dates If a decimal is supplied, number is rounded • Month
to nearest whole integer • Quarter
Example: • Year
Time Intelligence Example:
• Calendar[Transaction_Date]
Functions
• 1, 2, 3, etc.
• -1,-2,-3 etc.
• -1.4 (-1) -1.5 (-2)
Week-Based
Calculations
HEY THIS IS IMPORTANT!
PARALLELPERIOD computes the entire period in the interval (i.e. entire year, quarter, etc.).
Values in total rows may not reflect the expected total if partial periods are present!

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PARALLELPERIOD EXAMPLE

Automatic Date
Tables

Calendar
Functions

Total returned for the entire


previous quarter on each row
Date Formatting

Time Intelligence
Functions

Week-Based
Calculations
Year-level total = Q4 2018 + Q1 2019

Overall total is “missing” April 2019

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PREVIOUSQUARTER

Returns a table containing a column of all dates from the previous quarter, based
Automatic Date PREVIOUSQUARTER on the first date in the date range specified
Tables

Calendar =PREVIOUSQUARTER(Dates)
Functions

The name of a column containing dates When first date specified is


Date Formatting or a one column table containing dates 5/9/2017, totals for May and
June reflect Jan-Mar (Q1) 2017
Example:
Time Intelligence • Calendar[Transaction_Date] 2018 Total row reflects the
Functions total for Q4 2017

Week-Based
Calculations HEY THIS IS IMPORTANT!
PREVIOUSQUARTER works similarly to With no date context, total is
PARALLELPERIOD (QUARTER) and computes the based on first visible date in
entire prior period (but handles totals differently) the table (5/9/2017)

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SAMEPERIODLASTYEAR

Automatic Date
Tables
SAMEPERIODLASTYEAR() Returns a set of dates in the current selection from the previous year

Calendar = SAMEPERIODLASTYEAR(Dates)
Functions

Date Formatting The name of a column containing dates


or a one column table containing dates

Example:
Total Sales for 3/24/2017 –
Time Intelligence
• Calendar[Transaction_Date] 3/31/2017
Functions

Week-Based HEY THIS IS IMPORTANT!


Calculations Pay attention to totals and filter context!
SAMEPERIODLASTYEAR is equivalent to 2019 total has filter context
DATEADD(‘Dates’, -1, YEAR) and may return of 1/1/2019 – 4/30/2019,
values which aren’t always intuitive or and returns 2018 YTD total
straightforward to understand through April

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SYNTAX SUGAR: SAMEPERIODLASTYEAR

Automatic Date
Tables

SAMEPERIODLASTYEAR is really just a shortcut for DATEADD and YEAR:


Calendar
Functions
How it’s written: How it’s interpreted:

1 Store 3 Sales – Last Year = 1 Store 3 Sales – Last Year =


Date Formatting 2 CALCULATE( 2 CALCULATE(
3 [Total Sales], 3 [Total Sales],
4 DATEADD(
4 SAMEPERIODLASTYEAR(‘Calendar’[Transaction_Date])
5 ‘Calendar’[Transaction_Date],
Time Intelligence 5 )
6 -1,
Functions
7 YEAR)
8 )
Week-Based
Calculations

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: TIME PERIODS

MAVEN
ROASTERS Jean LeBean (CEO)
3:15 PM KEY OBJECTIVES:
CHANNELS (12)
1. Using time intelligence
Hey there! I’m sure you’ve seen the increase
in our sales since Barry’s Beans went out of
functions to calculate total
business last month. Could you please share sales for the previous month,
the month-over-month change in sales from as well as the percent change
Mar-Apr 2019? month-over-month
2. Calculate the year-over-year
MESSAGES (23) One more thing – could you also look at April change in sales from April
2019 compared to April 2018? I’d love to see 2018 vs. April 2019
the YoY sales comparison too... Thanks!
Jean LeBean 1

*Copyright 2020, Excel Maven & Maven Analytics, LLC


WEEK-BASED CALCULATIONS

Automatic Date
Tables
Week-based DAX calculations (i.e. previous week sales, same week last year)
can be especially challenging for several reasons:
Calendar • Can’t use standard DAX functions (i.e. PARALLELPERIOD, SAMEPERIODLASTYEAR)
Functions
• Weeks can start on various days (Sunday, Monday, etc.)
• There can be partial weeks in any given month, quarter or year (i.e. partial 53rd week)
Date Formatting
• Weeks can be grouped differently based on different fiscal calendars (i.e. 5-4-4, 4-5-4)

Time Intelligence
Functions

PRO TIP:
Week-Based Using a 5-4-4 or 4-5-4 fiscal calendar (common in the retail industry) can enable some
Calculations week-based calculations, but you still can’t use standard DAX time intelligence functions

*Copyright 2020, Excel Maven & Maven Analytics, LLC


4-4-5 & 4-5-4 FISCAL CALENDARS

Automatic Date
Tables 4 weeks

In a 4-4-5 fiscal calendar, the


Calendar months in each quarter contain
Functions
4 weeks
exactly 4, 4, and 5 weeks

Date Formatting 5 weeks

Time Intelligence
Functions
PROS: CONS:
Week-Based • Standardizes the number of • Does not have standard start & end
Calculations dates for years, quarters, or months
days in each week (7)
• Standardizes the number of • Cannot be used with standard DAX
weeks in each year (52) time intelligence functions

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PREVIOUS FISCAL WEEK (4-5-4)

Automatic Date To calculate performance for the previous fiscal week, you can use DATEADD
Tables with an interval of -7 days:

Calendar
Functions

This works with 4-5-4 or 4-4-5 fiscal calendars, but


Date Formatting
can break down when applied to standard calendars
where years often contain partial weeks

Time Intelligence
Functions

Week-Based
Calculations PRO TIP:
Use DATEADD with an interval of -364 days to compare the same week last year!

*Copyright 2020, Excel Maven & Maven Analytics, LLC


FISCAL PERIOD TO DATE (4-5-4)

Automatic Date
Instead of a standard DAX performance-to-date functions (i.e. DATESYTD, DATESMTD),
Tables you can use a combination of CALCULATE, MAX, and HASONEVALUE:

Calendar
Functions Here we’re calculating QTD sales based on a 4-5-4 fiscal calendar

Date Formatting

You can replace FiscalQuarter with


Time Intelligence the fiscal year or fiscal month for YTD
Functions and MTD calculations

Week-Based
Calculations

*Copyright 2020, Excel Maven & Maven Analytics, LLC


FISCAL PREVIOUS PERIOD (4-5-4)

Automatic Date
Instead of a standard DAX time period shifting functions (i.e. PREVIOUSYEAR,
Tables PARALLELPERIOD), you can use CALCULATE, FILTER, ALL and SELECTEDVALUE:

Calendar Here we’re calculating QTD sales for the previous quarter based on a 4-5-4 fiscal calendar
Functions

Date Formatting
You can replace FiscalQuarter with
the fiscal month or fiscal week for
MTD and WTD calculations
Time Intelligence Just make sure to update the fiscal
Functions period to either 4, 12 or 52!

Week-Based
Calculations

*Copyright 2020, Excel Maven & Maven Analytics, LLC


ASSIGNMENT: 4-5-4 CALENDAR

MAVEN
ROASTERS Lisa Latte (Manager)
3:15 PM
CHANNELS (12) KEY OBJECTIVES:
Hey there! Looks like we’ve shifted into using
a standard 4-5-4 calendar. I’d love to be able 1. Using the 4-5-4 Calendar and
to look at our customer sales by YTD, QTD, [Customer Sales] create
and MTD measures for YTD and MTD
sales

Also, Darren and I were chatting, and we’d 2. Create a measure to compute
MESSAGES (23) love to look at week over week sales. Could week-over-week % change
you help us out here?
Darren Xu 3. Create a matrix (or two) to
Jamie Toast
Jean LeBean 1
visualize your results

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PERFORMANCE TUNING

*Copyright 2020, Excel Maven & Maven Analytics, LLC


SNEAK PEEK: PERFORMANCE TUNING

Tools like DAX Studio and Power BI’s Performance Analyzer can you help you troubleshoot
issues, measure load times for visuals/DAX queries, and optimize your code

TOPICS WE’LL COVER: COMMON USE CASES:

Performance • Identify issues with slow-loading visuals or queries


Copy Query
Analyzer • Test and compare measures to determine the impact
on speed and performance
Optimization
DAX Studio • Optimize measures or report configurations to
Workflow minimize processing and load times

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PERFORMANCE ANALYZER

Power BI Desktop’s Performance Analyzer records user actions (like Excel’s macro
recorder), and tracks the load time (in milliseconds) for each step in the process:
Performance
Analyzer
DAX Query
• Shows the amount of time it takes for the
visual to send the query to the engines, and
Copy Query for the engines to return the result (Note:
DAX Studio can only help optimize this)

Visual Display
DAX Studio
• Shows the amount of time it takes for the
visual to populate, or “draw”, on the screen.
Includes time to retrieve web-based and
Optimization geocoded images
Workflow Other
• Shows the amount of time required by the
visual to prepare the query, wait for other
visuals to complete their queries and perform
other processing tasks

*Copyright 2020, Excel Maven & Maven Analytics, LLC


PRO TIP: COPY QUERY

The Performance Analyzer includes a Copy Query option, which you can use to copy
the actual code that DAX generates behind the scenes to produce specific visuals:
Performance
Analyzer

Copy Query

DAX Studio

Optimization
Workflow
Remember that VALUES will include a blank if it exists in the
model? Well if you see a blank option in a slicer, this is why!

*Copyright 2020, Excel Maven & Maven Analytics, LLC


DAX STUDIO

DAX Studio is a free tool that allows you to connect to your Power BI data model
to test and optimize your DAX queries

Performance
Analyzer

Copy Query

www.Daxstudio.org

DAX Studio

Optimization
Workflow

*Copyright 2019, Excel Maven & Maven Analytics, LLC


OPTIMIZATION WORKFLOW

1 Start Performance Analyzer in Power BI Desktop


• Click “Start Recording”, any actions will be logged and displayed in the Performance Analyzer pane

Performance
Analyzer 2 Analyze your query load times
• Each part of the query will likely take a different amount of time, and if the DAX Query load time is
long (100-200ms or more) you can likely optimize your code using DAX Studio
Copy Query
• Visual display load time may be decreased by changing the visual type or reducing data points

DAX Studio 3 Copy & paste or import queries into DAX Studio
• Copy and paste an individual query and analyze its performance in DAX Studio, or export the entire
performance analyzer results and use Analyze Performance Data to import a file containing all queries
Optimization
Workflow
4 Use DAX studio to optimize your code
• DAX Studio uses some specific functions which allow you to analyze your DAX queries (i.e. DEFINE,
EVALUATE and ORDER BY)
• In an optimized query, the storage engine (SE) should carry the majority of the CPU workload

*Copyright 2020, Excel Maven & Maven Analytics, LLC


WRAPPING UP

*Copyright 2020, Excel Maven & Maven Analytics, LLC


RESOURCES & NEXT STEPS

Need to brush up on your Power BI skills? Make sure to complete the


full Power BI Specialist stack from Maven Analytics:
• Up & Running with Power BI Desktop
• Publishing to Power BI Service
• Advanced DAX for Business Intelligence

Remember to check out these helpful resources for additional support:


• Dax.guide & docs.microsoft.com for DAX function reference
• Mavenanalytics.io/data-playground for free practice datasets
• pbiusergroup.com for helpful forums and local meetup groups
• Microsoft Power BI and Guy in a Cube YouTube channels for demos and advanced tutorials

Any feedback? Please take a moment to leave a rating or review!


• Please reach out if there’s anything we can do to improve your experience – we’re here to help!

*Copyright 2020, Excel Maven & Maven Analytics, LLC


THANK YOU
THANK YOU!

You might also like