Forum Discussion
Running total YoY - Dax experts needed
Sales table
| Order date | Shipment date | order amount | Total order amount (running total) for June shippment for the year |
| 1-Jan-16 | 1-Jun-16 | 100 | 100 |
| 2-Feb-16 | 2-Jun-16 | 110 | 210 |
| 4-Feb-16 | 3-Jun-16 | 100 | 310 |
| 6-Feb-16 | 4-Jun-16 | 110 | 420 |
| 8-Feb-16 | 5-Jun-16 | 110 | 530 |
| 15-Mar-16 | 6-Jun-16 | 120 | 650 |
| 20-Apr-16 | 7-Jun-16 | 110 | 760 |
| 26-May-16 | 8-Jun-16 | 100 | 860 |
| 1-Jun-16 | 9-Jun-16 | 100 | 960 |
| 1-Jan-15 | 1-Jun-15 | 200 | 200 |
| 2-Feb-15 | 2-Jun-15 | 220 | 420 |
| 4-Feb-15 | 3-Jun-15 | 200 | 620 |
| 7-Feb-15 | 4-Jun-15 | 220 | 840 |
| 8-Feb-15 | 5-Jun-15 | 220 | 1060 |
| 17-Mar-15 | 6-Jun-15 | 240 | 1300 |
| 24-Apr-15 | 7-Jun-15 | 220 | 1520 |
| 26-May-15 | 8-Jun-15 | 200 | 1720 |
| 1-Jun-15 | 9-Jun-15 | 200 | 1920 |
Would like to get this result
| Selected shipment month: June 2016 | |||||
| Order date | Shipment month | amount | running total this year June | running total same month (which is june 2015) last year | |
| 1-Jan-16 | 1/06/2016 | 100 | 100 | 200 | |
| 2-Feb-16 | 1/06/2016 | 110 | 210 | 420 | |
| 4-Feb-16 | 1/06/2016 | 100 | 310 | 620 | |
| 6-Feb-16 | 1/06/2016 | 110 | 420 | 620 | |
| 7-Feb-16 | 1/06/2016 | 0 | 420 | 840 | |
| 8-Feb-16 | 1/06/2016 | 110 | 530 | 1060 | |
| 15-Mar-16 | 1/06/2016 | 120 | 650 | 1060 | |
| 17-Mar-16 | 1/06/2016 | 0 | 650 | 1300 | |
| 20-Apr-16 | 1/06/2016 | 110 | 760 | 1300 | |
| 24-Apr-16 | 1/06/2016 | 0 | 760 | 1520 | |
| 26-May-16 | 1/06/2016 | 100 | 860 | 1720 | |
| 1-Jun-16 | 1/06/2016 | 100 | 960 | 1920 |
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?
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
Memorable 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.
- AnonymousNot 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
Microsoft 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.
- Add two columns with following formulas in Sales table.
OrderMonthDay = MONTH( Sales[Order date] ) & "/" & DAY( Sales[Order date] ) & "/"
OrderYear = YEAR( Sales[Order date] )
- Create a new table with following formula.
FixedDate = FILTER ( CROSSJOIN ( VALUES ( Sales[OrderMonthDay] ), VALUES ( Sales[OrderYear] ) ), Sales[OrderMonthDay] <> BLANK () && Sales[OrderYear] <> BLANK () ) - 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 ) - 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
Super 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 :)
- AnonymousNot 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.
- Add two columns with following formulas in Sales table.