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

Calculate average orders customer first, second, third etc. month

The problem I have is that I want to calculated the amount of orders an user places per month since creation.

I have two tables User:

 

UserIDCreation User Date
101-05-15
202-06-15
303-07-15
404-08-15
505-09-15

Orders:

Order DateUserID
02-06-151
03-07-151
04-08-152
05-09-154
02-11-155

 

 

I wanted to COUNTROWS per user in the offset period since the account creation, but I can only find functions or patterns to do a fix period for ALL users.

Does someone know how to calculate per user the amount of orders in the first, second etc. month of it's lifetime?

Desired outcome would be something like.

Month since creationAverage amount orders
110
25
36
42
53
64




 

1 ACCEPTED SOLUTION
AlB
Super User
Super User

Hi @Anonymous 

Try this:

1. Create a 1-to-many unidirectional relationship between UserTable[UserID] and OrdersTable[UserID]

2. Create a one-column calculated table:

 

 

AuxTable = 
VAR _MaxDiff =
    MAXX (
        ADDCOLUMNS (
            UserTable;
            "MonthDiff"; DATEDIFF (
                UserTable[Creation User Date];
                CALCULATE ( MAX ( OrdersTable[Order Date] ) );
                MONTH
            )
        );
        [MonthDiff]
    )
RETURN
    SELECTCOLUMNS ( GENERATESERIES ( 1; _MaxDiff + 1); "MonthSinceCreation"; [Value] )

3. Place AuxTable[MonthSinceCreation] just created in the previous step in the rows of a matrix visual

 

4. Create this measure and place it in values of the matrix visual:

 

 

AverageAmountOrders = 
AVERAGEX (
    ALL ( UserTable);
    CALCULATE (
        COUNT ( OrdersTable[Order Date] );
        FILTER (
            CALCULATETABLE ( OrdersTable );
            DATEDIFF ( UserTable[Creation User Date]; OrdersTable[Order Date]; MONTH )
                <= (SELECTEDVALUE ( AuxTable[MonthSinceCreation] ) - 1)
        )
    )
) + 0

 

 

 

 

 

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

 

Please clarify how you arrived at the numbers in the average amount orders column?  How did you get 10,5 etc.  


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
AlB
Super User
Super User

Hi @Anonymous 

Try this:

1. Create a 1-to-many unidirectional relationship between UserTable[UserID] and OrdersTable[UserID]

2. Create a one-column calculated table:

 

 

AuxTable = 
VAR _MaxDiff =
    MAXX (
        ADDCOLUMNS (
            UserTable;
            "MonthDiff"; DATEDIFF (
                UserTable[Creation User Date];
                CALCULATE ( MAX ( OrdersTable[Order Date] ) );
                MONTH
            )
        );
        [MonthDiff]
    )
RETURN
    SELECTCOLUMNS ( GENERATESERIES ( 1; _MaxDiff + 1); "MonthSinceCreation"; [Value] )

3. Place AuxTable[MonthSinceCreation] just created in the previous step in the rows of a matrix visual

 

4. Create this measure and place it in values of the matrix visual:

 

 

AverageAmountOrders = 
AVERAGEX (
    ALL ( UserTable);
    CALCULATE (
        COUNT ( OrdersTable[Order Date] );
        FILTER (
            CALCULATETABLE ( OrdersTable );
            DATEDIFF ( UserTable[Creation User Date]; OrdersTable[Order Date]; MONTH )
                <= (SELECTEDVALUE ( AuxTable[MonthSinceCreation] ) - 1)
        )
    )
) + 0

 

 

 

 

 

Anonymous
Not applicable

Hi @AlB,

 

Thank you very much for helping out.

When I put these in a matrix I get only a total average. It seems something goes wrong with making the series.

Do you have further suggestions by any chance?

 

Thank you very much again for helping.

@Anonymous 

 

what do you mean by a total average? I would need a clear example based on sample data with the expected result to understand what you are trying to do

Anonymous
Not applicable

@AlB 

 

Example.png

This is the current result

 

 

Desired would be indeed something like:

 

Month since creationAverage amount orders
110
25
36
42
53
64

 

So the matrix like you suggested but then that you can see the trend that for example more people do purchases the first month and then it dies off or that it increases the first months and then dies off for example.

So maybe something goes wrong with calculating the DATEDIFF.

An extra related made sure the relationship was calculated.
Thank you!

Hi @Anonymous ,

 

Believe that the values presented on your matrix visual are not giving you the expected result because like @AlB  said you need to place the column AuxTable[MonthSinceCreation] on the rows of the matrix, this will give you the 1 , 2 ,3, 4 ...

 

Regards,

Mfelix


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



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.