How do I dynamically calculate the first and last transaction date for each customer

0 votes

How do I dynamically calculate the first and last transaction date for each customer?
I need a Power BI measure that returns the first and last transaction dates for each customer in a dynamic manner. The measure should update based on user-applied filters. What is the best approach using DAX functions like MIN, MAX, CALCULATE, and RELATEDTABLE to ensure accurate and responsive results?

Apr 1, 2025 in Power BI by Evanjalin
• 36,180 points
747 views

1 answer to this question.

0 votes

Calculation of the first transaction date and last transaction date per customer dynamically based on the report context requires the DAX functions MIN, MAX, CALCULATE, and RELATED TABLE. This ensures that the measure responds to the filters applied in the report.

Methodology: Using CALCULATE, MIN, and MAX

Create Measures for the First and Last Transaction Date:

DAX is used to create two measures for the first and last transaction dates:

First Transaction Date = 
CALCULATE(
    MIN(Sales[Transaction Date]),
    ALLEXCEPT(Sales, Sales[Customer ID])
)

Last Transaction Date = 
CALCULATE(
    MAX(Sales[Transaction Date]),
    ALLEXCEPT(Sales, Sales[Customer ID])
)

Explanation of the Measures:

  • CALCULATE: Changes the context to focus on a specific customer.

  • MIN and MAX: Find the first and last dates within the filtered context.

  • ALLEXCEPT: Preserves filtering on the Customer ID column while ignoring others.

  • The measures respond dynamically to filters like slicers or visual-level filters.

Alternative Approach: Using Variables for Clarity

For better readability, use variables in a single measure:

Transaction Date Range = 
VAR FirstDate = 
    CALCULATE(
        MIN(Sales[Transaction Date]),
        ALLEXCEPT(Sales, Sales[Customer ID])
    )

VAR LastDate = 
    CALCULATE(
        MAX(Sales[Transaction Date]),
        ALLEXCEPT(Sales, Sales[Customer ID])
    )

RETURN
    "First Transaction: " & FORMAT(FirstDate, "dd-mmm-yyyy") & 
    " | Last Transaction: " & FORMAT(LastDate, "dd-mmm-yyyy")
  • This measure returns a formatted string displaying both dates.

  • It remains dynamic, reflecting changes in filters.

Dynamic Filtering with Slicers (Advanced Option)

If your model involves multiple date tables or complex filtering, consider using USERELATIONSHIP or custom date tables to ensure accurate results.

answered Apr 1, 2025 by anonymous
• 36,180 points

Related Questions In Power BI

+1 vote
2 answers

What are the prerequisites for running R scripts in Power BI Desktop, and how do I troubleshoot script failures?

The tedious way and probable fixes are ...READ MORE

answered Jan 13, 2025 in Power BI by pooja
• 24,450 points
815 views
0 votes
1 answer

How to Calculate Total Sales for the Last 3 Months Dynamically in DAX?

To dynamically calculate total sales for the ...READ MORE

answered Mar 3, 2025 in Power BI by anonymous
• 36,180 points
805 views
0 votes
1 answer

How do I calculate the percentage contribution of each category while considering multiple slicers?

You can calculate the percentage contribution dynamically ...READ MORE

answered Mar 25, 2025 in Power BI by anonymous
• 36,180 points
874 views
0 votes
1 answer

How do I ensure the deployment pipeline in Power BI maintains versioning for each report?

1. Make use of OneLake/Workspace History (Limited) You ...READ MORE

answered Jun 3, 2025 in Power BI by anonymous
• 36,180 points
356 views
0 votes
1 answer

Displaying Table Schema using Power BI with Azure IoT Hub

Answering your first question, Event Hubs are ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,090 points
2,492 views
+1 vote
1 answer

Unable to install connector for Power Bi and PostgreSQL

I think the problem is not at ...READ MORE

answered Aug 22, 2018 in Power BI by nirvana
• 3,090 points
3,833 views
+2 votes
2 answers

Migrate power bi collection to power bi embedded

I agree with Kalgi, this method is ...READ MORE

answered Oct 11, 2018 in Power BI by Hannah
• 18,520 points
2,653 views
+1 vote
1 answer

Connect power bi desktop to dataset and create custom reports

Open power bi report nd sign in ...READ MORE

answered Oct 10, 2023 in Power BI by Monika kale

edited Mar 5, 2025 2,717 views
0 votes
1 answer

How do I calculate the average time between two events for each customer?

To calculate the average time between two ...READ MORE

answered Apr 1, 2025 in Power BI by anonymous
• 36,180 points
589 views
0 votes
1 answer

I want to calculate the distinct count of active customers but only for the last three completed months—how can I do this in DAX?

To calculate the distinct count of active ...READ MORE

answered Mar 10, 2025 in Power BI by anonymous
• 36,180 points
755 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP