Understand How To Write Any DAX Formula

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Sure, I'll explain the DAX formula RegionSales = CALCULATE(SUM(Sales[SalesAmount]),

FILTER(Sales, Sales[Category] = "Electronics", RELATED('Region'[RegionName]) = "North America")) step-


by-step with contexts and a visual example, similar to the previous explanation.

Step-by-Step Explanation with Contexts

1. FILTER Function:

 Row Context: FILTER(Sales, Sales[Category] = "Electronics" &&


RELATED('Region'[RegionName]) = "North America") iterates over each row in the Sales
table, creating a row context to evaluate if Sales[Category] is "Electronics" and
RELATED('Region'[RegionName]) is "North America".
 Result: A new table with only rows where Sales[Category] is "Electronics" and the
related Region[RegionName] is "North America".

2. SUM Function:

 Filter Context: The SUM function operates within the filter context provided by
CALCULATE. It sums the SalesAmount column in the filtered table where Category is
"Electronics" and the related Region[RegionName] is "North America".
 No Row Context: SUM does not create a row context itself; it works on the entire
column based on the filter context applied.

3. CALCULATE Function:

 Changes Filter Context: CALCULATE(SUM(Sales[SalesAmount]), FILTER(Sales,


Sales[Category] = "Electronics" && RELATED('Region'[RegionName]) = "North America"))
modifies the filter context to include only rows where Sales[Category] is
"Electronics" and the related Region[RegionName] is "North America".
 Applies New Filter Context: It evaluates SUM(Sales[SalesAmount]) within this new
filter context, summing up the SalesAmount values from the filtered rows.

Visual Example with Contexts

Imagine your Sales table looks like this:

Category SalesAmount RegionID


Electronics 100 1
Furniture 200 2
Electronics 300 1
Clothing 150 3
Category SalesAmount RegionID
Electronics 250 2
Electronics 150 1

And your Region table looks like this:

RegionID RegionName
1 North America
2 Europe
3 Asia

Step 1: FILTER(Sales, Sales[Category] = "Electronics" && RELATED('Region'[RegionName]) =


"North America")

 Row Context: Evaluates each row to include only "Electronics" and where the
related RegionName is "North America".
 Filtered Table:

Category SalesAmount RegionID


Electronics 100 1
Electronics 300 1
Electronics 150 1

Step 2: SUM(Sales[SalesAmount])

 Filter Context: Operates within the context of the filtered table where Category is
"Electronics" and the related RegionName is "North America".
 Sum Calculation:
o 100 + 300 + 150 = 550

Step 3: CALCULATE

 Changes Filter Context: Applies the filter from FILTER to modify the filter context.
 Result: Evaluates SUM(Sales[SalesAmount]) in the new filter context, giving a result of
550 for RegionSales.

This breakdown shows how each step and function interacts with the contexts (row and
filter) to achieve the final result for RegionSales.

4o
similarly this "• TotalSalesIgnoreDateFilter = CALCULATE(SUM(Sales[SalesAmount]),
ALL('Date')) "
ChatGPT

Sure, I'll explain the DAX formula TotalSalesIgnoreDateFilter =


CALCULATE(SUM(Sales[SalesAmount]), ALL('Date')) step-by-step with contexts and a visual
example.

Step-by-Step Explanation with Contexts

1. ALL Function:

 Table Context: ALL('Date') removes any existing filters on the Date table.
 Result: It returns the entire Date table, effectively ignoring any filters applied to it.

2. SUM Function:

 Filter Context: The SUM function operates within the filter context provided by
CALCULATE. It sums the SalesAmount column in the Sales table.
 No Row Context: SUM does not create a row context itself; it works on the entire
column based on the filter context applied.

3. CALCULATE Function:

 Changes Filter Context: CALCULATE(SUM(Sales[SalesAmount]), ALL('Date')) modifies the


filter context to include all rows from the Date table, removing any existing filters
on the Date table.
 Applies New Filter Context: It evaluates SUM(Sales[SalesAmount]) within this new
filter context, summing up the SalesAmount values from the Sales table without any
date-related filters.

Visual Example with Contexts

Imagine your Sales table looks like this:

Date SalesAmount
2023-01-01 100
2023-01-02 200
2023-01-03 300
2023-02-01 150
2023-02-02 250
And your Date table looks like this:

Date
2023-01-01
2023-01-02
2023-01-03
2023-02-01
2023-02-02

Let's assume that there is an existing date filter applied in a report, which only shows
data for January 2023.

Step 1: ALL('Date')

 Table Context: Removes any existing filters on the Date table.


 Result: Returns the entire Date table, ignoring any date filters applied in the
report.

Step 2: SUM(Sales[SalesAmount])

 Filter Context: Operates within the context of the Sales table, unaffected by any
date filters due to the use of ALL('Date').
 Sum Calculation:
o 100 + 200 + 300 + 150 + 250 = 1000

Step 3: CALCULATE

 Changes Filter Context: Applies the filter from ALL('Date') to modify the filter
context, ignoring any existing date filters.
 Result: Evaluates SUM(Sales[SalesAmount]) in the new filter context, giving a result of
1000 for TotalSalesIgnoreDateFilter.

Breakdown

 Initial State: There is a date filter applied to the report, showing only data for
January 2023 (100 + 200 + 300 = 600).
 ALL Function: Removes the date filter, returning all dates.
 SUM Function: Sums the SalesAmount for all rows in the Sales table.
 CALCULATE Function: Modifies the filter context to ignore the date filter,
summing the SalesAmount for all sales, resulting in 1000.
This breakdown demonstrates how each function interacts with contexts (table and
filter) to calculate the total sales amount while ignoring any date filters applied in the
report.

4o
similarly this "• TotalSalesIgnoreCustomerFilter = CALCULATE(SUM(Sales[SalesAmount]),
ALL('Customer')) "
ChatGPT

Sure, I'll explain the DAX formula TotalSalesIgnoreCustomerFilter =


CALCULATE(SUM(Sales[SalesAmount]), ALL('Customer')) step-by-step with contexts and a visual
example.

Step-by-Step Explanation with Contexts

1. ALL Function:

 Table Context: ALL('Customer') removes any existing filters on the Customer table.
 Result: It returns the entire Customer table, effectively ignoring any filters applied
to it.

2. SUM Function:

 Filter Context: The SUM function operates within the filter context provided by
CALCULATE. It sums the SalesAmount column in the Sales table.
 No Row Context: SUM does not create a row context itself; it works on the entire
column based on the filter context applied.

3. CALCULATE Function:

 Changes Filter Context: CALCULATE(SUM(Sales[SalesAmount]), ALL('Customer')) modifies


the filter context to include all rows from the Customer table, removing any
existing filters on the Customer table.
 Applies New Filter Context: It evaluates SUM(Sales[SalesAmount]) within this new
filter context, summing up the SalesAmount values from the Sales table without any
customer-related filters.

Visual Example with Contexts

Imagine your Sales table looks like this:


CustomerID SalesAmount
1 100
2 200
1 300
3 150
2 250

And your Customer table looks like this:

CustomerID CustomerName
1 John
2 Mary
3 David

Let's assume that there is an existing customer filter applied in a report, which only
shows data for CustomerID 1.

Step 1: ALL('Customer')

 Table Context: Removes any existing filters on the Customer table.


 Result: Returns the entire Customer table, ignoring any customer filters applied in
the report.

Step 2: SUM(Sales[SalesAmount])

 Filter Context: Operates within the context of the Sales table, unaffected by any
customer filters due to the use of ALL('Customer').
 Sum Calculation:
o 100 + 200 + 300 + 150 + 250 = 1000

Step 3: CALCULATE

 Changes Filter Context: Applies the filter from ALL('Customer') to modify the filter
context, ignoring any existing customer filters.
 Result: Evaluates SUM(Sales[SalesAmount]) in the new filter context, giving a result of
1000 for TotalSalesIgnoreCustomerFilter.

Breakdown
 Initial State: There is a customer filter applied to the report, showing only data
for CustomerID 1 (100 + 300 = 400).
 ALL Function: Removes the customer filter, returning all customers.
 SUM Function: Sums the SalesAmount for all rows in the Sales table.
 CALCULATE Function: Modifies the filter context to ignore the customer filter,
summing the SalesAmount for all sales, resulting in 1000.

This breakdown demonstrates how each function interacts with contexts (table and
filter) to calculate the total sales amount while ignoring any customer filters applied in
the report.

4o
similarly this "• TotalSalesWithCustomRelationship =
CALCULATE(SUM(Sales[SalesAmount]), USERELATIONSHIP(Sales[DateID],
'CustomCalendar'[DateID])) "
ChatGPT

Sure, I'll explain the DAX formula TotalSalesWithCustomRelationship =


CALCULATE(SUM(Sales[SalesAmount]), USERELATIONSHIP(Sales[DateID], 'CustomCalendar'[DateID]))
step-by-step with contexts and a visual example.

Step-by-Step Explanation with Contexts

1. USERELATIONSHIP Function:

 Relationship Context: USERELATIONSHIP(Sales[DateID], 'CustomCalendar'[DateID])


specifies a relationship between the Sales table and the CustomCalendar table on the
DateID column. This relationship is not the active relationship but is used
temporarily for the calculation.
 Result: It enables the use of the specified relationship for the duration of the
calculation.

2. SUM Function:

 Filter Context: The SUM function operates within the filter context provided by
CALCULATE. It sums the SalesAmount column in the Sales table.
 No Row Context: SUM does not create a row context itself; it works on the entire
column based on the filter context applied.

3. CALCULATE Function:
 Changes Filter Context: CALCULATE(SUM(Sales[SalesAmount]),
USERELATIONSHIP(Sales[DateID], 'CustomCalendar'[DateID])) modifies the filter context to
use the specified relationship between the Sales and CustomCalendar tables.
 Applies New Filter Context: It evaluates SUM(Sales[SalesAmount]) within this new
filter context, summing up the SalesAmount values based on the custom
relationship.

Visual Example with Contexts

Imagine your Sales table looks like this:

DateID SalesAmount
20230101 100
20230102 200
20230103 300
20230201 150
20230202 250

And your CustomCalendar table looks like this:

DateID CustomDate CustomMonth


20230101 01-01-2023 Jan 2023
20230102 02-01-2023 Jan 2023
20230103 03-01-2023 Jan 2023
20230201 01-02-2023 Feb 2023
20230202 02-02-2023 Feb 2023

Let's assume there is an existing relationship between Sales[DateID] and another date
table, but you want to use CustomCalendar for this calculation.

Step 1: USERELATIONSHIP(Sales[DateID], 'CustomCalendar'[DateID])

 Relationship Context: Specifies a custom relationship between Sales[DateID] and


CustomCalendar[DateID].
 Result: Activates this relationship temporarily for the calculation.

Step 2: SUM(Sales[SalesAmount])

 Filter Context: Operates within the context of the Sales table, using the
relationship defined by USERELATIONSHIP.
 Sum Calculation:
o Sums the SalesAmount for all rows in the Sales table:
 100 + 200 + 300 + 150 + 250 = 1000

Step 3: CALCULATE

 Changes Filter Context: Applies the custom relationship to modify the filter
context.
 Result: Evaluates SUM(Sales[SalesAmount]) within this new filter context, giving a
result of 1000 for TotalSalesWithCustomRelationship.

Breakdown

 Initial State: There might be an existing relationship in the model (e.g.,


Sales[DateID] related to a primary Date table).
 USERELATIONSHIP Function: Temporarily activates a custom relationship
between Sales[DateID] and CustomCalendar[DateID].
 SUM Function: Sums the SalesAmount for all rows in the Sales table using the
custom relationship.
 CALCULATE Function: Modifies the filter context to use the custom relationship,
resulting in a total sales amount of 1000.

This breakdown demonstrates how each function interacts with contexts (relationship
and filter) to calculate the total sales amount using a custom relationship between the
Sales and CustomCalendar tables.

4o
similarly this "• LastYearSales = CALCULATE(SUM(Sales[SalesAmount]),
DATEADD('Date'[Date], -1, YEAR)) "
ChatGPT

Sure, I'll explain the DAX formula LastYearSales = CALCULATE(SUM(Sales[SalesAmount]),


DATEADD('Date'[Date], -1, YEAR)) step-by-step with contexts and a visual example.

Step-by-Step Explanation with Contexts

1. DATEADD Function:

 Time Intelligence Context: DATEADD('Date'[Date], -1, YEAR) shifts the dates in the
Date table back by one year.
 Result: It returns a modified table of dates that represent the same period in the
previous year.

2. SUM Function:

 Filter Context: The SUM function operates within the filter context provided by
CALCULATE. It sums the SalesAmount column in the Sales table.
 No Row Context: SUM does not create a row context itself; it works on the entire
column based on the filter context applied.

3. CALCULATE Function:

 Changes Filter Context: CALCULATE(SUM(Sales[SalesAmount]), DATEADD('Date'[Date], -1,


YEAR)) modifies the filter context to include only the dates from the previous year.
 Applies New Filter Context: It evaluates SUM(Sales[SalesAmount]) within this new
filter context, summing up the SalesAmount values corresponding to the same
period in the previous year.

Visual Example with Contexts

Imagine your Sales table looks like this:

Date SalesAmount
2023-01-01 100
2023-01-02 200
2023-01-03 300
2022-01-01 150
2022-01-02 250
2022-01-03 350

And your Date table looks like this:

Date
2023-01-01
2023-01-02
2023-01-03
2022-01-01
2022-01-02
2022-01-03
Let's assume that we are analyzing data for January 2023, and we want to compare it
with January 2022.

Step 1: DATEADD('Date'[Date], -1, YEAR)

 Time Intelligence Context: Shifts the dates in the Date table back by one year.
 Result: The modified table of dates representing January 2022 (2022-01-01,
2022-01-02, 2022-01-03).

Step 2: SUM(Sales[SalesAmount])

 Filter Context: Operates within the context of the Sales table, using the dates
from the previous year.
 Sum Calculation:
o Sums the SalesAmount for the corresponding dates in January 2022:
 150 + 250 + 350 = 750

Step 3: CALCULATE

 Changes Filter Context: Applies the dates from the previous year to modify the
filter context.
 Result: Evaluates SUM(Sales[SalesAmount]) within this new filter context, giving a
result of 750 for LastYearSales.

Breakdown

 Initial State: We are analyzing sales for January 2023.


 DATEADD Function: Shifts the dates back by one year to get the corresponding
period in January 2022.
 SUM Function: Sums the SalesAmount for all rows in the Sales table that match the
previous year's dates.
 CALCULATE Function: Modifies the filter context to use the previous year's
dates, resulting in a total sales amount of 750 for January 2022.

This breakdown demonstrates how each function interacts with contexts (time
intelligence and filter) to calculate the total sales amount for the same period in the
previous year.

4o
similarly this "RollingAverage3Months = CALCULATE(AVERAGE(Sales[SalesAmount]),
DATESINPERIOD('Date'[Date], MAX('Date'[Date]), -3, MONTH)) "
ChatGPT

Sure, I'll explain the DAX formula RollingAverage3Months =


CALCULATE(AVERAGE(Sales[SalesAmount]), DATESINPERIOD('Date'[Date], MAX('Date'[Date]), -3,
MONTH)) step-by-step with contexts and a visual example.

Step-by-Step Explanation with Contexts

1. MAX Function:

 Row Context: MAX('Date'[Date]) retrieves the latest date in the current context (e.g.,
the most recent date in the visible data in a report).
 Result: It returns the maximum date, which will be used as the end date for the
period calculation.

2. DATESINPERIOD Function:

 Time Intelligence Context: DATESINPERIOD('Date'[Date], MAX('Date'[Date]), -3, MONTH)


generates a table of dates starting from the MAX('Date'[Date]) and going back 3
months.
 Result: It returns a set of dates representing the period of the last 3 months from
the maximum date.

3. AVERAGE Function:

 Filter Context: The AVERAGE function operates within the filter context provided
by CALCULATE. It calculates the average of the SalesAmount column in the Sales table.
 No Row Context: AVERAGE does not create a row context itself; it works on the
entire column based on the filter context applied.

4. CALCULATE Function:

 Changes Filter Context: CALCULATE(AVERAGE(Sales[SalesAmount]),


DATESINPERIOD('Date'[Date], MAX('Date'[Date]), -3, MONTH)) modifies the filter context to
include only the dates from the last 3 months.
 Applies New Filter Context: It evaluates AVERAGE(Sales[SalesAmount]) within this
new filter context, calculating the average SalesAmount over the specified 3-month
period.

Visual Example with Contexts

Imagine your Sales table looks like this:


Date SalesAmount
2023-04-01 100
2023-03-15 200
2023-03-01 300
2023-02-15 150
2023-02-01 250
2023-01-15 350
2023-01-01 400

And your Date table looks like this:

Date
2023-04-01
2023-03-15
2023-03-01
2023-02-15
2023-02-01
2023-01-15
2023-01-01

Let's assume we are analyzing data up to April 1, 2023.

Step 1: MAX('Date'[Date])

 Row Context: Retrieves the latest date in the current context, which is April 1,
2023.
 Result: 2023-04-01.

Step 2: DATESINPERIOD('Date'[Date], MAX('Date'[Date]), -3, MONTH)

 Time Intelligence Context: Generates a table of dates for the last 3 months
starting from April 1, 2023.
 Result: The dates table will include dates from January 1, 2023, to April 1, 2023.

Step 3: AVERAGE(Sales[SalesAmount])

 Filter Context: Operates within the context of the dates from January 1, 2023, to
April 1, 2023.
 Average Calculation:
o Average of sales amounts for the period:
 (100 + 200 + 300 + 150 + 250 + 350 + 400) / 7 = 250

Step 4: CALCULATE

 Changes Filter Context: Applies the dates from the last 3 months to modify the
filter context.
 Result: Evaluates AVERAGE(Sales[SalesAmount]) within this new filter context, giving a
result of 250 for RollingAverage3Months.

Breakdown

 Initial State: We are analyzing sales up to April 1, 2023.


 MAX Function: Retrieves the maximum date in the current context, which is April
1, 2023.
 DATESINPERIOD Function: Generates dates from January 1, 2023, to April 1,
2023.
 AVERAGE Function: Calculates the average sales amount for the specified
period.
 CALCULATE Function: Modifies the filter context to use the dates from the last 3
months, resulting in an average sales amount of 250.

This breakdown demonstrates how each function interacts with contexts (time
intelligence and filter) to calculate the rolling average sales amount for the last 3
months.

4o
on the basis of above formulas can you give me the standard syntax, so that i can easily
create any DAX formula.
ChatGPT

Certainly! Understanding the structure and common components of DAX formulas can
help you create new ones more easily. Here is a standard syntax and explanation for
creating DAX formulas:

Standard Syntax for DAX Formulas

DAX
Copy code
ResultColumnName = CALCULATE(
[AggregationFunction](Table[Column]),
[FilterContextModifiers]
)
Explanation of Components

1. ResultColumnName: The name of the new calculated column or measure.


2. CALCULATE: A function that modifies the filter context for calculations.
3. AggregationFunction: An aggregation function like SUM, AVERAGE, MIN, MAX,
etc.
4. Table[Column]: The table and column on which the aggregation function
operates.
5. FilterContextModifiers: One or more functions that modify the filter context.
These can include functions like FILTER, ALL, USERELATIONSHIP, DATESINPERIOD,
DATEADD, etc.

Examples and Templates

1. Simple Aggregation with Filter

DAX
Copy code
FilteredSum = CALCULATE(
SUM(Table[Column]),
Table[Column2] = "Value"
)

2. Ignoring a Specific Filter

DAX
Copy code
TotalIgnoringSpecificFilter = CALCULATE(
SUM(Table[Column]),
ALL(Table[Column2])
)

3. Using a Custom Relationship

DAX
Copy code
SumWithCustomRelationship = CALCULATE(
SUM(Table[Column]),
USERELATIONSHIP(Table1[Column], Table2[Column])
)

4. Time Intelligence (e.g., Year-over-Year Calculation)

DAX
Copy code
LastYearValue = CALCULATE(
SUM(Table[Column]),
DATEADD('Date'[Date], -1, YEAR)
)

5. Rolling Average

DAX
Copy code
RollingAverage = CALCULATE(
AVERAGE(Table[Column]),
DATESINPERIOD('Date'[Date], MAX('Date'[Date]), -3, MONTH)
)

Detailed Templates

1. Standard Aggregation

DAX
Copy code
TotalSales = CALCULATE(
SUM(Sales[SalesAmount])
)

2. Filtered Aggregation

DAX
Copy code
CategorySales = CALCULATE(
SUM(Sales[SalesAmount]),
Sales[Category] = "Electronics"
)

3. Ignoring a Specific Filter

DAX
Copy code
TotalSalesIgnoreCustomerFilter = CALCULATE(
SUM(Sales[SalesAmount]),
ALL(Customer)
)

4. Using Custom Relationships

DAX
Copy code
TotalSalesWithCustomRelationship = CALCULATE(
SUM(Sales[SalesAmount]),
USERELATIONSHIP(Sales[DateID], 'CustomCalendar'[DateID])
)

5. Time Intelligence Calculation

DAX
Copy code
LastYearSales = CALCULATE(
SUM(Sales[SalesAmount]),
DATEADD('Date'[Date], -1, YEAR)
)

6. Rolling Calculation

DAX
Copy code
RollingAverage3Months = CALCULATE(
AVERAGE(Sales[SalesAmount]),
DATESINPERIOD('Date'[Date], MAX('Date'[Date]), -3, MONTH)
)

Creating a New DAX Formula

When creating a new DAX formula, follow these steps:

1. Identify the Result: Determine what you want to calculate (e.g., total sales,
average sales, sales last year, etc.).
2. Choose the Aggregation Function: Select an appropriate aggregation function
(SUM, AVERAGE, MIN, MAX).
3. Specify the Table and Column: Identify the table and column you want to
aggregate.
4. Modify the Filter Context: Decide if you need to modify the filter context using
functions like FILTER, ALL, USERELATIONSHIP, DATEADD, DATESINPERIOD, etc.
5. Construct the Formula: Combine these components using the CALCULATE
function.

By following this standard syntax and process, you can create a wide variety of DAX
formulas for different analytical needs.

4o
can you taught me more simpler that how to write any DAX formula.
ChatGPT

Of course! Writing DAX formulas can be made simpler by breaking down the process
into clear, easy-to-follow steps. Let's go through a simplified method for creating DAX
formulas.

Simplified Steps to Write a DAX Formula

1. Define What You Want to Calculate


o Decide on the measure or calculation you need (e.g., total sales, average
sales, sales for a specific category).
2. Choose the Aggregation Function
o Pick an aggregation function based on what you want to calculate:
 SUM for totals.
 AVERAGE for averages.
 COUNT for counts.
 MIN for minimum values.
 MAX for maximum values.
3. Identify the Table and Column
o Specify the table and column on which the aggregation function will
operate (e.g., Sales[SalesAmount]).
4. Apply Any Filters or Context Modifiers
o Use functions to modify the filter context if needed:
 FILTER to filter rows.
 ALL to remove filters.
 USERELATIONSHIP to use a different relationship.
 DATESINPERIOD or DATEADD for time-based calculations.
5. Combine Using CALCULATE
o Use the CALCULATE function to combine the aggregation function with any
filters or context modifiers.

Basic Formula Template

DAX
Copy code
ResultName = CALCULATE(
[AggregationFunction](Table[Column]),
[FilterContextModifiers]
)

Examples with Simplified Explanations


1. Total Sales

DAX
Copy code
TotalSales = SUM(Sales[SalesAmount])

Explanation: This simply sums up the SalesAmount column in the Sales table.

2. Filtered Total Sales for Electronics

DAX
Copy code
ElectronicsSales = CALCULATE(
SUM(Sales[SalesAmount]),
Sales[Category] = "Electronics"
)

Explanation: This calculates the total sales amount but only for rows where the Category
is "Electronics".

3. Total Sales Ignoring Customer Filters

DAX
Copy code
TotalSalesIgnoreCustomerFilter = CALCULATE(
SUM(Sales[SalesAmount]),
ALL(Customer)
)

Explanation: This calculates the total sales amount, ignoring any filters on the Customer
table.

4. Total Sales Using a Custom Relationship

DAX
Copy code
TotalSalesWithCustomRelationship = CALCULATE(
SUM(Sales[SalesAmount]),
USERELATIONSHIP(Sales[DateID], CustomCalendar[DateID])
)

Explanation: This calculates the total sales amount using a custom relationship between
Sales[DateID] and CustomCalendar[DateID].

5. Sales from the Previous Year


DAX
Copy code
LastYearSales = CALCULATE(
SUM(Sales[SalesAmount]),
DATEADD(Date[Date], -1, YEAR)
)

Explanation: This calculates the total sales amount for the same period in the previous
year.

6. Rolling 3-Month Average Sales

DAX
Copy code
RollingAverage3Months = CALCULATE(
AVERAGE(Sales[SalesAmount]),
DATESINPERIOD(Date[Date], MAX(Date[Date]), -3, MONTH)
)

Explanation: This calculates the average sales amount over the last 3 months, based on
the latest date in the Date table.

Practical Steps to Create Your Own DAX Formula

1. State Your Objective: Clearly define what you want to calculate (e.g., "I want to
find the total sales for the last quarter").
2. Choose the Aggregation: Determine the aggregation function you need (e.g.,
SUM for total sales).
3. Select the Column: Identify the table and column to aggregate (e.g.,
Sales[SalesAmount]).
4. Apply Necessary Filters: Decide if you need any filters or context changes (e.g.,
time filters, category filters).
5. Construct the Formula:
o Start with CALCULATE if you need to modify the filter context.
o Add the aggregation function.
o Include any filter context modifiers inside CALCULATE.

Example Walkthrough

Objective: Calculate the total sales for the last 6 months.

1. Aggregation Function: SUM


2. Table and Column: Sales[SalesAmount]
3. Filter Context Modifier: Last 6 months using DATESINPERIOD

DAX Formula:

DAX
Copy code
TotalSalesLast6Months = CALCULATE(
SUM(Sales[SalesAmount]),
DATESINPERIOD(Date[Date], MAX(Date[Date]), -6, MONTH)
)

Key Points to Remember

 Start Simple: Begin with basic calculations and gradually add complexity.
 Use CALCULATE: This function is key to modifying filter contexts.
 Aggregation First: Always define what you want to calculate first (SUM,
AVERAGE, etc.).
 Context Modifiers: Apply filters or relationships only if necessary.

By following these simplified steps and examples, you can create a wide range of DAX
formulas to suit your analytical needs.

4o
why we use CALCULATE function in every formula .
ChatGPT

The CALCULATE function is a powerful and versatile function in DAX that allows you to
modify the filter context in which a calculation is performed. Here’s why it is frequently
used in many DAX formulas:

Key Reasons to Use CALCULATE

1. Modifying Filter Context:


o CALCULATE can change the filter context by applying additional filters or
removing existing ones. This is essential when you want to perform
calculations under specific conditions that are different from the default
context.
2. Performing Context Transitions:
o CALCULATE can transition the row context into a filter context. This is
particularly useful in complex calculations where you need to aggregate
data based on row-level conditions.
3. Time Intelligence Calculations:
Many time-based calculations, such as year-over-year growth or rolling
o
averages, require changing the filter context to different periods.
CALCULATE helps in applying these dynamic time-based filters.
4. Complex Filtering:
o CALCULATE can handle complex filters that involve multiple conditions,
logical operations, or relationships between tables. This is critical for
advanced analytical scenarios.

How CALCULATE Works

 Filter Context: The set of filters that define which rows of data are included in
the calculation.
 Row Context: The context in which a formula is evaluated for each row in a table.

CALCULATE can take multiple filter arguments, each of which modifies the filter context
for the calculation it wraps.

Basic Syntax

DAX
Copy code
CALCULATE(
<Expression>,
<Filter1>,
<Filter2>,
...
)

 Expression: The calculation to perform (e.g., SUM(Sales[SalesAmount])).


 Filters: One or more filters that modify the filter context (e.g., Sales[Category] =
"Electronics").

Examples and Explanations

1. Basic Total Calculation

DAX
Copy code
TotalSales = SUM(Sales[SalesAmount])

Explanation: Calculates the total sales amount without modifying the filter context.

2. Filtered Calculation
DAX
Copy code
ElectronicsSales = CALCULATE(
SUM(Sales[SalesAmount]),
Sales[Category] = "Electronics"
)

Explanation: Uses CALCULATE to modify the filter context to include only rows where the
category is "Electronics".

3. Ignoring a Specific Filter

DAX
Copy code
TotalSalesIgnoreCustomerFilter = CALCULATE(
SUM(Sales[SalesAmount]),
ALL(Customer)
)

Explanation: Uses CALCULATE to ignore any filters applied to the Customer table.

4. Using Custom Relationships

DAX
Copy code
TotalSalesWithCustomRelationship = CALCULATE(
SUM(Sales[SalesAmount]),
USERELATIONSHIP(Sales[DateID], CustomCalendar[DateID])
)

Explanation: Uses CALCULATE to activate a custom relationship for the calculation.

5. Time Intelligence Calculation

DAX
Copy code
LastYearSales = CALCULATE(
SUM(Sales[SalesAmount]),
DATEADD(Date[Date], -1, YEAR)
)

Explanation: Uses CALCULATE to modify the filter context to include dates from the same
period in the previous year.

6. Rolling Average Calculation


DAX
Copy code
RollingAverage3Months = CALCULATE(
AVERAGE(Sales[SalesAmount]),
DATESINPERIOD(Date[Date], MAX(Date[Date]), -3, MONTH)
)

Explanation: Uses CALCULATE to change the filter context to the last 3 months.

When CALCULATE is Not Needed

For simple aggregations where no filter context modification is required, CALCULATE is


not necessary. For example:

DAX
Copy code
TotalSales = SUM(Sales[SalesAmount])

Explanation: This formula does not need CALCULATE because it does not alter the filter
context.

Summary

CALCULATE is essential in DAX for:

 Changing the filter context dynamically.


 Applying complex filters and conditions.
 Enabling time intelligence and advanced calculations.
 Performing context transitions for more accurate calculations.

You might also like