DAX Previous Month to date total is giving entire previous month's entire total - powerbi

I am using DAX in Power BI to calculate Previous Month sales total to date to create a KPI visual. i.e. if today is 7th Dec then I want to get sales total from 1st Nov to 7th Nov and compare with current month to date.
CurrentMTD = TOTALMTD(SUM(SALES_VOUCHERS[SaleValue]),DatesTable[Date])
This works fine.
However Previous Month YTD gives me total for entire month of November. I have tried the following so far
PMYTD = totalmtd(sum(SALES_VOUCHERS[SaleValue]),dateadd(DATESMTD(DatesTable[Date]),-1,month))
and
PMYTD = CALCULATE(sum(SALES_VOUCHERS[SaleValue]),
DATESBETWEEN(DatesTable[Date],
FIRSTDATE(PREVIOUSMONTH(DatesTable[Date])),
LASTDATE(DATEADD(DatesTable[Date],-1,MONTH))))
Both return the same answer which is total for the entire previous month .
If I simply hardcode the start and end date in datesbetween version above, then I do get the desired result. But that is not the solution.
I have linked the fact table (Sales_VOUCHERS) to a DatesTable and as of now there are no other visuals on the report page.
Kindly assist what I am missing out on and how I can get Previous Month year to date total

If you're aggregating at the month level (i.e. you're looking at December 2016 vs. November 2016), then the measure you have above will show you the entire month of December compared to the entire month of November (and since December is a partial month and November isn't, it causes the mismatch you see).
If you filter to the current date (e.g. 7th Dec), then both your MTD and Prior Month MTD measures will only show you through the 7th of their corresponding months.
Assuming you don't want to filter to the day level (not unreasonable), you could enhance your formula to filter out future dates. For example:
PMYTD = totalmtd(
sum(SALES_VOUCHERS[SaleValue]),
dateadd(
FILTER(
DATESMTD(DatesTable[Date]),
DatesTable[Date]<TODAY()
),
-1,
month
)
)
This says, if the date is after today, don't pass it into the TOTALMTD calculation (so it will only calculate the first 7 days of the month, for example, if today is Dec 8th - even if you're looking at full months on your report).
Side note: you can also write your previous month measure to re-use your MTD measure rather than redefining it. In this way, if you ever change the MTD calculation, the previous MTD calculation automatically updates.
PMYTD = CALCULATE(
[CurrentMTD],
DATEADD(
FILTER(
DatesTable[Date],
DatesTable[Date]<TODAY()
),
-1,
MONTH
)
)
Useful Resources:
https://www.powerpivotpro.com/2016/01/year-to-date-in-previousprior-year/ (article that covers this problem and a variety of solutions)
https://community.powerbi.com/t5/Desktop/Compare-MTD-with-previous-period/td-p/24656 (forum discussion about the same problem)
http://community.powerbi.com/t5/Desktop/Time-Intelligence-TOTALMTD-vs-DATESMTD-vs-DATEADD/td-p/10088 (forum discussion about DATESMTD vs TOTALMTD)

Related

DAX: Projected sum for all months greater than current month of current year

I have a requirement to filter a sum of projected sales for all months >= current month of the current year and any subsequent years that we have project sales data for.
So for 2023 I'd need the sum of projected sales for only February (the current month) through December 2023 on.
My DAX below accomplishes this accurately for current year OR current month, but I can't seem to filter by both without the results including all prior years. Client wants to see past sales and projected sales in the same visual so I cannot just filter out the prior years.
CURRENT SUM OF SALES = CALCULATE(SUM('Table'[Sales]), FILTER('DateTable', YEAR('DateTable'[Date]) >= YEAR(TODAY())))
Thank you for any help!
First, let me answer your request on writing a DAX that filters all months >= current month of the current year; you should create your date as follow:
DATE(YEAR(NOW()),MONTH(NOW()),1)
Then add it to your measure as follow:
CURRENT SUM OF SALES = CALCULATE(SUM('Table'[Sales]), FILTER('DateTable', 'DateTable'[Date] >= DATE(YEAR(NOW()),MONTH(NOW()),1)))
Second, regarding your concern about your client wanting to see past sales and projected sales in the same visual, you only have to create another measure that shows past sales.
If I understand your table structure correctly, this month's sales value will be updated to the actual value when your ETL refreshes the data next month; if not, please let me know.
If my assumption is valid, you can write Past Sales - measure as follows:
Past Sales = CALCULATE(SUM('Table'[Sales]), FILTER('DateTable', 'DateTable'[Date] < DATE(YEAR(NOW()),MONTH(NOW()),1)))
Then, use both measures in a Line chart with the 'DateTable'[Date] column as the X-axis.
I hope I helped in a way; if so, please mark this as an answer and vote for it :)

DATEADD to calculate PYTD (Jan 1 till today's date) in DAX Power BI

I have a daily sales data for 3-4 years. I want to create the YTD and Prior Year To Date sales measure that will be updated daily. That is it should always be from beginning of the year (selected) to TODAY or the last day of the data (1 day lag from today and max date).
I used Sameperiodlastyear but it is problematic at the beginning of the month as it compares say Jan 1, 2022-June 8 2022 with Jan 1, 2021 with June 30, 2021.
Any suggestion how I can create a modified prior year to date measure to address this nuance?
This is a standard solution for the case. First, you get all dates, with a DATESYTD() function, for the current year or last visible year up today or last visible day, then you offset it.
SAMEPERIODLASTYEAR(DATESYTD(‘Date’[Date]))
It is equal to
DATEADD(DATESYTD(‘Date’[Date]),-1,YEAR))
Try this if you want to get exact days set:
VAR FirstDayThisYear =
SAMEPERIODLASTYEAR(STARTOFYEAR(‘Date’[Date])
VAR LastDayThisYear =
SAMEPERIODLASTYEAR(
LASTDATE(‘Date’[Date])
)
VAR SetOfDates=
DATESBETWEEN(
‘Date’[Date]
,FirstDayThisYear
,LastDayThisYear
)
RETURN
SetOfDates

Power BI growth rate calculation YoY

I'm having some difficulty getting a YoY change % for values in Power BI. The averages don't come out proper. I've come to understand this is an AVERAGE vs AVERAGEX issue in Power BI.
I need to create charts of year on year growth monthly. So Jan20 % change from Jan 19. I thought what was below was correct, but it is always throwing an issue for the month of February and a few other months. But some of the months are correct. My Measure is below.
Growth =
IF(
ISFILTERED('Oct5_5'[TRAFFIC_DTE]),
ERROR("Check Time Filter."),
VAR PrevMonth =
CALCULATE(
AVERAGE('Oct5_5'[VISITS_AMT]),
DATEADD('Oct5_5'[TRAFFIC_DTE].[Date], -12, MONTH)
)
RETURN
DIVIDE(
AVERAGE('Oct5_5'[VISITS_AMT]) - PrevMonth,
PrevMonth
)
)
Snippet
Can someone please show me how to use the right Average? Thank you so much!
Do You have all calendar days in your traffice_dte?
Probably not. This causes you to go back to a date that does not exist, which causes an error.
DATEADD function work ok if you use it on "calendar" tabel.
Requirement below:
https://dax.guide/dateadd/
The Date table must always start on January 1 and end on December 31, including all the days in this range. If the report only references fiscal years, then the date table must include all the dates from the first to the last day of a fiscal year. For example, if the fiscal year 2008 starts on July 1, 2007, then the Date table must include all the days from July 1, 2007 to June 30, 2008.
There needs to be a column with a DateTime or Date data type containing unique values. This column is usually called Date. Even though the Date column is often used to define relationships with other tables, this is not required. Still, the Date column must contain unique values and should be referenced by the Mark as Date Table feature. In case the column also contains a time part, no time should be used – for example, the time should always be 12:00 am.
The Date table must be marked as a date table in the model, in case the relationship between the Date table and any other table is not based on the Date.

How to filter YTD to last complete month in Power BI

I am creating a report with buttons that use a slicer to show the last 3 calendar months, the default view, and YTD. The first two are all set and will continue to work fine, however i am having trouble with the YTD filter because i need it to exclude the current month (some of the key metrics for this slicer are only accurate monthly, even thought the data is updated Daily). Any idea how to accomplish this without me having to manually change it every month? An example of it working today would show me 2020 through August, since September is not complete. September would be included in the filter starting October first. I am thankful for your help/insights!
I typically build a calculated column on my date table called something like "Date in Range", that looks something like the below. You could also apply this to a date in a normal table if you are not using a date dimension.
Date in Range = IF ('MyTable'[Date] <
DATEADD(TODAY(), -1 * DAY(TODAY()), day),
1,
0)
This compares the date in the table row with TODAY(), e.g. 14 Sep 2020, minus the day of the month of today (14), effectively getting you back to the start of the current month. This will then return 1 for dates before the end of last month or 0. Filter on 1 or 0 to get your result (or use something more meaningful in place of the 1 or 0).

Compare totals for the same partial date range year-over-year in DAX / Power BI

I'm trying to create a table which shows a sum of monthly values for one year compared to the last year's totals (structured as the screenshot below):
Monthly Comparison
However, the caveat I'm dealing with is comparing the most current month, which will always contain partial month data (unless it's the last day of the month), to the same date range of the previous year. In the screenshot I attached, our data for January 2018 only goes through January 22nd. However, it's comparing it to the full month of January from 2017, whereas we want that total to be January 1st - 22nd, 2017: Value That Needs to be Updated.
I've tried messing around with various MTD and cumulative totals, but I can't seem to get the logic to work while keeping the aggregation to the monthly level. Any idea what type of logic needs to used in order to compare year-over-year totals, but only do a partial sum for the same date range of a month that is currently in progress?
Thanks in advance.
In my short example, this seems to work:
Total Sales LY 2 =
VAR MaxDate = EDATE(CALCULATE(MAX(Sales[Date]);ALL(Sales));-12)
RETURN
CALCULATE(
[Total Sales];
FILTER(SAMEPERIODLASTYEAR('Date'[Date]);'Date'[Date]<=MaxDate)
)
I calculate Total Sales for the same period last year, with the max of the last available sales date this year.
Total Sales LY = Comparing last year full month (wrong)
Total Sales LY 2 = Comparing last year month, with max of last sales date
PBIX file