Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Help with Dax - Day on Day difference, with MTD and YTD

Hi, can you please assist with my DAX issue?

 

I have this data - see table below:

 

The Day on Day difference is taking the current day sales and subtracting the previous day sales.

 

atalabi_0-1595248256484.png

 

This was achieve using the dax below:

 

Day on Day difference =
VAR currentDate = MAX('Date'[Date])
VAR priorDate = CALCULATE(MAX('Date'[Date]), ALL('Date'[Date]), 'Date'[Date] < currentDate)
VAR priorDateSales = CALCULATE(SUMX('Sales','Sales'[DailySales), 'Date'[Date] = priorDate)
VAR CurrentDaySales = CALCULATE(SUMX('Sales','Sales'[DailySales), 'Date'[Date] = currentDate)
VAR daydiff = CurrentDaySales-priorDateSales
RETURN
daydiff

 

The problem I have is I need the YTD and MTD totals to be used in a Card Visual, but i only get the yesterday value (for 17th July) as you see in the table above.

 

Please how do I calculate the YTD and MTD totals for the Day on Day difference?

 

Thanks.

 

Ad

6 REPLIES 6
Greg_Deckler
Super User
Super User

@Anonymous - So, typically with YTD and MTD you use time intelligence functions with a separate date table. https://docs.microsoft.com/en-us/dax/time-intelligence-functions-dax

 

Also, see if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TITHW/m-p/434008


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thanks @Greg_Deckler 

 

Unfortunately this did not help - when i used the DATEYTD and the date table, i only got the same result as the Yesterday measure:

 

atalabi_0-1595250041564.png

I am hoping to see the total of 36,130 (all days), instead of 0 which is the yesterday value for 17th July

@Anonymous - Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

 

@Greg_Deckler 

 

I have added my sample data as text below.

 

Original data - Date and DailySales:

 

DateDailySales
01-Jul-20                 -35,300 
02-Jul-20-38,150
03-Jul-20-35,800
06-Jul-20-23,650
07-Jul-20-30,900
08-Jul-20-19,750
09-Jul-20-28,700
10-Jul-20-25,850
13-Jul-20-17,610
14-Jul-20-5,960
15-Jul-20-2,220
16-Jul-20-2,220
17-Jul-20-2,220
Total 

 

1st task - to calculate the day on day difference in the days, so Today sales - yesterday sales. 

 

I could not use the DATEADD or PREVIOUSDAY functions as the dates have gaps - no sales on weekends.

 

So to achive that, I used the dax fomular below to create a data on day difference column:

 

Day on Day difference =
VAR currentDate = MAX('Date'[Date])
VAR priorDate = CALCULATE(MAX('Date'[Date]), ALL('Date'[Date]), 'Date'[Date] < currentDate)
VAR priorDateSales = CALCULATE(SUMX('Sales','Sales'[DailySales), 'Date'[Date] = priorDate)
VAR CurrentDaySales = CALCULATE(SUMX('Sales','Sales'[DailySales), 'Date'[Date] = currentDate)
VAR daydiff = CurrentDaySales-priorDateSales
RETURN
daydiff

 

DateDailySalesDay on Day difference
01-Jul-20                             -35,300                                      3,050
02-Jul-20-38,150-2,850
03-Jul-20-35,8002,350
06-Jul-20-23,65012,150
07-Jul-20-30,900-7,250
08-Jul-20-19,75011,150
09-Jul-20-28,700-8,950
10-Jul-20-25,8502,850
13-Jul-20-17,6108,240
14-Jul-20-5,96011,650
15-Jul-20-2,2203,740
16-Jul-20-2,2200
17-Jul-20-2,2200
Total 0

 

This works fine for Yesterday's Day on Day difference, I now want the MTD and YTD totals for the Day on Day column.

 

When i use CALCULATE(Day on Day difference, DATEMTD('Date'[Date])), i still get the same 0 total, i expect to see 31,130

 

DateDailySalesDay on Day differenceDay on Day with DATEYTDExpected Results
01-Jul-20                                 -35,300                                 3,0503,0503,050
02-Jul-20-38,150-2,850-2,850-2,850
03-Jul-20-35,8002,3502,3502,350
06-Jul-20-23,65012,15012,15012,150
07-Jul-20-30,900-7,250-7,250-7,250
08-Jul-20-19,75011,15011,15011,150
09-Jul-20-28,700-8,950-8,950-8,950
10-Jul-20-25,8502,8502,8502,850
13-Jul-20-17,6108,2408,2408,240
14-Jul-20-5,96011,65011,65011,650
15-Jul-20-2,2203,7403,7403,740
16-Jul-20-2,220000
17-Jul-20-2,220000
Total 0031,130

 

I hope this makes my issue clearer.

@Anonymous , do want to stop YTD a day before

 

YTD Sales till yesterday = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"), Date[Date] <=Max(Date[Date])-1 )

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))

 

Day level formula is in the blog, I already shared

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors