Professional Documents
Culture Documents
Understand How To Write Any DAX Formula
Understand How To Write Any DAX Formula
Understand How To Write Any DAX Formula
1. FILTER Function:
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:
RegionID RegionName
1 North America
2 Europe
3 Asia
Row Context: Evaluates each row to include only "Electronics" and where the
related RegionName is "North America".
Filtered Table:
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
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:
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')
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
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:
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')
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
1. USERELATIONSHIP Function:
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.
DateID SalesAmount
20230101 100
20230102 200
20230103 300
20230201 150
20230202 250
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 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
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
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:
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
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.
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
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
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:
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:
Date
2023-04-01
2023-03-15
2023-03-01
2023-02-15
2023-02-01
2023-01-15
2023-01-01
Step 1: MAX('Date'[Date])
Row Context: Retrieves the latest date in the current context, which is April 1,
2023.
Result: 2023-04-01.
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
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:
DAX
Copy code
ResultColumnName = CALCULATE(
[AggregationFunction](Table[Column]),
[FilterContextModifiers]
)
Explanation of Components
DAX
Copy code
FilteredSum = CALCULATE(
SUM(Table[Column]),
Table[Column2] = "Value"
)
DAX
Copy code
TotalIgnoringSpecificFilter = CALCULATE(
SUM(Table[Column]),
ALL(Table[Column2])
)
DAX
Copy code
SumWithCustomRelationship = CALCULATE(
SUM(Table[Column]),
USERELATIONSHIP(Table1[Column], Table2[Column])
)
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"
)
DAX
Copy code
TotalSalesIgnoreCustomerFilter = CALCULATE(
SUM(Sales[SalesAmount]),
ALL(Customer)
)
DAX
Copy code
TotalSalesWithCustomRelationship = CALCULATE(
SUM(Sales[SalesAmount]),
USERELATIONSHIP(Sales[DateID], 'CustomCalendar'[DateID])
)
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)
)
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.
DAX
Copy code
ResultName = CALCULATE(
[AggregationFunction](Table[Column]),
[FilterContextModifiers]
)
DAX
Copy code
TotalSales = SUM(Sales[SalesAmount])
Explanation: This simply sums up the SalesAmount column in the Sales table.
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".
DAX
Copy code
TotalSalesIgnoreCustomerFilter = CALCULATE(
SUM(Sales[SalesAmount]),
ALL(Customer)
)
Explanation: This calculates the total sales amount, ignoring any filters on the Customer
table.
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].
Explanation: This calculates the total sales amount for the same period in the previous
year.
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.
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
DAX Formula:
DAX
Copy code
TotalSalesLast6Months = CALCULATE(
SUM(Sales[SalesAmount]),
DATESINPERIOD(Date[Date], MAX(Date[Date]), -6, MONTH)
)
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:
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>,
...
)
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".
DAX
Copy code
TotalSalesIgnoreCustomerFilter = CALCULATE(
SUM(Sales[SalesAmount]),
ALL(Customer)
)
Explanation: Uses CALCULATE to ignore any filters applied to the Customer table.
DAX
Copy code
TotalSalesWithCustomRelationship = CALCULATE(
SUM(Sales[SalesAmount]),
USERELATIONSHIP(Sales[DateID], CustomCalendar[DateID])
)
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.
Explanation: Uses CALCULATE to change the filter context to the last 3 months.
DAX
Copy code
TotalSales = SUM(Sales[SalesAmount])
Explanation: This formula does not need CALCULATE because it does not alter the filter
context.
Summary