Why can't I use the #duration function in Power query in Power BI? - function

I'm trying to create a new personalized column in Power Query using the #duration function because I need a duration value from days, hours, minutes, and seconds.
I already have three columns that represents hours, minutes and seconds named 'Hours', 'Minutes' and 'Seconds'.
On my custom column formula I did:
= #duration(0,[#'Hours'],[#'Minutes'],[#'Seconds']) as duration
and it says 'invalid identifier'.
Why can't I use this function? Please, help. I am new at Power BI

Related

How do I tell google sheets to only make a calculation after a certain time period?

Context
So I am currently building a database of data for financial assets to conduct some machine learning from to build trading signals. I am trying to calculate the geometric mean but over a given period (monthly). I want to tell google sheets to only calculate the geometric mean after every month. I tried using this formula to no avail:
=IF(last date of the month - first date of month = total days in a month,
GEOMEAN(filter(abs(range),abs(range)>0)),""))
** There were values in the last date of the month - first date of month = total days in a month **
It ended up doing it for every day for the 10 year data set.
** Update
This is the data:
Date Close Cleaned Data Returns Gross returns Geometric average returns
13/11/2015 280 -0.0267 0 1
16/11/2015 280 -0.0267 0 1
17/11/2015 280 -0.0267 0 1
...
23/12/2016 296.4 0.0236 -0.1561348935 0.8438651065
28/12/2016 295.2 0.0199 -0.0770931339 0.9229068661
29/12/2016 294.7 0.0183 0.03341318035 1.03341318
30/12/2016 294.9 0.0190 0.3718276303 1.37182763
Problem (UPDATE)
How do I create a function to let google sheet do calculations only for the last day of every month for a given time series data? Say within this time period, (1 year) I want to calculate the geometric mean for each month in this period and for new data I might want to add later in the future.
To do this you will have to set a trigger event. This is found within the script editor under the edit tab, second to last option.
Image of where to find the trigger manager: It's in spanish, but it will be found in the same place
Once there, click on add trigger, which will be found on the bottom right corner. The first option will ask you which function do you want to run from the bound script. Then select the source of the event and select according to time (My platform is in spanish so I'm trasnlating it you might have it written differently). Then it will prompt you: if you want it to be at an exact date and time, every minute, every hour, day, week and month. Select month and select the day of the month you want the trigger to happen in the next prompt and select the time for the last prompt, then click save.
Finding the last day of a month:
function lastdayOfMonths() {
let html='';
let td=new Date();
for(var i=0;i<12;i++) {
let dt=new Date(td.getFullYear(),td.getMonth()+i+1,0);
let dts=Utilities.formatDate(dt, Session.getScriptTimeZone(),"E MMM dd,yyyy");
html+=Utilities.formatString('<br />%s',dts);
}
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), "Last Days of Months for next year");
}

Set last month ending balance to next month open balance: Power Query M Function

Using a Power Query M function, I am trying to set current month's ending balance to be next month's opening balance. I have tried various different date functions (i.e. Date.IsInPreviousMonth) but cannot get this to work.
The attached image shows what I am trying to accomplish using Power Query M formula.
This is possible with M but is quite complex. Here is a PowerBI Community post that gives options.
I would prefer to use DAX's PREVIOUSMONTH function as opposed to M.
Create a new measure along the lines of:
Previous Month Balance =
CALCULATE(SUM('Fact - Sales'[Cost Price]),
PREVIOUSMONTH('Dimension - Calendar'[Date]))

Remove Enter Parameter Value

I'm trying to calculate the time difference between two variables. The first variable is checkin_time and the second variable is triage_time. Both variables contain values with the same format, for example... 12/31/2018 3:24:00 PM.
So in theory I would be subtracting Checkin_time (12/31/2018 3:24:00 PM) from Triage_time (12/31/2018 4:24:00 PM) to get 60 minutes.
I'm using the following code to calculate the time difference
Select DateDiff(Minute,CHECKIN_TIME,triage_time) AS triagetime
From TAT_Import
but when executing this query I'm getting an 'enter paramteer value' pop up box and it's asking me to enter a value for minutes.
How do I run my query without having to enter a value for minutes in the pop up box?
Thanks!
The DateDiff function requires a string as the first argument, hence your code should be:
select DateDiff("n", CHECKIN_TIME, triage_time) AS triagetime
from TAT_Import

Interesting SSRS parameter discovery when using subscriptions

I have recently been putting together an SSRS report that will run every 15 minutes for the previous 15 minute 'chunk' of time. In essence a very straight forward and simple report that will run via an automated subscription.
I was using Microsoft SQL Server 12 Report Builder Version 3.
I was alerted to an issue with the output csv when my recipient reported being sent blank files, most odd considering the report generated as expected when run manually.
Long story short, it was the expressions I was using to generate the From and To dates. Manual runs produced data, subscription runs did not.
Original parameters
FromDate
dateadd(DateInterval.Second, (second(now()) + 900) * -1, dateadd(Dateinterval.Minute, (minute(now()) mod 15) * -1, now()))
ToDate
dateadd(DateInterval.Second, (second(now()) + 1) * -1, dateadd(Dateinterval.Minute, (minute(now()) mod 15) * -1, now()))
New Parameters
FromDate
dateadd(DateInterval.Minute, -15, dateadd(DateInterval.Minute, cint(datediff(DateInterval.Minute,today(),now()) / 15) * 15, today()))
ToDate
dateadd(DateInterval.Second, 899,Parameters!FromDate.Value)
Thought I would post this here for two reasons
Theories as to why
It might help someone in the future
Your original parameters take Now and subtract the minutes to arrive at a 15-minutes time, then they take the seconds of another Now (which is later and could be in the next second or even minute) and subtract that value to arrive at a 0-second time (or a 59-second time). This could already cause a problem when there is a change of seconds between the first and the second Now, which isn't very unikely, as on my test system there were 0.59 seconds between the two evaluations of Now in the FromDate parameter. Also, the Now value is more accurate than just a second, and your formula does not respect that. Therefore, if the records you are trying to process in your report happen to have a time of exactly a quarter of an hour, the first parameter is for sure greater (by maybe 0.01 second) and so the record is ignored.
Your formula for the "new parameters" does not depend on the seconds of Now() and will always return a time with no fraction of a second, so I guess that that's what makes the difference.
The expression for the FromDate could be simplified a little:
=Today.AddSeconds(900*(DateDiff(DateInterval.Minute, Today, Now)\15-1))
If you do not plan to run the report very short before midnight, there should not be a problem caused by a change of the day during the evaluation of Today and Now, and you could calculate the second parameter in a similar way, independently from the first one:
=Today.AddSeconds(900*(DateDiff(DateInterval.Minute, Today, Now)\15)-1)
The original parameter values were being calculated individually which means they would each have slightly different values for Now(). I know this is a long shot, but it's a theory. If the subscription job fired off a fraction of a second before a 15 minute interval, it's possible that the ToDate returned just before the FromDate. This would result in an invalid date range.
With the new expressions, the ToDate is referencing the FromDate which forces them to be calculated in sequence, not in parallel. Not to mention you're adding to the FromDate which also forces the date range to have a consistent length. However, you may still run into a case where you get the same report twice if the FromDate is calculated on the wrong side of a 15 minute cutoff.
One way to test/avoid this issue would be to offset the subscription time so that it doesn't actually try to fire at the exact 15 minute cutoffs. For example, you could have it scheduled to go off 1 minute afterwards.

insert number of hours mysql database

I wanna enter the number of hours of an aircraft, using Visual Basic, into a Mysql database.
For example:
Aircraft X has 3158:25 hours
This means it has 3158 hours and 25 minutes.
In my database I've declared Number of Hours as Time (format).
The problem is that Visual Studio returns the following error:
Incorrect time value: '3425:25' for column 'NbreHours' at row 1
3425:25 is not a correct Time format, change the format of your NbreHours column to nvarchar(10) and that will solve your issue.
The meaningful way to save a time in the hh:mm format is to calculate the total minutes of your record, and then to save it in the database as an int.
First sum the total minutes of your record:
string time = "3158:25:00";
double minutes= TimeSpan.Parse(time).TotalMinutes;
Then save the minutes variable in the database as an int.