Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
10 years ago
Solved

Running total YoY - Dax experts needed

Sales table

Order dateShipment dateorder amountTotal order amount (running total) for June shippment for the year
1-Jan-161-Jun-16100100
2-Feb-162-Jun-16110210
4-Feb-163-Jun-16100310
6-Feb-164-Jun-16110420
8-Feb-165-Jun-16110530
15-Mar-166-Jun-16120650
20-Apr-167-Jun-16110760
26-May-168-Jun-16100860
1-Jun-169-Jun-16100960
1-Jan-151-Jun-15200200
2-Feb-152-Jun-15220420
4-Feb-153-Jun-15200620
7-Feb-154-Jun-15220840
8-Feb-155-Jun-152201060
17-Mar-156-Jun-152401300
24-Apr-157-Jun-152201520
26-May-158-Jun-152001720
1-Jun-159-Jun-152001920

 

Would like to get this result

 

Selected shipment month: June 2016    
Order dateShipment monthamountrunning total this year Junerunning total same month (which is june 2015) last year
1-Jan-161/06/2016100100200 
2-Feb-161/06/2016110210420 
4-Feb-161/06/2016100310620 
6-Feb-161/06/2016110420620 
7-Feb-161/06/20160420840 
8-Feb-161/06/20161105301060 
15-Mar-161/06/20161206501060 
17-Mar-161/06/201606501300 
20-Apr-161/06/20161107601300 
24-Apr-161/06/201607601520 
26-May-161/06/20161008601720 
1-Jun-161/06/20161009601920 

 

My model is simple. The sales table and DimShippmentDate (joined by shipment date) and DimOrderDate (joined by Order date).

The running total this year = 

calculate (sum('Sales'[amount]),

filter(all(DimOrderDate), DimOrderDate[FullDate]<=max(DimOrderDate[FullDate]))

)

nothing special here. Just typical running total calculation. Working fine.

 

but it is killing me to get the running total same month last year (in the case June 2015).

I tried something like this but no working

Running Total LY =

CALCULATE(sum('Sales'[amount]),

filter(all(DimOrderDate[FullDate]), DimOrderDate[FullDate]=max(DimOrderDate[FullDate])-365), filter(all(DimShippmentDate[FullDate]), year(DimShippmentDate[FullDate]) = year(max(DimShippmentDate[FullDate]))-1 && month(DimShippmentDate[FullDate]) = month(max(DimShippmentDate[FullDate])))

)

 

 

Ultimate goal is to display this year selected month (EG June 2016) and the same period last year (June 2015) shippment's running total order amount (by order date) displayed on a SAME chart for YoY comparison.

currently I can only display this year and last year running total line chart on TWO different line charts instead of ONE.

 

Is this even possible? If not, any workaround please?

 

  • OwenAuger's avatar
    OwenAuger
    10 years ago

    Hi Anonymous & v-haibl-msft

     

    Here is another approach.

     

    PBIX:

    https://www.dropbox.com/s/judt8pl17by54b5/Running%20total%20YoY%20-%20Owen.pbix?dl=1

     

    I left the tables as per the description (slight name changes):

    Then created below measures:

    • The important one is [Amount Last Year] that shifts both Shipment Date & Order Date back a year.
    • Then [Running Total Last Year] and [Running Total This Year] can be calculated using DATESYTD on Order Date.
    • The last two "relevant dates" measures just blank out Running Totals when Amount is blank, for presentation purposes.

     

    Amount = 
    SUM ( Sales[order amount] )
    
    Amount Last Year = 
    CALCULATE (
        [Amount],
        SAMEPERIODLASTYEAR ( DimShipmentDate[Shipment Date] ),
        SAMEPERIODLASTYEAR ( DimOrderDate[Order Date] )
    )

    Running Total This Year =
    CALCULATE ( [Amount], DATESYTD ( DimOrderDate[Order Date] ) )
    Running Total Last Year = CALCULATE ( [Amount Last Year], DATESYTD ( DimOrderDate[Order Date] ) ) Running Total Last Year (relevant dates) = IF ( OR ( NOT ( ISBLANK ( [Amount] ) ), NOT ( ISBLANK ( [Amount Last Year] ) ) ), [Running Total Last Year] ) Running Total This Year (relevant dates) = IF ( OR ( NOT ( ISBLANK ( [Amount] ) ), NOT ( ISBLANK ( [Amount Last Year] ) ) ), [Running Total This Year] )

     

    Output is more or less as you wanted:

     

    Regards

    Owen :)

7 Replies

  • KGrice's avatar
    KGrice
    Icon for Memorable Member rankMemorable Member

    It's definitely possible to get a current year running total and a last year running total on the same chart, plotted over the same days. Are you making use of a calendar/dates table? If so, you can use the SAMEPERIODLASTYEAR function. For example, if you have your Running Total CY (current year) measure already set, you could create a Running Total LY measure as

     

    =CALCULATE([Running Total CY], SAMEPERIODLASTYEAR(Calendar[Date]))

     

    where the Calendar[Date] part is your calendar/dates table, and [Date] is the name of the date column.

    • Anonymous's avatar
      Anonymous
      Not applicable

      KGrice Thanks for the suggestion. But this is more complex than a typical this YTD vs last YTD calculation. Yes, I have a dimOrderDate dimension for this calculation. 

      With that, I can get

      this year's running total (by order date) for ANY ORDERS and last year's running total for ANY ORDERS. 

       

      But,What I need is that

      this year's running total (by order date) for ONLY ORDERS that shipped in a selected month this year (EG June 2016) and last year's running total for ONLY ORDERS that shipped in a same month last year (June 2015). 

       

      So you see, this is another date (shippment date) also in play here. Have a close look at my example please and hope it makes sense.

       

       

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

    Anonymous

     

    In this scenario, I think we need to create another table and add some columns to show the results you wanted.

    Please refer to following steps. I've also upload the Power BI file here for reference.

    1. Add two columns with following formulas in Sales table.
      OrderMonthDay = MONTH( Sales[Order date] ) & "/" & DAY( Sales[Order date] ) & "/"
      OrderYear = YEAR( Sales[Order date] )
    2. Create a new table with following formula.
      FixedDate = 
      FILTER (
          CROSSJOIN ( VALUES ( Sales[OrderMonthDay] ), VALUES ( Sales[OrderYear] ) ),
          Sales[OrderMonthDay] <> BLANK ()
              && Sales[OrderYear] <> BLANK ()
      )
    3. Create below columns in above new created table.
      FixedDate = 
      FixedDate[OrderMonthDay] & FixedDate[OrderYear]
      FixedPre&LastDate = 
      IF (
          FixedDate[OrderYear] = MAX ( 'Calendar'[CalendarYear] ),
          DATEADD ( FixedDate[FixedDate], -1, YEAR ),
          DATEADD ( FixedDate[FixedDate], 1, YEAR )
      )
      FixedShipMonth = 
      IF (
          RELATED ( DimShippmentDate[ShipYearMonth] ) <> BLANK (),
          RELATED ( DimShippmentDate[ShipYearMonth] ),
          IF (
              FixedDate[OrderYear] = MAX ( 'Calendar'[CalendarYear] ),
              LOOKUPVALUE (
                  DimShippmentDate[ShipYearMonth],
                  Sales[Order date], FixedDate[FixedPre&LastDate]
              )
                  + 100,
              LOOKUPVALUE (
                  DimShippmentDate[ShipYearMonth],
                  Sales[Order date], FixedDate[FixedPre&LastDate]
              )
                  - 100
          )
      )
      Sales = 
      IF (
          RELATED ( DimOrderDate[Order Date] ) <> BLANK (),
          RELATED ( Sales[order amount] ),
          0
      )
    4. Create three measures to show the results.
      SaleRunningTotal = 
      CALCULATE (
          SUM ( FixedDate[Sales] ),
          FILTER (
              ALL ( FixedDate[FixedDate] ),
              FixedDate[FixedDate] <= MAX ( FixedDate[FixedDate] )
          ),
          VALUES ( FixedDate[FixedShipMonth] )
      )
      SalesSameDayLY = 
      CALCULATE (
          SUM ( FixedDate[Sales] ),
          SAMEPERIODLASTYEAR ( FixedDate[FixedDate] )
      )
      SaleRunningTotalLY = 
      CALCULATE ( [SaleRunningTotal], SAMEPERIODLASTYEAR ( FixedDate[FixedDate] ) )


      Best Regards,

      Herbert

    • OwenAuger's avatar
      OwenAuger
      Icon for Super User rankSuper User

      Hi Anonymous & v-haibl-msft

       

      Here is another approach.

       

      PBIX:

      https://www.dropbox.com/s/judt8pl17by54b5/Running%20total%20YoY%20-%20Owen.pbix?dl=1

       

      I left the tables as per the description (slight name changes):

      Then created below measures:

      • The important one is [Amount Last Year] that shifts both Shipment Date & Order Date back a year.
      • Then [Running Total Last Year] and [Running Total This Year] can be calculated using DATESYTD on Order Date.
      • The last two "relevant dates" measures just blank out Running Totals when Amount is blank, for presentation purposes.

       

      Amount = 
      SUM ( Sales[order amount] )
      
      Amount Last Year = 
      CALCULATE (
          [Amount],
          SAMEPERIODLASTYEAR ( DimShipmentDate[Shipment Date] ),
          SAMEPERIODLASTYEAR ( DimOrderDate[Order Date] )
      )

      Running Total This Year =
      CALCULATE ( [Amount], DATESYTD ( DimOrderDate[Order Date] ) )
      Running Total Last Year = CALCULATE ( [Amount Last Year], DATESYTD ( DimOrderDate[Order Date] ) ) Running Total Last Year (relevant dates) = IF ( OR ( NOT ( ISBLANK ( [Amount] ) ), NOT ( ISBLANK ( [Amount Last Year] ) ) ), [Running Total Last Year] ) Running Total This Year (relevant dates) = IF ( OR ( NOT ( ISBLANK ( [Amount] ) ), NOT ( ISBLANK ( [Amount Last Year] ) ) ), [Running Total This Year] )

       

      Output is more or less as you wanted:

       

      Regards

      Owen :)

      • Anonymous's avatar
        Anonymous
        Not applicable

        Guys, really appreciate the efforts for helping me here! Hughly impressed with your Dax skill.

        v-haibl-msftyour solution is certainly an eye-opening dax exercise. Never imagined you can do those in dax.

        OwenAugerI think your solution is what I have been looking for and easier to implement. Seems that the amount last year was the key element missing in my puzzle. Still a lot to digest though. Will have a test on my model and see how it goes.