MinIfs syntax with conditional time/date - excel

I'm trying to use the MinIfs method of the WorksheetFunction object to determine a conditional minimum.
Adding another condition makes my code fail, resulting in 0, which suggests that the last condition is not satisfied in the used range. But in fact it is.
What I need help with is the correct syntax of the last condition.
What works:
WorksheetFunction.MinIfs(wsSource.Columns(34), wsSource.Columns(36), False, wsSource.Columns(14), False, wsSource.Columns(3), wsL.Cells(j, 13).Value)
What doesn't:
WorksheetFunction.MinIfs(wsSource.Columns(34), wsSource.Columns(36), False, wsSource.Columns(14), False, wsSource.Columns(3), wsL.Cells(j, 13).Value, wsSource.Columns(34), """>""&" & startTime)
The variable startTime is defined as a date, and contains date and time information. Column 34 contains date and time information in custom format that looks like this: 2021.01.04 22:20:07.
What the code is trying to do is to find the earliest time and date that fits the other criteria after a certain time of that specific day (stored in startTime).
Changing format, however, shouldn't matter, and in fact it doesn't: I changed the source's format to number and it hadn't worked either. I tried to replace startTime with CDbl(startTime) as well.

Related

Power Automate - check if valid date

I have a Power Automate flow that calls on an Office Script to read a specific cell in an Excel workbook. That cell is supposed to have a properly formatted date. If it is properly formatted, in order for Power Automate to read that, I set a variable with this expression:
addDays('1899-12-30', int(outputs('Run_script_2')?['body/result/DeliveryDate']), 'MM/dd/yy')
However, if the output of that script isn't an expected value (i.e. 8.9.2022 instead of 8/9/2022), the flow breaks when trying to run that expression. How can I write an expression that doesn't fail if there isn't the expected Excel-type date? I'd like the expression to equate to null if it's not able to calculate an actual date value.
You can check the NumberFormatCategory, or the specific NumberFormat of the cell to check if it is formatted as a date.
if (selectedSheet.getRange("A1").getNumberFormatCategory() == ExcelScript.NumberFormatCategory.date)
...
or
if (selectedSheet.getRange("A1").getNumberFormatLocal() == "m/d/yyyy")
...
You can return this boolean to Power Automate as well and build your expression accordingly.
Here is how I solved this issue, because my users use the excel date field for more than a date, such as entering "NA", "Not Known", "TBD", etc.
Use the INT() function on the date value. Configure the next step using the "Configure run after" setting to be "Failed" only. Configure the step after that to be "is skipped".
In my case, in the failed case, I simply copy the excel value into a string I'll use for my output file. If the INT() succeeds, I run a condition statement because I want to blank out future dates but leave past dates
You can try using JavaScript's Date class to convert the date in the cell. When you create an instance of the date object, you provide it with the date value from the cell in its constructor. You can then call the toLocalDateString() method of the date object and return that. You can see an example of how to do that below:
function main(workbook: ExcelScript.Workbook) {
let selectedSheet: ExcelScript.Worksheet = workbook.getActiveWorksheet();
let rang: ExcelScript.Range = selectedSheet.getRange("A1") //cell contains the value 8.9.2022
rang.setNumberFormat("mm/dd/yyyy")
let date: Date = new Date(rang.getValue() as string);
return date.toLocaleDateString();
}

Pyspark conditionally replace value in column with value from another column

I am working with some weather data that is missing some values (indicated via value code). For example, if SLP data is missing, it is assigned code 99999. I was able to use a window function to calculate a 7 day average and save it as a new column. A significantly reduced example of a single row is shown below:
SLP_ORIGIN
SLP_ORIGIN_7DAY_AVG
99999
11945.823516044207
I'm trying to write code such that when SLP_ORIGIN has the missing code it gets replaced using the SLP_ORIGIN_7DAY_AVG value. However, most code explains how to replace a column value based on a conditional with a constant value, not the column value. I tried using the following:
train_impute = train.withColumn("SLP_ORIGIN", \
when(train["SLP_ORIGIN"] == 99999, train["SLP_ORIGIN_7DAY_AVG"]).otherwise(train["SLP_ORIGIN"]))
where the dataframe is called train.
When I perform a count on the SLP_ORIGIN column using train.where("SLP_ORIGIN = 99999").count() I get the same count from before I attempted replacing the value in that column. I have already checked and my SLP_ORIGIN_7DAY_AVG does not have any values that match the missing code.
So how do I actually replace the 99999 values in the SLP_ORIGIN column with the associated SLP_ORIGIN_7DAY_AVG value?
EVEN BETTER, is there a way to do this replacement and window calculation without making a 7 day average column (I have other variables I need to do the same thing with so I'm hoping there is a more efficient way to do this).
Make sure to double check with dataframe you are verifying on.
I was using train.where("SLP_ORIGIN = 99999").count() when I should have been using train_impute.where("SLP_ORIGIN = 99999").count()
Additionally, instead of making a whole new column to store the imputed 7 day average, one can only calculate the average when the missing value code is present:
train = train.withColumn("SLP_ORIGIN", when(train["SLP_ORIGIN"] == 99999, f.avg('SLP_ORIGIN').over(w)).otherwise(train["SLP_ORIGIN"]))\

Changing format of TODAY() in excel

I'm using today to aquire todays date and then adding a static value to the end of it using the following:
=TODAY()&"T23:00:00"
Which Returns 43202T23:00:00
I really need it in the format 2018-04-12T23:00:00
Any help on this would be great!
There are a couple ways to accomplish this, depending on whether your goal is a formatted String (to display) or a numeric value (such as data type Date) for storing or using with calculations.
If you want a formatted date/time result (to display to the user)...
Use the TEXT worksheet function:
=TEXT(TODAY(),"yyyy-mm-dd")&"T23:00:00"
...the reason this works is because TODAY() returns a Date data type, which is basically just a number representing the date/time, (where 1 = midnight on January 1, 1900, 2 = midnight on January 2, 1900, 2.5 = noon on January 2, 1900,etc).
You can convert the date type to a String (text) with the TEXT function, in whatever format you like. The example above will display today's date as 2018-04-12.
If, for example, you wanted the date portion of the string displayed asApril 12, 2018 then you would instead use:
TEXT(TODAY(),"mmmm d, yyyy")
Note that the TEXT worksheet function (and VBA's Format function) always return Strings, ready to be concatenated with the rest of the String that you're trying to add ("T23:00:00").
If you want to use the result in calculations...
If you instead want the result to be in a Date type, then instead of concatenating a string (produced by the TEXT function) to a string (from "T23:00:00"), you could instead add a date to a date:
=TODAY()+TIME(23,0,0)
or
=TODAY()+TIMEVALUE("23:00")
..and then you can format it as you like to show or hide Y/M/D/H/M/S as necessary with Number Formats (shortcut: Ctrl+1).
More Information:
MSDN : TEXT Function (Excel)
MSDN : TIMEVALUE Function (Excel)
MSDN : TIME Function (Excel)

If statement depending on time of day

I need to use a specific time, so far I had this
=IF(AND(TIME(15,45,0)<=(AW15=$A$11,$A$13)),IF(AND(TIME(15,45,0)>=(AW16=$A$13,$A$15))
So if before 15:45:00 I need for it to equal this logical test AW15=$A$11,$A$13 and if after 15:45:00 to equal to AW16=$A$13,$A$15.
Hope it makes sense and thanks in advance.
So it sounds like you are trying to write a nested IF statement. That is, IF A is true, THEN IF B is also true THEN return result 1, otherwise (A true, but B not true) return result 2, otherwise (A not true, B not tested) return result 3.
In Excel, this would be written as follows:
=IF(Parameter1=Condition1,IF(Parameter2=Condition2,Result1,Result2),Result3)
Applying it to your scenario, I think you are aiming for this:
=IF(TIME(HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())) < TIME(15,45,0),
IF(AW15=$A$11,$A$13,"Condition1.2"),
IF(AW16=$A$13,$A$15,"Condition2.2"))
Note, some scenarios have not been covered by your statement, so I have written "Condition1.2" and "Condition2.2" which you can replace with additional tests or results to return.
Condition1.2 is where the time is before 15:45, but AW15 did NOT equal A11.
Condition2.2 is where the time is at or after 15:45, but AW16 did NOT equal A13.
You don't have to put anything in those placeholders if you don't want to, but if either of those conditions are ever met then the formula will simply return "FALSE".
Also, if you do not want the test time to be NOW(), then you will need to reference another cell that contains a fixed timestamp for when the row is being worked. NOW() is volatile, which means if you save the spreadsheet before 15:45 but then open it again after 15:45, the results you had already calculated will all change.
If i understand your question correctly, you need to compare the time now to 15:45:00 and make a selection based on that. If so the solution is:
=IF(TIME(HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())) < TIME(15,45,0),
AW15=$A$11,$A$13, AW16=$A$13,$A$15)
This translates to: If the time now is before 15:45:00 then do AW16=$A$13,$A$15 else do AW16=$A$13,$A$15
If you want to compare the time in a specific cell then substitute the TIME(HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())) with a cell which has a time for example:
=IF(A1 < TIME(15,45,0),
AW15=$A$11,$A$13, AW16=$A$13,$A$15)

How to convert and compare a date string to a date in Excel

= "7/29/2011 12:58:00 PM" > NOW()
I'd like this expression to return FALSE and yet it returns TRUE.
I know I can break apart my datetime into a date and a time and add them together as follows:
= DateValue("7/29/2011") + TimeValue("12:58:00 PM") > NOW()
But, this seems inelegant to me. I want a simple function or approach that looks nice and I feel certain that it's out there but I just can't find it.
I also know there is a VBA function called CDate which can typecast the string into a datetime and that would be perfect. But, I don't see how to call a VBA function in an excel cell.
Multiply the string by one and the comparison function will work:
= 1*"7/29/2011 12:58:00 PM" > NOW()
The answer to your question is tightly related to #Jean-François's comment: Why is the date being interpreted by Excel as a Text and not by a date?
Once you find it out, you'll be able to do the comparison.
If that's because the string is being retrieved as a text, you can simply multiply it by one and the comparison function will work, then. But it applies only in case the string format is a valid date/time format in your regional settings.
You could wrap the VBA call in a custom function:
Function ReturnDate(ByVal datestr As String) As Date
ReturnDate = CDate(datestr)
End Function
which you can use just like a formula in your sheet.
I'm upgrading the following from a comment to an answer:
Unless you have a very specific reason to do so (and right now I can't think of any), dates (and other values) really shouldn't be "hard-coded" in cells as strings like you show. Hard-coding the string like that makes it invisible and inflexible. The user will just see TRUE or FALSE with no indication of what this means.
I would just put your date 7/29/2011 12:58:00 PM in a cell on its own e.g. A1, and set the cell's format to some date format. Then you can say = A1 > NOW().
Contrary to #jonsca's and #Tiago Cardoso's answers, this answer doesn't address your specific question, but then again, what you are asking seems like really bad practice to me!
The simplest way to do this is to make a VBA function that uses CDATE and return your comparison. Then, call the function from an excel cell.
The VBA Function
Public Function compareDate(ByVal inputDate As String) As Boolean
compareDate = CDate(inputDate) > Now()
End Function
Then in your spreadsheet, just do
=compareDate("YOUR DATE")
The function will return "FALSE" if it is older and "TRUE" if it is newer than Now()

Resources