Power Bi : non-unique value in dateadd() - date

I'm trying to create a metric to give me the number of request between two relative dates.
I tested this:
Request M =
VAR StartDate = DATEADD('Request'[request_date],-20,DAY)
VAR EndDate = DATEADD('Request'[request_date],0,DAY)
RETURN
calculate(sum('Request'[request_count]),DATESBETWEEN('Request'[request_date], StartDate, EndDate))
And I have the error : "A table of multiple values was supplied where a single value was expected "
My column request_date looks like this :
5 july 2022
5 july 2022
5 july 2022
5 july 2022
6 july 2022
6 july 2022
7 july 2022
...
--> All my dates are present more than once.
So I tried to add a "firstdate", like this:
VAR StartDate = DATEADD(FirstDate('Request'[request_date]),-20,DAY)
VAR EndDate = DATEADD(FirstDate('Request'[request_date]),0,DAY)
This time I have no error, but I don't get the right value. Could you help me please ?

Add a (unique) Date table to your model and setup a one-to-many relation between 'Date'[date] and your 'Request'[request_date]
Then use
Request M =
VAR StartDate =
DATEADD('Date'[date], -20, DAY)
VAR EndDate =
DATEADD('Date'[date], 0, DAY)
RETURN
CALCULATE(
SUM('Request'[request_count]),
DATESBETWEEN('Date'[date], StartDate, EndDate)
)

Related

Display Data between Dec Y-1 and Dec current Y in PowerBI

I am new in powerBI, I loonking to display data in during a closing period and in my case it is from :
31/12/Y-1 to 31/12/Y my issue it is with slicer the year filter all date in the current year and not taking into account the value in 31/12/Y-1
How can I set PowerBI to do so,
thanks for your support
Display Sum of data in a defined period 31/12/Y-1 till 31/12/Y, user can select the period that they want to display and the data will update via PowerBI
Slicer below :
Use this calculated column
Closing Period =
VAR thisYear = YEAR(TODAY())
VAR startDate = DATE(thisYear -1 , 12, 31)
VAR endDate = DATE(thisYear, 12, 31)
RETURN
IF(startDate <= 'Date'[Date] && 'Date'[Date] <= endDate, thisYear)
and filter the 'Date'[Closing Period] column on the current year.
You can use a new calculated column for the slicer in your case:
Filter_date = DATEADD ( 'Date Table'[date].[Date], +1, DAY )

Generate Series of month and years

How do I generate a series of month and year dates which I can use as a parameter to pass to other data tables.
For example, I am currently using:
MonthYearSelector = GENERATESERIES(DATE(2019,1,1), DATE(2019,12,1),30)
However, this generates the following dates:
01 January 2019
31 January 2019
02 March 2019
...
What I want is the following list:
01 January 2019
01 February 2019
01 March 2019
....
MonthYearSelector = GENERATESERIES(DATE(2019,1,1), DATE(2019,12,1),30)
How about something like this?
MonthYearSelector =
VAR Years = SELECTCOLUMNS( {2018, 2019}, "Year", [Value] )
VAR Months = SELECTCOLUMNS( GENERATESERIES(1, 12, 1), "Month", [Value] )
RETURN
ADDCOLUMNS(
CROSSJOIN( Years, Months ),
"Date", DATE( [Year], [Month], 1 )
)
In the above, I use SELECTCOLUMNS just to rename the default Value column name.
Try this out.. It's basically give you start of the month & End of the Month... your choice to use..
Start of Month Dates =
GENERATE (
GENERATESERIES(1,48),
VAR inc = [Value]
RETURN ROW (
"date", DATE(2017,inc,1),
"Month End",EOMONTH(DATE(2017,inc,1),0)
)
)
Check this for details

getting Date months/ rest of days difference

is there a way to get these information from two dates:
calculate month between dates ( month for me is a complete month)
calculate rest of days between dates
here is my example:
start date:
01/01/2014
end date:
21/02/2014
i need a resualt like this : months:1 days:20
onother example:
start date:
15/01/2014
end date:
10/03/2014
i need a resualt like this : months:1 days:25
Using Java8 Date/Time API you may do it like so,
LocalDate startDate = LocalDate.of(2014, 1, 1);
LocalDate endDate = LocalDate.of(2014, 2, 21);
Period period = startDate.until(endDate);
System.out.println("months: " + period.getMonths() + " days: " + period.getDays());

vbscript asp - Find every Thursday in a given month

I'm trying to find all Thursdays within a given Month using VBScript.
Can anyone help?
Here is one way;
base_date = cdate("21 aug 2011")
'get 1st thursday;
thurs = dateserial(year(base_date), month(base_date), 1)
if (weekday(thurs) <> 1) then thurs = 5 - weekday(thurs) + thurs
'loop subsequent;
do until month(thurs) <> month(base_date)
msgbox thurs
thurs = dateadd("d", 7, thurs)
loop
While the accepted answer does the job it's overly complicated for something that can be accomplished with WeekDay() function and a For loop.
Dim day
Dim startdate: startdate = CDate("21 aug 2011")
Dim enddate
'Get first day of month.
startdate = DateSerial(Year(startdate), Month(startdate), 1)
'Get last day of month.
enddate = DateAdd("m", 1, startdate) - 1
For day = startdate To enddate
If WeekDay(day) = vbThursday Then WScript.Echo day & " = " & WeekDayName(WeekDay(day))
Next
Output:
04/08/2011 = Thursday
11/08/2011 = Thursday
18/08/2011 = Thursday
25/08/2011 = Thursday
Any Date and Time constant can be used here to look for different or multiple weekdays with a little tinkering.

date format using Ajaxpro

I am using AjaxPro to retreive database, the date field return Sat Apr 3 00:00:00 UTC+0700 1982 i want to format it like "dd/mm/yyyy".
Assuming you are getting a DateTime value from the server, you can do something similar to this:
Visit http://www.ajaxpro.info/Examples/DataType/default.aspx
and run the following code in Firebug.
var result = AJAXDemo.Examples.DataType.Demo.GetDateTime(new Date()).value;
var year = result.getFullYear();
var month = result.getMonth() + 1;
var day = result.getDate();
var formattedDate = [day, month, year].join('/');
console.log(formattedDate);