Calculated Column in Power BI - Last 7 Days - powerbi

I have a table in Power BI with the date and I am trying to create a calculated column that indicates if that date is within the last 7 days. I understand the syntax of the IF statement, but am having trouble with the actual time calculation. Can anyone assist?

Use the DATEDIFF function to reliably determine the number of days (or weeks, or months, or years) between two dates:
IF(
DATEDIFF(MyTable[MyDate], TODAY(), DAY) < 7,
"Within last 7 days",
"Not within last 7 days"
)

Related

Power BI Measure to return date if the condition is true

I am struggling in writing a measure which returns me the list of dates if the condition is true.
For example, Last 7 days, I was able to do it in calculated column as below
Last 7 days = if(DateTime[Datetime]<=TODAY(),DateTime[DateTime])
The problem with the calculated column is when I am using this column as a filter, It is showing the dates that are not the last 7 days as blanks which I dont want. Please help.
Since you want to return a list of dates you need a Calculated Table:
Last 7 days =
FILTER(
'Datetime',
AND(
'Datetime'[Datetime] > TODAY() - 7,
'Datetime'[Datetime] <= TODAY()
)
)
You can use this table to filter your other data.

DAX - Creating a rolling "within last 7 days" TRUE/FALSE column in Date Dimension table based on latest data run date in Fact table

I know this should be really straightforward, but cannot figure this out. I am wanting to create a rolling "within last 7 days" TRUE/FALSE column in Power BI within my Data Dimension table to allow me to use this as a filter for other calculations - this will be based on the latest data run date in my Fact table (which can be delayed until the afternoon every day it is ran, hence why I am not just simply using the TODAY() function).
I have tried to use the DATEDIFF function when creating the column, which works fine when I test this within my fact table (as the MAX run date is the last run date in the table). However, when referencing my Date Dimension table, it gives me TRUE values for everything up to 7 days prior to the Latest Run Date', but also for every date in the future past the Latest Run Date. My Date Dimensioning table has dates for the next 5 years, hence why this is an issue.
Rolling 7 Days = if (DATEDIFF('Fact Table'[Date], MAX('Fact Table'[Date]), DAY) <= 7, TRUE(), FALSE())
How can I re-write this so that it explicitly gives a TRUE result for ONLY the last 7 days from the latest data run date so I can use it as a filter?
Could this even be wrote as a measure?
Thanks in advance!
Try using two conditions instead of just one.
For example,
Rolling 7 Days =
VAR MaxFactDate = MAX ( 'Fact Table'[Date] )
RETURN
( dimDate[Date] > MaxFactDate - 7 ) && ( dimDate[Date] <= MaxFactDate )

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.

Power Bi DAX: Divide Value and set it for each week

I have a target value of 20 for January but it is 20 for the month, i need to show this over each week. I have to divide the target by 4 if there are 4 weeks in a month and by 5 if there are 5 weeks in a month. It is as simple as that, i am using a line and clustered column chart to display my data, i need the target spread out into each week of the month. I also need another field to do this but im sure i can replicate your formula and make it applicable.
I have added a WeeksinMonth column that counts how many weeks are in a particular month e.g January has 5 weeks and February has 4 weeks. I need an IF statement that will divide the target and value by how many weeks in a month. e.g if month has 5 weeks then divide target and value by 5, if month has 4 weeks divide target and value by 4.
I have a calendar table with week values which i can used to put the divided target into and also the desired output i made in excel (See attached).
How will i go about this?
Desired Output
Calendar Table
enter code hereYou can create an extra column in your calendar table:
WeekMax =
var stOfMonth = Weeks[Start of Week].[Month]
var stOfYear = Weeks[Start of Week].[year]
return CALCULATE(max(Weeks[Weekinmonth]);FILTER(Weeks;Weeks[Start of Week].[Month] = stOfMonth && Weeks[Start of Week].[Year] = stOfYear))
Make sure you have a relation in your model between calendar and target date. Now you can use this number for that week/date to divide by.
You can now add an extra column in your target table:
WeeklyTarget = Targets[Target]/Related(Calendar[WeekMax])

DAX Looping Variable

I'm building a Power BI dashboard that includes actual cost vs budget. Summarizing the cost is not a problem. The budget amount is entered in a table as a weekly amount, for example $1,000. We use a custom accounting calendar that dictates the number of weeks per month. The second month of each quarter is 5 weeks while all other months are 4 weeks. Here's what it looks like:
January is 4 weeks
February is 5 weeks
March is 4 weeks
April is 4 weeks
May is 5 weeks
June is 4 weeks
July is 4 weeks
August is 5 weeks
September is 4 weeks
October is 4 weeks
November is 5 weeks
December is 4 weeks
I need a looping variable in DAX that will simply count the number of weeks based on the months chosen with the month slicer. Then I can multiply the number of weeks by the weekly budget amount.
Here's a practical example:
If I choose Jan, Feb, Mar, Apr then the number of weeks should be 17 weeks. And the budget amount should be $17,000.
I figured this out in Crystal Reports. Here's the formula I wrote:
local datetimevar startdate:= {?BegMonth};
local numbervar numofWeeks;
While startdate <= {?EndMonth}
Do(
if month(startdate) in [2,5,8,11] then
numofWeeks := numofWeeks + 5
else
numofWeeks := numofWeeks + 4;
startdate := DATEADD("m",1,startdate);
);
numofWeeks
I've tried several approaches with DAX but I got nothing to work.
DAX doesn't exactly do looping. It does aggregations on filtered columns.
If you don't have a Month column, then create a calculated column.
TableName[Month] = MONTH ( TableName[Date] )
Once you have that, adding up weeks can be done with an iterator function.
Weeks =
SUMX (
VALUES ( TableName[Month] ),
IF ( TableName[Month] IN { 2, 5, 8, 11 }, 5, 4 )
)
The VALUES function is a list of unique values in the filter context, so if you selected Jan - Apr, then it would be {1,2,3,4}. Then SUMX iterates ("loops") over those four values and add 5 or 4 depending on the condition specified in the IF function.