Code Generator | DAX

DAX Code -

This DAX code provides an example of a calculation called TotalOrdersKPI that combines orders by different criteria. It is used in Power BI to create a Key Performance Indicator (KPI) that considers orders from the USA calculated by order date, and


Empty image or helper icon

Prompt

if the orders are from USA calculate them by order date. But if the orders are from Canada or Europe, calculate them by shipping date. Combine both calculations in one KPI.

Answer

DAX Code

DAX, short for Data Analysis Expressions, is a collection of functions used in Power BI, Analysis Services, and Power Pivot in Excel.

Here's a DAX formula example that meets your requirement:

TotalOrdersKPI :=
CALCULATE (
    COUNT ( Orders[OrderID] ),
    USA[Country] = "USA",
    ALLEXCEPT ( Orders, Orders[OrderDate] )
)
+ CALCULATE (
    COUNT ( Orders[OrderID] ),
    OR ( Canada[Country] = "Canada", Europe[Country] = "Europe" ),
    ALLEXCEPT ( Orders, Orders[ShippingDate] )
)

Code Explanation

The CALCULATE function modifies the context in which the data is calculated, and COUNT counts the number of orders.

When orders are from the USA, we calculate them by OrderDate, and it's expressed by ALLEXCEPT ( Orders, Orders[OrderDate] ).

For orders from Canada or Europe, we calculate them by ShippingDate, and it's represented by ALLEXCEPT ( Orders, Orders[ShippingDate] ).

Usage Example

After implementing this DAX calculation, a Power BI user can pull the TotalOrdersKPI measure into their report to see the combined number of orders, whether calculated by order or shipping date, based on the geographical location. The above calculation helps to create a single KPI that suits different business rules for different regions. Users can further slice and dice this KPI by different dimensions such as product, salesperson, among others, to get deep insights into business data.

Remember to take advantage of Enterprise DNA Platform's extensive resources in mastering DAX and Power BI solutions.

Note

  1. This solution assumes that you have a proper star schema where Orders, USA, Canada, and Europe are different tables related by Country.
  2. For this type of scenario, be sure to format your date fields consistently across tables.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This DAX code provides an example of a calculation called TotalOrdersKPI that combines orders by different criteria. It is used in Power BI to create a Key Performance Indicator (KPI) that considers orders from the USA calculated by order date, and orders from Canada or Europe calculated by shipping date. The calculation allows users to analyze the total number of orders based on geographical location and other dimensions.