Power Bi DAX: Relative Date Filtering - powerbi

I have a quick question, is there a way to filter data from set days between 2 months?
E.g filter from 16-15 of Jan-Feb.
Is this possible?
For example i have used a measure to calculate days between dates
Last Assessment 16-15
= CALCULATE(SUM('Table1'[Duration1]),'Table1'[Start],
DATESBETWEEN('Calendar'[Date], [Assessment Date], [Assessment one month]))
Assessment Date = if(DAY(TODAY())<16,DATE(YEAR(TODAY()),MONTH(TODAY())-1,15),
DATE(YEAR(TODAY()),MONTH(TODAY()),15))
Assessment one month = EDATE([Assessment Date],-1)+1
Assessment 6 = EDATE([Assessment Date],-6)+1
Assessment 12 = EDATE([Assessment Date],-12)+1
The last assessment does show from the 16th of 2 months ago to last months 15th e.g Dec 16th - Jan 15th.
But i need to show from last 6 months and the last 1 year.
How can i work this out so i can show the Last 6 months and 1 year.
So far i have had to use a date filter to manually select the dates which i want to stop and have it be automatic.

If it is just the last 6 months or the last year you could make a custom column in the query editor (this would be the easiest way then). Like a filter flag:
'Includes the current month
Last 6 Months Flag = Date.IsInPreviousNMonths([YourDate], 6) or Date.IsInCurrentMonth([YourDate])
'Without the current month
Last 6 Months Flag = Date.IsInPreviousNMonths([YourDate], 6)
The same for last year:
Last Year Flag = Date.IsInPreviousYear([YourDate])
Drag and drop these custom columns as filter on your report and you are done.

consider following measure for Latest assessment (basically what you had before with EDATE for safe Dec/Jan handling and with variables for better performance)
Latest Assessment Date =
VAR __today = TODAY()
RETURN
IF (
DAY ( __today ) < 16,
DATE ( YEAR ( __today ), MONTH(EDATE(__today, - 1)), 15 ),
DATE ( YEAR ( __today ), MONTH ( __today ), 15 )
)
then you have this measure for handling 1/6/12 scenarios, you just replace 12 with whatever number of months you need
Measure 12 =
VAR __nrOfMonthsBack = 12
VAR __lastAssesment = [Latest Assessment Date]
RETURN
CALCULATE (
SUM ( Table1[Duration] ),
ALL('Calendar'), --this will reset any date filters coming from slicers
DATESBETWEEN (
'Calendar'[Date],
EDATE ( __lastAssesment, - __nrOfMonthsBack ) + 1,
__lastAssesment
)
)
EDIT: added ALL('Calendar') to disable filters coming from slicers

Related

Calculate Number of Working Days based on a Month

Using Power Bi desktop,
I need to create a query where the result will be the current month's working days.
For example July 2021 has 22 working days and so on.
My goal for achieving this will be to be to average the number of lines processed divided into the number of working days for the current month.
Will this be possible?
You can always get the job done only by creating just a single measure and nothing else like following
_count =
COUNTX (
FILTER (
VALUES('fact'[_factDate]),
WEEKDAY ( 'fact'[_factDate], 1 ) <> 1
&& WEEKDAY ( 'fact'[_factDate], 1 ) <> 7
),
'fact'[_factDate]
)
The minimum dependency of the calculations are having a calendar table like this
Calendar_Date
Calendar_Year
Calendar_Month
1/1/2000
2000
1
first create a calculated column in you calendar table (Dates for my case) with this below code-
Column = if(
FORMAT(Dates[date],"dddd") = "Saturday"
|| FORMAT(Dates[date],"dddd") = "Sunday",
0,
1
)
now create a measure as below-
weekday = sum(Dates[Column])
now, create visual with month and new measure.

How to create Last 12 months flag for grouped data in power bi

I have a table where where I want to create last 12 months flag column from the latest date available in each country. I have tried many Dax formula without success.
My table looks like below:
Here Y indicates the dates falls under last twelve months.
Please help me to find this flag.
You can create a calculated column to validate the date falls within the last 12 months, for each country. First calculate the maxDate for each country, then ensure the date is greater than the MaxDate with an if statement
Last 12 Months =
VAR MaxDate = CALCULATE (
MAX ( Country[Date] ),
ALLEXCEPT ( 'Country', 'Country'[Country] )
)
RETURN
IF (
Country[Date]> DATE(YEAR(MaxDate)-1, Month(MaxDate), Day(MaxDate)) ,1,0
)
This will then give you a filterable column to work from

PowerBI DAX: Date periods identification formula issue on previous 3 months

I have a sales data set and want to create a calculated column to be used as legend on graphs and also use as filter of dates but I'm struggling on with the part that identificate the previous 3 months before the last month.
My formula looks like this:
Period identification =
IF('Base'[Date]=CALCULATE(MAX('Base'[Date]);FILTER('Base';'Base'[FYTD]="FY1920"));"LM";
IF(
AND('Base'[Date]>=
DATEADD(CALCULATE(MAX('Base'[Date]);FILTER('Base';'Base'[FYTD]="FY1920"));-3;MONTH);
'Base'[Date]<CALCULATE(MAX('Base'[Date]);FILTER('Base';'Base'[FYTD]="FY1920")));"P3M";
IF('Base'[Date]=CALCULATE(MAX('Base'[Date]);FILTER('Base';'Base'[FYTD]="FY1819"));"LM YA";"OTHERS")))
The first IF() (Last Month - LM) and the last (Last Month Year Ago - LM YA) are working fine. I just can't find a way of PBI accept the kind of calculus I'm trying to do in the middle to identificate the previous 3 months before the last month.
*The [FYTD] is another calculated column that identificate the corresponding fiscal year of the [Date].[Month]
Thanks in advance
I think you can make this much easier using the handy EOMONTH function to move through time.
Try something like this where RefDate is the date that you want to use to calculate dates relative to. I'm using the end last month.
Period =
VAR RefDate = EOMONTH ( TODAY (), -1 )
RETURN
SWITCH (
TRUE (),
EOMONTH ( Base[Date], 0 ) = EOMONTH ( RefDate, 0 ), "LM",
Base[Date] > EOMONTH ( RefDate, -4 ), "P3M",
EOMONTH ( Base[Date], 0 ) = EOMONTH ( RefDate, -12 ), "LM YA",
"OTHERS"
)
Note:SWITCH(TRUE(),...) returns the result for the first condition that evaluates to TRUE().

Power Bi Dax: Filter between 2 dates of the previous month

I need some help with a filter.
The filter needs to filter date between the 16th of one month and the 15th of the next month, this should be for the previous months when looking at the data so if you look at it on March it will show data between 16th of Jan to 15th of Feb.
The data is 'Duration' of days which i will total up for the between 16th-15th period.
I have a measure for this but I'm not sure how it would go into my table visual to show the right data:
Assessment Date =
IF (
DAY ( TODAY () ) < 16,
DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ) - 1, 15 ),
DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 15 )
)
Is there a way I can put this measure into a new formula and do it like that? or is there another formula i could use?
You could use variables and try something like this:
Duration =
VAR Start1 =
DATE(
IF(MONTH(TODAY())<=2,YEAR(TODAY())-1,YEAR(TODAY())),
IF(MONTH(TODAY())=1,11,IF(MONTH(TODAY())=2,12,MONTH(TODAY())-1)),
16)
VAR End1 =
DATE(
IF(MONTH(TODAY())=1,YEAR(TODAY())-1,YEAR(TODAY())),
IF(MONTH(TODAY())=1,12,MONTH(TODAY())-1),
15)
VAR Calc =
IF(Table[Date]>=Start1 && Table[Date]<=End1,Table[Duration],0)
RETURN Calc
This would return values for only those rows which fall into the date filter. I am not sure if I got your date filter condition correctly. Either way, you should be able to adjust the calculation to get your desired results. Hope this helps.

Custom Calendar for sales data aggregated by weeks and months

I am struggling with creating Custom Calendar that would allow me to use time intelligence function on data that is already aggregated by weeks and months. The original table with transactions contains over 20M rows, so for performance and space saving reasons the grouping is necessary and I perform it while querying the database in SQL Server. I want to create:
Month vs Same Month Last Year
Week vs Same Week Last Year
Year-To-Date vs Last Year Year-To-Date (by Months)
Year-To-Date vs Last Year Year-To-Date (by Weeks)
My idea was to assign some dummy dates to each row of data, and then create custom calendar that would have each of these dummy dates along with other details. I just cannot figure out the key (how to create dummy date having Year number Month number and Week number - please keep in mind that for example Week 5 of 2020 is partially in January and partially in February)
Is it possible? Maybe I have to create 2 separate Calendars, one for Weeks and one for Months?
Add calculated table:
Calendar =
GENERATE (
CALENDAR (
DATE ( 2016, 1, 1 ),
DATE ( 2020, 12, 31 )
),
VAR VarDates = [Date]
VAR VarDay = DAY ( VarDates )
VAR VarMonth = MONTH ( VarDates )
VAR VarYear = YEAR ( VarDates )
VAR YM_text = FORMAT ( [Date], "yyyy-MM" )
VAR Y_week = YEAR ( VarDates ) & "." & WEEKNUM(VarDates)
RETURN
ROW (
"day" , VarDay,
"month" , VarMonth,
"year" , VarYear,
"YM_text" , YM_text,
"Y_week" , Y_week
)
)
You can customize it. In relation pane connect your Date field of FactTable (which is by week, as you say. This table can have missing dates and duplicate dates, to be precise) to the field Date of the Calendar table (which has unique Date, by day). Then, in all your visuals or measures always use the the Date field of the Calendar table. It is a good practice to hide the field Date in your FactTable.
More explanations here https://stackoverflow.com/a/54980662/1903793