Power Query convert from Date to MMM-YY and QYY format - powerquery

I was wondering if you could help me to convert the data format per below:
01/01/2018 to Jan 18
09/30/2018 to Q3 18
=Table.AddColumn(#"Removed Columns2", "Custom", each Date.ToText([Report Date],"MMM")&"-"&Date.Year([Report Date],"YY"))
This is what I have tried so far and no results:
Thank you very much for your help!

You're nearly there.
Try
Custom1 = Table.AddColumn(#"Removed Columns2", "MMM-YY", each Date.ToText([Report Date],"MMM-yy")),
Custom2 = Table.AddColumn(#"Custom1", "QYY", each Number.ToText(Date.QuarterOfYear([Report Date])) & Date.ToText([Report Date], "yy"))

Related

DAX number of days

Can you please help how I can return the total number of days for each month in a given quarter?
For example, I already have 92 days for December 2025 but how can I show 92 days for October and November as well?
If you want only a count, you can use measure:
QuarterDays = calculate(countrows(VALUES('Calendar'[Date])), FILTER(ALL('Calendar'), selectedvalue('Calendar'[Year]) = 'Calendar'[Year] && selectedvalue('Calendar'[Quarter]) = 'Calendar'[Quarter] ))

time data doesn't match format specified

I am trying to convert the string to the type of 'datetime' in python. My data match the format, but still get the
'ValueError: time data 11 11 doesn't match format specified'
I am not sure where does the "11 11" in the error come from.
My code is
train_df['date_captured1'] = pd.to_datetime(train_df['date_captured'], format="%Y-%m-%d %H:%M:%S")
Head of data is
print (train_df.date_captured.head())
0 2011-05-13 23:43:18
1 2012-03-17 03:48:44
2 2014-05-11 11:56:46
3 2013-10-06 02:00:00
4 2011-07-12 13:11:16
Name: date_captured, dtype: object
I tried the following by just selecting the first string and running the code with same datetime format. They all work without problem.
dt=train_df['date_captured']
dt1=dt[0]
date = datetime.datetime.strptime(dt1, "%Y-%m-%d %H:%M:%S")
print(date)
2011-05-13 23:43:18
and
dt1=pd.to_datetime(dt1, format='%Y-%m-%d %H:%M:%S')
print (dt1)
2011-05-13 23:43:18
But why wen I using the same format in pd.to_datetime to convert all the data in the column, it comes up with the error above?
Thank you.
I solved it.
train_df['date_time'] = pd.to_datetime(train_df['date_captured'], errors='coerce')
print (train_df[train_df.date_time.isnull()])
I found in line 100372, the date_captured value is '11 11'
category_id date_captured ... height date_time
100372 10 11 11 ... 747 NaT
So the code with errors='coerce' will replace the invalid parsing with NaN.
Thank you.

(Swift) Using NSDataFormatter with a medium style string to get date

I seem to be stuck here and I have been wasting way too much tome on this.
What I have is a string that is in the RFC 1123 format that I would like to get a date out of, but not matter what I do, I get a nil result;
let dateFormat = NSDateFormatter();
dateFormat.dateStyle = .MediumStyle;
dateFormat.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss z";
dateFormat.locale = NSLocale.systemLocale();
dateFormat.timeZone = NSTimeZone(abbreviation:"GMT");
var currentDate = dateFormat.dateFromString("Sun, 28 Jun 2015 04:30:54 GMT");
I am not sure what I am missing, if I changed the MMM to MM and make Jun 06, then it works. It seems to be only this instance. I have tried moving the order of how dateFormat gets created, and still I get no results. Any help on this matter would greatly be appreciated
I think you have confused the formatter. You don't need to set anything except the format string, because the formatter's job is to learn those other settings from the string it reads.
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss z"
var currentDate = dateFormat.dateFromString("Sun, 28 Jun 2015 04:30:54 GMT")
// "Jun 27, 2015, 11:30 PM"
If you do as above, it will return an NSDate? from the date string you provided.

Converting string to ruby datetime

I have the following string as a date
Thu 17 Jan
I want to convert this to a ruby date time object (to save in the database). I have tried chronic, but without luck.
Can someone help me out, thanks in advance
No need for chronic. Simple Date will do.
require 'date'
s = 'Thu 17 Jan'
Date.parse(s) # => #<Date: 2013-01-17 ((2456310j,0s,0n),+0s,2299161j)>
You may also use:
s = 'Thu 17 Jan'
date = DateTime.parse(s)

How do I output to the following format: month - year?

Today's month is November (11). With 1.years.ago.to_date..Date.today how can I output:
11 - 2010, 12 - 2010, 01 - 2011, 02 - 2011, 03 - 2011, etc
strftime
Use function for all date modifications in ruby
Refer This DOC
There's probably a more efficient way to do this, but this will give you the output you want:
require "active_support/core_ext/integer/time"
((1.year.ago.to_date)..(Date.today)).map { |d| d.strftime("%m-%Y") }.uniq!
For print date used strtotime() function.
//For today print a date used the following code
echo date('m/d/Y',strtotime("today"));
//For one year ago print a date used the following code
echo date('m.d.Y',strtotime("-1 years"));
//For coming year date from today used following code
echo date('m.d.Y',strtotime("1 years"));
You can to add a new format to your locales.
#/config/locales/en.yml
en:
date:
formats:
month_year: "%m - %Y"
and to use it with I18n.l(your_date, :format => :month_year)
This will help if you want to change the format later, you will change in a unique point.

Resources