Power BI DAX: CALCULATE, DISTINCTCOUNT, LASTDATE

Mohamad MahmoodMohamad Mahmood
6 min read
CALCULATE ( DISTINCTCOUNT ( Balances[AccountID] ), LASTDATE ( 'Date'[Date] ) )

In data analysis, understanding the number of unique accounts at specific points in time is crucial for tracking performance and making informed decisions. The DAX expression CALCULATE(DISTINCTCOUNT(Balances[AccountID]), LASTDATE(Date[Date])) is a powerful tool for this purpose. It combines the CALCULATE function, which modifies the context of the calculation, with DISTINCTCOUNT, which counts unique accounts, and LASTDATE, which filters the data to the most recent date in the current context. This expression helps analysts determine the number of active accounts on the last date of a selected period, providing valuable insights into account activity trends.

Scenario

You are a data analyst at a financial institution, and you need to create a report that shows the number of unique active accounts at the end of each month. This will help you understand how many accounts are active at the end of each period and identify trends in account activity.

Example

Data Assume you have a Balances table with the following columns:

• AccountID: The unique identifier for each account.

• BalanceDate: The date when the balance was recorded.

• BalanceAmount: The balance amount for the account on that date.

Sample Data

AccountIDBalanceDateBalanceAmount
12023-08-315000
22023-08-313000
12023-09-305500
32023-09-302000
22023-09-303200

DAX Expression

To calculate the number of unique active accounts at the end of each month, you can use the following DAX expression:

Unique Accounts Last Date =
CALCULATE ( DISTINCTCOUNT ( Balances[AccountID] ), LASTDATE ( Date[Date] ) )

Explanation

• CALCULATE: Modifies the context of the calculation by applying the specified filters.

• DISTINCTCOUNT(Balances[AccountID]): Counts the number of unique AccountID values in the Balances table.

• LASTDATE(Date[Date]): Filters the data to include only the last date in the current context.

Result

When you apply this expression in your report, it will calculate the number of unique active accounts on the last date of each month. For example, for August 2023, the expression will count the unique accounts on 2023-08-31, resulting in 2 unique accounts. For September 2023, it will count the unique accounts on 2023-09-30, resulting in 3 unique accounts.


Exercise

Find the balance at the month level and year level using DAX expressions.

Monthly Balance

To find the balance at the end of each month, you can use the following DAX expression:

Monthly Balance =
CALCULATE (
    SUM ( Balances[BalanceAmount] ),
    LASTDATE (
        CALENDAR ( MIN ( Balances[BalanceDate] ), MAX ( Balances[BalanceDate] ) )
    )
)

Yearly Balance

To find the balance at the end of each year, you can use a similar approach but adjust the context to the year level:

Yearly Balance =
CALCULATE (
    SUM ( Balances[BalanceAmount] ),
    LASTDATE (
        DATEADD (
            CALENDAR ( MIN ( Balances[BalanceDate] ), MAX ( Balances[BalanceDate] ) ),
            0,
            YEAR
        )
    )
)

Example Data

Assume you have the following data in your Balances table:

AccountIDBalanceDateBalanceAmount
12023-08-315000
22023-08-313000
12023-09-305500
32023-09-302000
22023-09-303200
12023-12-316000
22023-12-313500
32023-12-312500

Result

• Monthly Balance:

  • For August 2023, the balance would be the sum of balances on 2023-08-31, which is 8000.

  • For September 2023, it would be the sum of balances on 2023-09-30, which is 10700.

• Yearly Balance:

  • For 2023, the balance would be the sum of balances on 2023-12-31, which is 12000.

These expressions help you aggregate balances at different time levels, providing insights into financial performance over time.

Further Observation

For the Monthly Balance, the context is implicitly set by the LASTDATE function within the CALCULATE function, which operates at the month level by default when used in a monthly context. Here's the expression again for clarity:

Monthly Balance =
CALCULATE (
    SUM ( Balances[BalanceAmount] ),
    LASTDATE ( Balances[BalanceDate] )
)

For the Yearly Balance, the context needs to be explicitly set to the year level. This is done using the YEAR function to ensure the calculation is performed for the entire year:

Yearly Balance =
CALCULATE (
    SUM ( Balances[BalanceAmount] ),
    LASTDATE ( Balances[BalanceDate] ),
    VALUES ( YEAR ( Balances[BalanceDate] ) )
)

In summary, the LASTDATE function adapts to the context it's used in, so for monthly calculations, it inherently works at the month level. For yearly calculations, we need to explicitly define the year context to ensure the calculation is accurate


Advanced Exercise

What if the data table consists of daily dates instead of month end dates?

If your data table consists of daily dates instead of month-end dates, you can still calculate the balance at the end of each month or year by using DAX expressions that aggregate the data accordingly.

Monthly Balance To find the balance at the end of each month, you can use the ENDOFMONTH function to get the last date of each month and then calculate the balance for that date.

Monthly Balance =
CALCULATE (
    SUM ( Balances[BalanceAmount] ),
    ENDOFMONTH ( Balances[BalanceDate] )
)

Yearly Balance Similarly, to find the balance at the end of each year, you can use the ENDOFYEAR function.

Yearly Balance =
CALCULATE (
    SUM ( Balances[BalanceAmount] ),
    ENDOFYEAR ( Balances[BalanceDate] )
)

Example Data

Assume you have the following data in your Balances table:

AccountIDBalanceDateBalanceAmount
12023-08-015000
22023-08-153000
12023-09-105500
32023-09-202000
22023-09-253200
12023-12-056000
22023-12-153500
32023-12-252500

Result

• Monthly Balance:

  • For August 2023, the balance would be the sum of balances on the last date of August, which is 8000.

  • For September 2023, it would be the sum of balances on the last date of September, which is 10700.

• Yearly Balance:

  • For 2023, the balance would be the sum of balances on the last date of December, which is 12000.

These expressions help you aggregate balances at different time levels, providing insights into financial performance over time.

0
Subscribe to my newsletter

Read articles from Mohamad Mahmood directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohamad Mahmood
Mohamad Mahmood

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He studies at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).