Forum Discussion

ngadiez's avatar
ngadiez
Icon for Helper II rankHelper II
9 years ago

rolling 12 months formula dynamic

Hi Everyone,

 

I have a table which consists of the channel of sales, transaction date, sales amount, the cost of the goods sold, and the sales commission.

 

ChannelTransaction DateSales AmountCost of GoodsSales Commission
A07-Dec-16950374150
B18-Feb-16806206281
C28-Jan-17907348251
A04-Aug-16880158258
B24-Jul-1640815385
C31-Aug-16972228204
A25-Jul-1647669132
C12-Dec-1621611537
B01-Apr-16946248281
A20-Dec-16744153300
C17-Dec-16778181207
A07-Nov-16467121164
A22-Jan-17960465247
B05-Jan-1725314158
A08-Aug-16975157132
A24-Mar-17909100349
A07-Mar-16864258196
C26-Feb-16772450135
B21-Dec-16768359108
A20-Jun-16827332210
C04-Apr-16449265100
A30-Jan-1628533107
B15-Apr-17765417178
A21-Mar-1756581255
C12-Feb-17720203284
A15-Sep-16941400147
B12-Dec-16764307161
A26-Feb-1663933870
C14-Nov-161362314

 

Every beginning of the month, I will take the previous 12 months data and calculate the total sales, the percentage of goods sold from total sales, the percentage of commission from total sales.

 

So for January 2017:

Total Sales= Calculate(SUM([Sales Amount]),FILTER(Table1,AND([Transaction Date]<01/01/2017,[Transaction Date]>=01/01/2016)))

Of course this will do if I just want to calculate the total sales amount only for january.

However I dont know what should I do if I want to change the valuation month: 

example for Feb 2017: 

Total Sales= Calculate(SUM([Sales Amount]),FILTER(Table1,AND([Transaction Date]<01/02/2017,[Transaction Date]>=01/02/2016)))

 

How can I produce some table like this? where the formula is based on the valuation month:

 

BOMJan-17  Feb-17  Mar-17  
ChannelTotal Sales% Cost of Good%  CommissionTotal Sales% Cost of Good%  CommissionTotal Sales% Cost of Good%  Commission
A         
B         
C         

 

Thank you so much for helping. 

Tagging the pro user: MattAllingtonkcantorDatatouilleMiguelMartinezelliotdixon

6 Replies

  • MattAllington's avatar
    MattAllington
    Icon for Community Champion rankCommunity Champion

    First you need a calendar table. Ready my article here https://exceleratorbi.com.au/power-pivot-calendar-tables/

     

    make sure you have a MonthID column that co sisters of Integers counting unique months. 

     

    Then write a custom time intelligence function. Read my article here https://exceleratorbi.com.au/dax-time-intelligence-beginners/

     

    Something like this

     

    Total Sales Year MAT =
    CALCULATE (
    [Total Sales],
    FILTER (
    ALL ( Calendar ),
    Calendar[MonthID] >= MAX ( Calendar[MonthID] ) - 11
    && Calendar[MonthID] <= MAX ( Calendar[MonthID] )
    )
    )

  • Hi ngadiez

     

    You are doing Time-Intelligence calculations so you need a proper calendar table.

     

    Once you have this Calendar Table, link it to your Main Table with the Dates Column and create the following measure:

     

    SalesRolling = Calculate ( [Total Sales] , DatesInPeriod(Calendar[Dates], min(Calendar[Dates]) - 1, -1, YEAR)

    where [Total Sales] = Sum(YourTable[SalesAmount])

     

    In this example, DatesInPeriod starts from the minimum date in your current filter context, and shifts from 1 year using the Calendar table.

    You can apply the same logic to the other measures.

  • v-yulgu-msft's avatar
    v-yulgu-msft
    Icon for Microsoft Employee rankMicrosoft Employee

    Hi ngadiez,

     

    Have you achieved your desired result? If yes, would you please kindly mark the helpful reply as an answer so that others having similar requirement can benefit from it?

     

    Regards,
    Yuliana Gu

  • I have a set of data:

     

    ChannelTransaction DateSales AmountCost of GoodsSales Commission
    A07-Dec-16950374150
    B18-Feb-16806206281
    C28-Jan-17907348251
    A04-Aug-16880158258
    B24-Jul-1640815385
    C31-Aug-16972228204
    A25-Jul-1647669132
    C12-Dec-1621611537
    B01-Apr-16946248281
    A20-Dec-16744153300
    C17-Dec-16778181207
    A07-Nov-16467121164
    A22-Jan-17960465247
    B05-Jan-1725314158
    A08-Aug-16975157132
    A24-Mar-17909100349
    A07-Mar-16864258196
    C26-Feb-16772450135
    B21-Dec-16768359108
    A20-Jun-16827332210
    C04-Apr-16449265100
    A30-Jan-1628533107
    B15-Apr-17765417178
    A21-Mar-1756581255
    C12-Feb-17720203284
    A15-Sep-16941400147
    B12-Dec-16764307161
    A26-Feb-1663933870
    C14-Nov-161362314
    A20-Apr-1725610571
    A23-Aug-16770244307
    B22-Apr-164996360
    B12-Jun-16864422139
    A16-Jun-1617910720
    C03-Jul-1638118155
    A21-Oct-16557201150
    B05-Mar-1752971132
    A01-Mar-17880476224
    C23-Jun-1644813165
    A16-Nov-1619211525
    B07-Jun-161363021
    A17-Jan-16854109417

     

    Where I want to make Matrix like this:

     

    BOMJan-17  Feb-17  Mar-17  Apr-17  
    ChannelTotal Sales% Cost of Good%  CommissionTotal Sales% Cost of Good%  CommissionTotal Sales% Cost of Good%  CommissionTotal Sales% Cost of Good%  Commission
    A1060029.90%26.27%1042133.51%24.07%978232.24%24.92%1127231.52%27.24%
    B519134.44%21.88%544435.43%21.93%463837.15%19.69%516734.72%20.22%
    C415237.91%19.68%505937.99%21.11%500733.45%24.31%500733.45%24.31%

    For example, for Jan 17, Total sales is all transaction from 1 jan 2016 to 31 dec 2016. 
    for every beginning of the month (BOM), I will calculate the number from the previous 12 months.

     

    I know how to calculate each of the items if there is no matrix column, by using calculate and filter.

    However, if there is matrix column, it will become more difficult. 

     

    Is there any way to do this?

  • kcantor's avatar
    kcantor
    Icon for Community Champion rankCommunity Champion

    ngadiez,

    My solution will not be elegant like Matt's but we have a different data culture at my company. Every time I provide data, I have to provide very specific filter context. That means I have to display the dates I use to make it very Fisher Price for consumers. I have a report similar to what you are building. I list our reps and their Moving Annual Net Invoiced total in a table. Beside that, I have a multi card that shows the dates used in the calculation. That reduces time spent responding to questions that I shouldn't have to answer because "Moving Annual Total" is a very specific title . . . but I digress. 

    That means I use extra steps that may be be useful for you depending on  your company's data culture. First, as everyone else has said, make sure you have a good solid date table.

    My next step is the basic calculations such as creating the Net Invoiced total. I will not walk you through that. I am sure you can handle the Sums.

    Then I have a measure called Last day of previous month for simplicity and to make sure nobody misunderstands.

    Last day of previous month: =

    EOMONTH(TODAY(), -1)

    Then a measure for the first day included in the calculation.

    First date in period: =
    EOMONTH ( TODAY (), -13 ) + 1

    Finally, I have my moving annual net invoiced that only uses full months and ignores current month to date.

    Annual Net Invoiced: =
    CALCULATE (
        [Net Invoiced],
        DATESBETWEEN (
            DimDate[DateKey],
            [First date in period],
            [Last day of previous month]
        )
    )

    As I said, my company needs the extra information displayed. Yours may not. 

    Anyway, hope this helps and who knows, MattAllington or Datatouille may find an error in my calculation I have not seen as I am no where near their level in this DAX thing.

  • Hi @ngadiez,

    Please refer below stpes to get desired output
    1. Clearly you will need one dim-table for data. You can use CalendarAuto function in New table to create that 

    2. Create Custom Column for Month 

           Month  = EOMONTH(Table1[Transaction Date],-1)+1

    3. Create a measure for Total Sales

           TotalSales = CALCULATE(SUM(Table1[Sales Amount]),DATESBETWEEN('calendar'[Date],PREVIOUSYEAR(FIRSTNONBLANK(Table1[month],TRUE())),FIRSTNONBLANK(Table1[month],TRUE())-1))

    4. Now pull Month in column and TotalSales in values in Matrix

     

    Hope this helps :)