DateTable=CALENDARAUTO()
Calculated columns:
NextYearDate = DATEADD(DateTable[Date],1,YEAR)
The above calculated column shows blanks instead of the next years date. Why is that and how to fix it?
DATEADD is a Time Intelligence function, which returns dates from your date column shifted by the specified interval. This means that for the latest year in your date table, there are no "next year" values to return.
An alternative would be:
NextYearDate =
DATE (
YEAR ( DateTable[Date] ) + 1,
MONTH ( DateTable[Date] ),
DAY ( DateTable[Date] )
)
Related
I have a measure that sums expense amount for the current filtered year. This works as desired.
Expense = CALCULATE(SUM('GL Data'[Amount]),FILTER('GL Data','GL Data'[SubGrp1] = "EXPENSE"))
I want to sum the expense amount for the same time period of prior year. My measure calculates correct data as of 11 months ago, not 1 year ago. It appears to function as if the data calculation is looking at my system date, not the specified date:
Expense PY = CALCULATE([Expense], DATEADD(Dates[Date],-1,YEAR))
I've attempted using -12, -13 and MONTH in the DATEADD but the result doesn't correctly total 1 year ago. I've also tried SAMEPERIODLASTYEAR() which provides wrong info. Perhaps my Expense measure is the culprit.
My Date table is generated with this Dax:
Could you please use this as your date table, and test it. In your date table, you imposed no restrictions on the calendar auto() functions which I think is not useful. Also You used Generate function which ignores rows with blank values. In such a case, use GENERATEALL() function; but You need to form your date table like this:
Date =
VAR MinYear =
YEAR ( MIN ( Fact_Table[Date_Column] ) )
VAR MaxYear =
YEAR ( MAX ( Fact_Table[Date_Column] ) )
RETURN
ADDCOLUMNS (
FILTER (
CALENDARAUTO ( 12 ),
AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
),
"Calendar Year", "CY " & YEAR ( [Date] ),
"Day", FORMAT ( [Date], "dddd" ),
"Month Number", MONTH ( [Date] ),
"Month", FORMAT ( [Date], "mmmm" ),
"Year Month", FORMAT ( [Date], "mmm yy" ),
"Year", FORMAT ( [Date], "YYYY" )
)
After creating your Date Table, please do not forget to mark it as date table which is a good DAX practice as in the photo below.
I'm trying to calculate MAT (Moving Annual Total) using DAX, but can't realize exactly what I want.
I have the data as below.
What I'm trying to is calculate the total of treatment for each 12 month, like "Jan 21 - Dec 21" which has 'date' from 2020/1/1 to 2020/12/1. i.e. 'A' appears 4 times in that period, so the result will be like below.
Then I'd like to continue this for each latest 12 months, and finally visualize it like below.
I've read that I should use CALCULATE and DATESPERIOD in DAX, can't calculate exactly though. The following code is the one I tried and failed.
Moving Annual Total = CALCULATE(
COUNTA('2017-2022Q1'[idnum]),
DATESINPERIOD('2017-2022Q1'[mat].[Date], LASTDATE('2017-2022Q1'[mat].[Date]), 12, MONTH))
Could someone kindly give me an advise to realize this?
Thanks.
*This is the sample file.
https://drive.google.com/file/d/1gDNeBe5KiKBqx3cZ7G0SMiSQ23w96NF4/view?usp=sharing
In your dax measure, I recommend you first to create a date table and create a one-to-many relationship with the fact table(your table: '2017-2022Q1') ; then filter the date table in your calculate measure, not the fact table('2017-2022Q1') directly as you did here. You should follow the best practice.
For Example: How to create a date table using dax:
Date =
VAR MinYear = YEAR ( MIN ( '2017-2022Q1'[date] ) )
VAR MaxYear = YEAR ( MAX ( '2017-2022Q1'[date] ) )
RETURN
ADDCOLUMNS (
FILTER (
CALENDARAUTO( ),
AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
),
"Calendar Year", "CY " & YEAR ( [Date] ),
"Month Name", FORMAT ( [Date], "mmmm" ),
"Month Number", MONTH ( [Date] )
)
Your final code could be like this:
Moving Annual Total = CALCULATE(
COUNTA('2017-2022Q1'[idnum]),
DATESINPERIOD('Date'[Date], LASTDATE('Date'[Date]), -12, MONTH)
)
if you define the start date using the lastdate() function, then It returns the last date in your filter context. you should also specify the interval as (-12) month to extract all dates within 1 year period.
Hope It helps.
How can I get the last day of month from the last date with sales? I want the result to be returned as a DAX calculated table.
The first step, calculated table:
LastDate = LASTNONBLANK( T[Date], CALCULATE( SUM( T[Amount] ) ) )
It returns a table (one column, one row) with the last date with sales. So far, correct, as expected. Say the last day with sales is 2020-04-15. But I want the end of month so 2020-04-30.
I hoped this should work, but it doesn't.
LastDate =
ENDOFMONTH (
LASTNONBLANK (
T[Date],
CALCULATE ( SUM ( T[Amount] ) )
)
)
It still returns 2020-04-15, instead of expected 2020-04-30.
The problem is caused by using dates from table T. Instead, you need to use dates from the calendar table, because list of dates for the time intelligence functions must be continuous.
Assuming you have a properly designed and connected calendar table 'Date' in your data model, change your DAX to:
LastDate =
ENDOFMONTH (
LASTNONBLANK (
Date[Date],
CALCULATE ( SUM ( T[Amount] ) )
)
)
ENDOFMONTH will use now a different table (Date), because LASTNONBLANK function will keep data lineage to it.
I need several measures in my report. The measure I want to start with is count of distinct ID four months before the month I selected on (e.g if I select on Aug 2018, the calculation would be all distinct ID before 30/04/2018). The reason I'm doing this is later on I also want to use this same slicer to work on count of ID within the four month period based on the selection.
Here is my DAX Calculation with comments:
Count four months ago =
// Find the end date of the month
VAR end_of_last_quarter =
FORMAT ( EOMONTH ( MAX ( 'Calendar'[Date] ), -4 ), "dd/mm/yyyy" )
RETURN
// Count distinct ID on or before that date
CALCULATE (
DISTINCTCOUNT ( 'Report Data'[Id] ),
FORMAT ( 'Report Data'[REPORT DATE], "d/mm/yyyy" )
<= FORMAT ( end_of_last_quarter, "d/mm/yyyy" )
)
& " Reports before "
& end_of_last_quarter
However after checking this calculation, it seems it only gives me the number of counts in the month I selected:
The screenshot tells me there are 12 report in Apr 2018, rather than the right number before 31/12/2017.
Thanks in advance for any ideas
I try to calculate moving average in DAX power bi. I use different codes, for example this.
Moving AverageX 7B Days =
AVERAGEX (
DATESINPERIOD(
sahkoInput[Date];
LASTDATE ( sahkoInput[Date]);
-7;
DAY
);
sahkoInput[price]
)
All codes give the same result - Moving AverageX 7B Days is equal to column "price". What went wrong and how to fix it?
Firstly, I would look to add a date/calendar table to your data model.
This can be as simple as a list of consecutive dates from at least seven days before your first data point until after the end of when you expect your last one to be.
The reason for this is that the date functions in DAX always work best when they have a table of consecutive dates to look at - you can get unpredictable results when your fact table doesn't have any data on a particular date.
Once you have added the date table, create a relationship to link the date column in your sahkoInput table to the date column in your date table.
Now, the following measure should work:
Moving AverageX 7B Days =
CALCULATE (
AVERAGE('sahkoInput'[Price]);
DATESINPERIOD ('DateTable'[Date];
LASTDATE ('DateTable'[Date]);
-7;
DAY)
)
Create a date table, this will dramatically improve performance and increase readability. You can do that by using the following DAX:
Min Date := MIN('sahkoInput'[Date])
Max Date := MAX('sahkoInput'[Date])
Dates :=
VAR BaseCalendar =
CALENDAR ( [Min Date], [Max date] )
RETURN
GENERATE (
BaseCalendar,
VAR BaseDate = [Date]
VAR YearDate =
YEAR ( BaseDate )
VAR MonthNumber =
MONTH ( BaseDate )
RETURN
ROW (
"Day", BaseDate,
"Year", YearDate,
"Month Number", MonthNumber,
"Month", FORMAT ( BaseDate, "mmmm" ),
"Year Month", FORMAT ( BaseDate, "mmm yy" )
)
)
Then referencing this date table you can create an average using the standard average function, like so:
Moving Average 7 Days :=
CALCULATE (
AVERAGE ( 'sahkoInput'[Price] );
KEEPFILTERS ( DATESINPERIOD ( 'Dates'[Date]; MAX ( 'Dates'[Date] ); -7; DAY ) )
)
I hope this helps!