All, I am trying to isolate the time portion only of a date/time stamp and then create a case statement to populate another column with data. This is how the date/time stamp looks: 2022-03-25 04:00:00
Here is the code I originally wrote but when I just use the hours portion I get an error in PowerBi saying invalid date/timestamp...
case when create_tmstp between '04:00:00' and '07:30:00' then 'N' else 'D' end as Day_Night,
From the looks of it, you're using SQL, not DAX. I say that, because you've tagged this with dax and not SQL.
Problem is that you're not showing us what create_tmsp is, or what version of what kind of sql server you're using. So, just to make it easier, this is an working example to use for instance inside SSMS. This uses getdate() to get the current datetime, and then converts it into just showing what hour it is (0-24), then there is a case when that checks which intervall it falls under. You can use this, and simply replace getdate() with the column you wish to use if you're using a server that accepts this syntax.
select
DATEPART(HOUR,getdate()) AS 'HOUR',
getdate() AS 'Datetime',
CASE WHEN DATEPART(HOUR,getdate()) BETWEEN 9 AND 16
THEN 'Day'
WHEN DATEPART(HOUR,getdate()) BETWEEN 17 AND 24
THEN 'Evening'
WHEN DATEPART(HOUR,getdate()) BETWEEN 0 AND 4
THEN 'Night'
WHEN DATEPART(HOUR,getdate()) BETWEEN 5 AND 8
THEN 'Morning'
END AS 'Label'
Example result:
Hour
Datetime
Label
0
2022-04-04T00:18:13.203Z
Night
Fiddle here:
http://sqlfiddle.com/#!18/56f78/3829
Related
I have a simple if Statement in Power Query I feel I need a separate set of eyes on.
I am trying to say if the current day of the week is Monday then I want the system to display whatever the date was 3 days ago, else any other day just show yesterdays date. My formula below is receiving the error "Token RightParen expected." at the "Date" after "then" on the 2nd line.
if(Date.DayOfWeek(DateTime.FixedLocalNow()) = 0 then
Date.AddDays(Date.From(DateTime.FixedLocalNow()),-3) else
Date.AddDays(Date.From(DateTime.FixedLocalNow()),-1))
A few extra characters. Remove first and last parenthesis
let Source = if
Date.DayOfWeek(DateTime.FixedLocalNow()) = 0 then
Date.AddDays(Date.From(DateTime.FixedLocalNow()),-3) else
Date.AddDays(Date.From(DateTime.FixedLocalNow()),-1)
in Source
There is a large table containing among other fields the following:
ID, effective_date, Expiration_date.
expiration_date is datetime20. format, and can be NULL
I'm trying to extract rows that expire after Dec 31, 2014 or do not expire (NULL).
Adding the following where statement to the proc sql query gives me no results
where coalesce(datepart(expiration_date),input('31/Dec/2020',date11.))
> input('31/Dec/2014',date11.);
However, when I only select NULL expiration dates and add the following fields:
put(coalesce(datepart(expiration_date),input('31/Dec/2020',date11.)),date11.) as value,
put(input('31/Dec/2014',date11.),date11.) as threshold,
case when coalesce(datepart(expiration_date),input('31/Dec/2020',date11.)) > input('31/Dec/2014',date11.)
then 'pass' else 'fail' end as tag
It shows 'pass' under TAG and all the other fields are correct.
This is an effort to duplicate what I used in SQL Server
where isnull(expiration_date,'9999-12-31') > '2014-12-31'
Using SAS Enterprise Guide 7.1 and while trying to figure it out I've been using
proc sql inobs=100;`
What am I doing wrong ? Thank you.
Some Expiration Dates:
30OCT2015:00:00:00
30OCT2015:00:00:00
29OCT2015:00:00:00
30OCT2015:00:00:00
I would recommend using a date constant ("31DEC2014"d) rather than date functions, or else either use explicit passthrough or disable implicit passthrough. Date functions are challenging when going between databases and so avoiding them when possible is best.
I have a column of time data in 24 Hr time:
Time
625
844
1241
1566
Where 625 is 06:25 AM.
How can I convert this into a usable form for PowerBI? I tried the following M query in the advanced editor
Column = CONCATENATE(CONCATENATE(LEFT([Time],LEN([Time])-2),":"),RIGHT([Time],2))
but receive the following message:
Expression error: The name 'CONCATENATE' wasn't recognized. Make sure it's spelled correctly.
Thank you,
This can be done easily using DAX by creating a new column in you model and use this expression:
=FORMAT(TimeTable[time];"00:00")
You can set type and format to the column:
This will return something like this:
I hope 1566 time value is a typo in your question.
Note I am using Excel with PowerPivot but this approach can be applied to Power BI too.
Let me know if this helps.
M queries are not written like Excel or DAX formulas. Look at this site to learn more about how to write M queries.
In your case, you could add this formula in a custom column (assuming your times are in Column1):
Time.ToText(#time(Number.RoundDown([Column1] / 100), Number.Mod([Column1], 100), 0), "hh:mm tt")
If you want to keep it as a time value, then remove the Time.ToText function in the example above.
The database (SQLite) has a field of type REAL with the values of the form (42153.659595).
How to translate this value in the form "dd.MM.yy HH:mm:ss" if 42153.659595 = 29.05.2015 15:49:49 ?
You can be explicit about what calendar system you require: http://www.sqlite.org/lang_datefunc.html
SELECT julianday('now') - julianday('1776-07-04');
In principle just don't "parse" (you mean: interpret raw representation). Use Sqlite API/builtin SQL functions to do it for you
In the interest of information:
The date and time functions use a subset of IS0-8601 date and time formats.
The datetime() function returns "YYYY-MM-DD HH:MM:SS". The julianday() function returns the Julian day - the number of days since noon in Greenwich on November 24, 4714 B.C. (Proleptic Gregorian calendar)
The date() function returns the date in this format: YYYY-MM-DD. The time() function returns the time as HH:MM:SS.
The correct value is achieved using the API/builtin SQL functions (42153.659595 - value from database):
SELECT datetime(julianday(42153.659595, 'localtime') + 2415018.29167) AS DT;
Output:
DT |
2015-05-29 15:49:49|
Constant 2415018.29167 was selected manually and query:
SELECT datetime (2415018.29167);
returns the current Greenwich Mean Time.
I work with a third-party application and documentation on the database is missing. Perhaps this strange decision, but it works. Thank you all for answers.
We have a staging table that's used to load raw data from our suppliers.
One column is used to capture a time-stamp but its data-type is varchar(265). Data's dirty: about 40% of the time, there is garbage data, otherwise time-stamp data like this
2011/11/15 20:58:48.041
I have to create a report that filters some dates/timestamps out that column but where I try to cast it, I get an error:
db2 => select cast(loadedon as timestamp) from automation
1
--------------------------
SQL0180N The syntax of the string representation of a datetime value is incorrect. SQLSTATE=22007
What do I need to do in order to parse/cast the timestamp string?
The string format for a DB2 timestamp is either:
'2002-10-20-12.00.00.000000'
or
'2002-10-20 12:00:00'
You have to get your date string in either of these formats.
Also DB2 runs on a 24 hour clock even though the output sometimes uses a 12 hour clock (AM / PM)
So '2002-10-20 14:49:50' For 2:49:50 PM
Or '2002-10-20 00:00:00' For midnight. Output would be 12:00:00 AM
It seems you have a lot of garbage data, so firt of all you should check if the data is a valid timestamp in the format you expect ('2011/11/15 20:58:48.041'). We could use a simple solution - just replace all digits with '0' and check the result format:
TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
If the format is the expected one, you should convert to DB2 timestamp. In DB2 for iSeries there is a build-in function since V6R1 TIMESTAMP_FORMAT. In your case it will look like that:
TIMESTAMP_FORMAT('2011/11/15 20:58:48.041','YYYY/MM/DD HH24:MI:SS.NNNNNN')
So the solution query combined should look something like that:
SELECT
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END
FROM
your_table_with_bad_data
EDIT
I just saw your comment that provider agreed to clean the data. You could use the solution provided to speed up the process and clean the data by yourself:
ALTER your_table_with_bad_data ADD COLUMN clean_timestamp TIMESTAMP DEFAULT NULL;
UPDATE your_table_with_bad_data
SET clean_timestamp =
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END;