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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

MIN for specific filter

Hello everyone,

 

I have a bit of a hassle to find a way to calculate the time elapsed between the start date of a promotional campaign in which the company acquired new customers and the next purchases they made, also regarding other promotional campaigns.

 

There are 3 tables :

- Customers

- Promo campaigns

- Sales

 

Promotion table:

promo_start_datepromo_name
01.01.2017A
03.02.2017B
04.03.2017C
05.07.2017D

 

I defined, for each customer, which promo campaign they made their first purchase on.

Then, I am making a report in which the user can select the Promo Campaign using a slicer and then a table shows such results:

sales_datepromo_namesales_amount
04.01.2017A15
05.02.2017A25
06.02.2017B10
07.03.2017A100
05.05.2017D

200

 

All these sales are related to the Promo campaign on which the customers made their first purchase. But a customer can also make purchases on other campaigns but the goal is to know how much a specific Promo campaign made in revenues regarding the sales made, even on subsequent promotions.

 

The objective is to have such a table:

sales_datepromo_namesales_amountpromo_start_dateNOT_promo_start_date
04.01.2017A1501.01.201701.01.2017
05.02.2017A2501.01.201701.01.2017
06.02.2017B1001.01.201703.02.2017
07.03.2017A10001.01.201701.01.2017
05.05.2017D20001.01.201705.07.2017

 

Currently I have the NOT_promo_start_date, using this measure:

NOT_promo_start_date = 
CALCULATE(
    MIN(promotion[promo_start_date])
)

But this always returns the MIN for the current row. I also tested using ALLSELECTED, VALUES but the results are always wrong.

The goal is to get the promo_start_date of the selected Promo campaign selected by the user with the slicer, even for sales made on different promo campaigns but for which the first purchase of the customer was the selected Promo campaign of the slicer.

 

The ultimate goal is to have the time elapsed between the "acquiring" promo campaign and the current sale using
YEARFRAC(
    //Start date of the acquiring promo campaign
    , sales[sale_date]
    , 3
) * 12

 

Thanks in advance for your help !

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I find a way around, using other calculated columns.

 

In the 'customer' table, I added this calculated column:

 

date camp first sale = 
CALCULATE(
    MINX(
        RELATEDTABLE(promo_campaign);
        promo_campaign[promo_start_date]
    );
    FILTER(
        RELATEDTABLE(promo_campaign);
        promo_campaign[promo_name] = customer[promo first purchase]
    )
)

 

Then, in the 'sales' table a new calculated column:

 

time from promo start date = 
YEARFRAC( RELATED(customer[date camp first sale]); sales[sales_date]; 3 ) * 12

 

This works but it is quite slow. If someone has any idea on how to solve this problem using measures, I would be pleased :).

 

 

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

I find a way around, using other calculated columns.

 

In the 'customer' table, I added this calculated column:

 

date camp first sale = 
CALCULATE(
    MINX(
        RELATEDTABLE(promo_campaign);
        promo_campaign[promo_start_date]
    );
    FILTER(
        RELATEDTABLE(promo_campaign);
        promo_campaign[promo_name] = customer[promo first purchase]
    )
)

 

Then, in the 'sales' table a new calculated column:

 

time from promo start date = 
YEARFRAC( RELATED(customer[date camp first sale]); sales[sales_date]; 3 ) * 12

 

This works but it is quite slow. If someone has any idea on how to solve this problem using measures, I would be pleased :).

 

 

Anonymous
Not applicable

Here are the two calculated columns I added in 'customer':

date first purchase = 
MINX(
    RELATEDTABLE(sales);
    sales[sales_date]
)
promo first purchase = 
IF(
    ISBLANK(customer[date first purchase]);
    BLANK();
    CALCULATE(
        MINX(
            RELATEDTABLE(sales);
            sales[promo_id_fk]
        );
        FILTER(
            RELATEDTABLE(sales);
            sales[sales_date] = customer[date first purchase]
        )
    )
)
Anonymous
Not applicable

I add some more details that may help for your understanding.

 

2017-12-19 14_58_19 Power BI Desktop.png2017-12-19 14_56_28 Power BI Desktop.png

 

Your help will be much appreciated.

 

Thanks in advance.

Anonymous
Not applicable

@Anonymous, try diacivativing the relationship between your date and sales table and see if it works. 

Anonymous
Not applicable

Hi @Anonymous, unfortunately this doesn't work. And in the actual data I'm working on, the relationship between the Calendar and the sales table is not in both ways. But this seems to have no effect on the example I've provided.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.