Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

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:   UserID Creation User Date 1 01-05-15 2 02-06-15 ...
  • AlB's avatar
    7 years ago

    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