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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
Anonymous
Not applicable

Help with solution for calculation based previous order

Hi!

 

I really hope someone has a quick and good solution for my issue. 

What I need to do is to calculate how many order_type "order" that are generated after the customer placed the order_type "ht_order". Moreover, the "order" should be placed within 30 days after the "ht_order" was placed. 

 

For example, customer 123 placed a "ht_order" 01-01-20 and then another "order" 20-01-20 (19 days later) so this should be counted. 

 

Expected outcome from below tables are 6 orders and sales of 830. 

 

Example data: 

Sales Table 

customer_keyorder_numberorder_typesalesorder_date
1231001ht_order1001-jan-20
1231020order15020-jan-20
1241100ht_order1010-okt-19
1241200order14010-dec-19
1251201ht_order1011-jan-20
1251202order13015-jan-20
1261205order11002-feb-20
1271206ht_order1010-mar-20
1281207ht_order1017-maj-20
1291208ht_order1010-jun-19
1291209order18022-jun-19
1301210ht_order1004-jun-20
1301211order12013-jun-20
1301212order15013-sep-20
1311213ht_order1015-mar-20
1311005order13015-maj-20
1321215ht_order1001-aug-20
1321220order12005-aug-20
1321225order13008-aug-20

 

Customer Table

CUSTOMER_KEYCUSTOMER_EMAIL
123123@gmail.com
124124@gmail.com
125125@gmail.com
126126@gmail.com
127127@gmail.com
128128@gmail.com
129129@gmail.com
130130@gmail.com
131131@gmail.com
132132@gmail.com

 

Big thanks in advance!

 

1 ACCEPTED SOLUTION

@Anonymous,

 

Try this. I revised the logic to work at both the total level and the year/month level.

 

Count of Order = 
SUMX (
    ALL ( Customer ),
    VAR vCustomer = Customer[customer_key]
    VAR vHTOrderRow =
        FILTER (
            ALL ( Sales ),
            Sales[customer_key] = vCustomer
                && Sales[order_type] = "ht_order"
        )
    VAR vHTOrderDate =
        MAXX ( vHTOrderRow, Sales[date_key] )
    VAR vCountOrderRows =
        CALCULATE (
            COUNTROWS ( Sales ),
            Sales[customer_key] = vCustomer,
            Sales[order_type] = "order",
            Sales[date_key] <= vHTOrderDate + 30
        )
    RETURN
        vCountOrderRows
)

 

 

DataInsights_0-1603990745921.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

9 REPLIES 9
DataInsights
Super User
Super User

@Anonymous,

 

The measure below assumes one "ht_order" per customer (per the sample data). If this is not the case, let me know.

 

Count of Order = 
SUMX (
    ALL ( Customer ),
    VAR vCustomer = Customer[customer_key]
    VAR vHTOrderRow =
        FILTER (
            ALL ( Sales ),
            Sales[customer_key] = vCustomer
                && Sales[order_type] = "ht_order"
        )
    VAR vHTOrderDate =
        MAXX ( vHTOrderRow, Sales[order_date] )
    VAR vOrderRows =
        FILTER (
            ALL ( Sales ),
            Sales[customer_key] = vCustomer
                && Sales[order_type] = "order"
                && Sales[order_date] <= vHTOrderDate + 30
        )
    RETURN
        COUNTROWS ( vOrderRows )
)

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Thanks for the solution and great reply!

@DataInsightswould it be possible to tweak the formula to be based on 3 tables instead of only 2 (realized that I have the date in a separately table). In that case, this is the example data: 

Sales table

customer_keydate_keyorder_numberorder_typesales
123202001011001ht_order10
123202001201020order150
124201910101100ht_order10
124201912101200order140
125202001111201ht_order10
125202001151202order130
126202002021205order110
127202003101206ht_order10
128202005171207ht_order10
129201906101208ht_order10
129201906221209order180
130202006041210ht_order10
130202006131211order120
130202009131212order150
131202003151213ht_order10
131202005151005order130
132202008011215ht_order10
132202008051220order120
132202008081225order130

 

Customer Table

CUSTOMER_KEYCUSTOMER_EMAIL
123123@gmail.com
124124@gmail.com
125125@gmail.com
126126@gmail.com
127127@gmail.com
128128@gmail.com
129129@gmail.com
130130@gmail.com
131131@gmail.com
132132@gmail.com

Date Table

date_keyfull_dateweekday_numberios_week
202001012020-01-0131
202001202020-01-2014
201910102019-10-10441
201912102019-12-10250
202001112020-01-1162
202001152020-01-1533
202002022020-02-0275
202003102020-03-10211
202005172020-05-17720
201906102019-06-10124
201906222019-06-22625
202006042020-06-04423
202006132020-06-13624
202009132020-09-13737
202003152020-03-15711
202005152020-05-15520
202008012020-08-01631
202008052020-08-05332
202008082020-08-08632

 

 

Big thanks in advance!

 

 

@Anonymous,

 

See the revised measure below. This requires a relationship between the Sales table and Date table.

 

Count of Order = 
SUMX (
    ALL ( Customer ),
    VAR vCustomer = Customer[customer_key]
    VAR vHTOrderRow =
        FILTER (
            ALL ( Sales ),
            Sales[customer_key] = vCustomer
                && Sales[order_type] = "ht_order"
        )
    VAR vHTOrderDate =
        MAXX ( vHTOrderRow, Sales[date_key] )
    VAR vOrderRows =
        FILTER (
            ALL ( Sales ),
            Sales[customer_key] = vCustomer
                && Sales[order_type] = "order"
                && Sales[date_key] <= vHTOrderDate + 30
        )
    RETURN
        COUNTROWS ( vOrderRows )
)

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

@DataInsights  - Unfortunately, getting still the same output..

@Anonymous,

 

Would you be able to attach screenshots of the data model, visual, and slicers?





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Hi!

I think it sholud be more correct to use ALL as filter, right? This since we would like to include order_type "ht_order" also outside selected period of time? 

Yes, I did a try on the example data so please have a look. 

ReportReportModelModel

 

 

 

 

 

 

 

Thanks for helping!

@DataInsights 

@Anonymous,

 

Try this. I revised the logic to work at both the total level and the year/month level.

 

Count of Order = 
SUMX (
    ALL ( Customer ),
    VAR vCustomer = Customer[customer_key]
    VAR vHTOrderRow =
        FILTER (
            ALL ( Sales ),
            Sales[customer_key] = vCustomer
                && Sales[order_type] = "ht_order"
        )
    VAR vHTOrderDate =
        MAXX ( vHTOrderRow, Sales[date_key] )
    VAR vCountOrderRows =
        CALCULATE (
            COUNTROWS ( Sales ),
            Sales[customer_key] = vCustomer,
            Sales[order_type] = "order",
            Sales[date_key] <= vHTOrderDate + 30
        )
    RETURN
        vCountOrderRows
)

 

 

DataInsights_0-1603990745921.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Hi again, 

@DataInsights I have two relationships in place (customer_key and date_key), both with cardinality "Many to one". 

When trying the formula below I get an error since it show me Count of Order = 6  - no matter what date I select. 

 

Any suggestions on whats being wrong?

 

Thanks!

 
 
 

 

 

@Anonymous,

 

Try this:

 

Count of Order = 
SUMX (
    ALL ( Customer ),
    VAR vCustomer = Customer[customer_key]
    VAR vHTOrderRow =
        FILTER (
            ALLSELECTED ( Sales ),
            Sales[customer_key] = vCustomer
                && Sales[order_type] = "ht_order"
        )
    VAR vHTOrderDate =
        MAXX ( vHTOrderRow, Sales[date_key] )
    VAR vOrderRows =
        FILTER (
            ALLSELECTED ( Sales ),
            Sales[customer_key] = vCustomer
                && Sales[order_type] = "order"
                && Sales[date_key] <= vHTOrderDate + 30
        )
    RETURN
        COUNTROWS ( vOrderRows )
)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.