Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
pgenero
Frequent Visitor

Average Days between 2 tables

Hi! I have a model with 4 tables: Dates | Accounts | Sales | Samples.
What I want to do is to calculate the difference of days between the first sale for each account and the first sample (only if the sample was delivered before the date of the first sale). Then I want to average those days for all the accounts that have made their first purchase in the month provided.
The final goal is to display something like this: a table with month year in one column, then another column with the average number of days between the sample date and the sale date.
I was able to create in memory a table using Summarize and /or Addcolumns, where I can get the first date from Sales and Samples, and then also the AVG of days. But I-m not able to display those days in the desired month.

 

This is my code:
MeasureName =

VAR _TABLE =
    SUMMARIZE(
        Account,
        Account[Customer_Number],
        "@firstSale",
        CALCULATE(
            FIRSTDATE(SALES[SalesDate]),
            ALL(SALES),
            VALUES(SALES[Customer_Number])
        ),
        "@firstSample",
        CALCULATE(
            FIRSTDATE(SAMPLES[SampleDate]),
            ALL(SAMPLES),
            VALUES(SAMPLES[Customer_Number])
        )
    )
 
VAR _result =
AVERAGEX(
   _TABLE,
   DATEDIFF([@firstSample], [@firstSale], DAY)
)
 
RETURN
_result

 

Currently this formula display the average (note that there is no restriction about the negative days, which is not my actual problem) in the month when the LAST sales happen. I need to display the number only in the month of the first sale.

pgenero_0-1705340493413.png

 

1 ACCEPTED SOLUTION
AmiraBedh
Resident Rockstar
Resident Rockstar

You current measure calculates the average difference in days between the first sample and the first sale for each customer,
but it does not restrict the calculation to the specific month of the first sale.

MeasureName =

VAR SelectedMonthYear = MAX(Dates[MonthYear]) // Assuming you have a MonthYear column in your Dates table

VAR _TABLE =
    SUMMARIZE(
        Account,
        Account[Customer_Number],
        "@firstSale",
        CALCULATE(
            FIRSTDATE(SALES[SalesDate]),
            ALL(SALES),
            VALUES(SALES[Customer_Number])
        ),
        "@firstSample",
        CALCULATE(
            FIRSTDATE(SAMPLES[SampleDate]),
            ALL(SAMPLES),
            VALUES(SAMPLES[Customer_Number])
        )
    )

VAR FilteredTable = 
    FILTER(
        _TABLE,
        MONTH([@firstSale]) = MONTH(SelectedMonthYear) && YEAR([@firstSale]) = YEAR(SelectedMonthYear)
        && [@firstSample] < [@firstSale]
    )

VAR _result =
    IF(
        ISEMPTY(FilteredTable),
        BLANK(),
        AVERAGEX(
            FilteredTable,
            DATEDIFF([@firstSample], [@firstSale], DAY)
        )
    )

RETURN
    _result

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

1 REPLY 1
AmiraBedh
Resident Rockstar
Resident Rockstar

You current measure calculates the average difference in days between the first sample and the first sale for each customer,
but it does not restrict the calculation to the specific month of the first sale.

MeasureName =

VAR SelectedMonthYear = MAX(Dates[MonthYear]) // Assuming you have a MonthYear column in your Dates table

VAR _TABLE =
    SUMMARIZE(
        Account,
        Account[Customer_Number],
        "@firstSale",
        CALCULATE(
            FIRSTDATE(SALES[SalesDate]),
            ALL(SALES),
            VALUES(SALES[Customer_Number])
        ),
        "@firstSample",
        CALCULATE(
            FIRSTDATE(SAMPLES[SampleDate]),
            ALL(SAMPLES),
            VALUES(SAMPLES[Customer_Number])
        )
    )

VAR FilteredTable = 
    FILTER(
        _TABLE,
        MONTH([@firstSale]) = MONTH(SelectedMonthYear) && YEAR([@firstSale]) = YEAR(SelectedMonthYear)
        && [@firstSample] < [@firstSale]
    )

VAR _result =
    IF(
        ISEMPTY(FilteredTable),
        BLANK(),
        AVERAGEX(
            FilteredTable,
            DATEDIFF([@firstSample], [@firstSale], DAY)
        )
    )

RETURN
    _result

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.