Forum Discussion

heathernicole's avatar
heathernicole
Icon for Continued Contributor rankContinued Contributor
8 years ago
Solved

TopN Customers Rolling 12 months

Ok - So here's what I'm trying to recreate in Power BI (currently an excel file):     Logically, I get what's occurring in this report - however it's escaping me as to HOW to actually pull thi...
  • mattbrice's avatar
    8 years ago

    This is an interesting problem so I gave it a try.  Let me know if it doesn't work for you as I haven't tested these as I don't really have a good sample data set to work off of.  You'll have to translate the column names I used for your model.

    Here is what I came up with for 4 measures assuming you have YearMonth on rows like you have in your result screenshots.

     

    Total Sales = SUM ( FactSales[SalesAmount] )

     Then for total sales last 12 Mo

     

    Total Sales Last 12MO =
    VAR _last12MO =
        DATESBETWEEN (
            DimDate[Datekey],
            NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( DimDate[Datekey] ) ) ),
            LASTDATE ( DimDate[Datekey] )
        )
    RETURN
        CALCULATE ( [Total Sales], _last12MO )

    then for rolling top 20 sales last 12 months:

     

    Total Sales Last 12 MO Rolling Top20 =
    VAR _last12MO =
        DATESBETWEEN (
            DimDate[Datekey],
            NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( DimDate[Datekey] ) ) ),
            LASTDATE ( DimDate[Datekey] )
        )
    VAR _Top20Last12MO =
        CALCULATE (
            SUMX (
                VALUES ( DimDate[YearMonth] ),
                CALCULATE (
                    [Total Sales],
                    TOPN ( 20, VALUES ( DimCustomer[CustomerKey] ), [Total Sales] )
                )
            ),
            _last12MO
        )
    RETURN
        _Top20Last12MO

    and finally the percentage is easy:

     

    Percentage of Sales = DIVIDE ( [Total Sales Last 12 MO Rolling Top20], [Total Sales Last 12MO] )

    Let me know if this worked.

  • heathernicole's avatar
    heathernicole
    8 years ago

    mattbrice - Hey -  I think this is right. :) I've modified a few things - but you gave me the core idea. THANK YOU!
    I'm getting ready to meet with our Sales Manager to verify the numbers but from what I've checked so far it seems accurate.

     

    THANKS SO MUCH! I'll post the final after I confirm. 

     

  • heathernicole's avatar
    heathernicole
    8 years ago

    Full answer with mattbrice base formulas from a test dataset -  I'm also including the measures for the Count of Customers. I redid the customers count - using the base that matt created. So shout-out to you, mattbrice! :) THANKS!

     

    Count of Customers =
    CALCULATE (
        DISTINCTCOUNT ( 'SALES DETAILS'[LinkToCustomerID] ),
        'SALES DETAILS'[SalesTxn Document Type] = "Invoice",
        'Calendar - Transaction Date'[Year] >= 2017
    )
    Count of Customers Last 12MO =
    VAR _last12MO =
        DATESBETWEEN (
            'Calendar - Transaction Date'[Transaction Date],
            NEXTDAY (
                SAMEPERIODLASTYEAR (
                    LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] )
                )
            ),
            LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] )
        )
    RETURN
        CALCULATE ( [Count of Customers], _last12MO )
    Total Outgoing Sales =
    CALCULATE (
        SUM ( 'SALES DETAILS'[Sales $$] ),
        'Calendar - Transaction Date'[Year] >= 2017
    )
    Total Sales Last 12MO =
    VAR _last12MO =
        DATESBETWEEN (
            'Calendar - Transaction Date'[Transaction Date],
            NEXTDAY (
                SAMEPERIODLASTYEAR (
                    LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] )
                )
            ),
            LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] )
        )
    RETURN
        CALCULATE ( [Total Outgoing Sales], _last12MO )
    Total Sales Last 12 MO Rolling Top20 =
    VAR _last12MO =
        DATESBETWEEN (
            'Calendar - Transaction Date'[Transaction Date],
            NEXTDAY (
                SAMEPERIODLASTYEAR (
                    LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] )
                )
            ),
            LASTDATE ( 'Calendar - Transaction Date'[Transaction Date] )
        )
    VAR _Top20Last12MO =
        CALCULATE (
            SUMX (
                VALUES ( 'Calendar - Transaction Date'[Month] ),
                CALCULATE (
                    [Total Outgoing Sales],
                    TOPN (
                        20,
                        VALUES ( 'SALES DETAILS'[LinkToCustomerID] ),
                        [Total Outgoing Sales]
                    )
                )
            ),
            _last12MO
        )
    RETURN
        _Top20Last12MO

    Final Result: