Convert datetime String format to Date format in Power BI - string

I have a column in a Power BI dataset which is Datetime values but in a String format. I've attempted to change the format to Date and Datetime but always receive the following error:
DataFormat.Error: We couldn't parse the input provided as a Date value.
Details:
27MAY2021:15:42:29
All of the values in the column are as the format above, e.g. 04JAN2022:15:33:42
I want to convert all of the values in this column to Date or Datetime, but ideally 01/01/2022

Use this M-code to convert
let
Source = Table.FromList({"27MAY2021:15:42:29", "04JAN2022:15:33:42"}, null, {"Date String"}),
#"Changed Type" = Table.TransformColumnTypes(
Source,{{"Date String", type text}}),
#"Added Custom" = Table.AddColumn(
#"Changed Type", "Date",
each DateTime.FromText([Date String], [Format="ddMMMyyyy:HH:mm:ss", Culture="en-US"]), type datetime)
in
#"Added Custom"
Note that how the datetime is shown in your GUI depends on your regional settings.

Related

Power Query Expression.Error: The Date value must contain the Date component. Details: 43831

I am using power query in Excel. I have a date column with date starting in 1/31/2015 and ending 1/31/2022. I have the below line of code that works just fine.
...
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Reading", type number}, {"EM_Date", type date}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [EM_Date] <= #date(2021, 12, 31)),
...
Now if I change the code just slightly like below It stops working and gives me an error. "Power Query Expression.Error: The Date value must contain the Date component. Details: 43831"
...
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Reading", type number}, {"EM_Date", type date}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [EM_Date] >= #date(2021, 1, 31)),
...
How do I fix this error?
For those that are coming to this post for answers. What I found is that the Column must be set to Dates. In my case I was calculating dates and the source column was not set as dates.
Hope this helps you!

Convert time interval from text to number and grouping by customer time interval

I have some data grouped by quarter hour, and this column is a text format. I want to group the data by customer time intervals, like hourly, or 6am-9am etc. How can I do this in Power Query Editor for use with Power Pivot/Excel?
Thank you!
This should get you most of the way there
right click and split the column on the - character
Transform the desired column wth something like the hour function and change the code from
= Table.TransformColumns(#"Changed Type1",{{"Quarter Hour.2", Time.Hour, Int64.Type}})
to
= Table.TransformColumns(#"Changed Type1",{{"Quarter Hour.2", each Time.From(Number.Round(Number.From(Time.From(_))*(24*60/15))/(24*60/15)), type time}})
to round to nearest desired number of minutes, here I am using closest 15 minutes
From there just filter using drop downs for the desired time ranges and group whatever you want
sample code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Quarter Hour", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Quarter Hour", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"Quarter Hour.1", "Quarter Hour.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Quarter Hour.1", type time}, {"Quarter Hour.2", type time}}),
#"Extracted Hour" = Table.TransformColumns(#"Changed Type1",{{"Quarter Hour.2",
each Time.From(Number.Round(Number.From(Time.From(_))*(24*60/15))/(24*60/15))
, type time}})
in #"Extracted Hour"

Expression.Error: We cannot convert the value #date(2021, 9, 2) to type Logical

I am trying to combine date and time by adding custom column with Date/Time data (e.g. 2021-09-01 18:00:00). Trying to achieve this with the following code:
[date] and " " and [time]
but I end up with the error from the title. I don't see anything wrong in the formula bar:
= Table.AddColumn(#"CreateTime naming", "Custom", each [date] and " " and [time])
Before I did very similar operation with date and time, using the same code phrase structure as above and it worked. Then I tried to adjust it for the mentioned operation and it failed.
The error message I get is:
Expression.Error: We cannot convert the value #date(2021, 9, 2) to type Logical.
Details:
Value=2021-09-02
Type=[Type]
Could you help me to solve this?
Hopefully, this discussion will clarify what I have written in the comments.
The formula to use:
#"Added Custom" = Table.AddColumn(#"Changed Type", "datetime",
each [date] & [time])
I suspect you are trying to use something like:
each [date] & " " & [time])
which is why you see the last error you mention in your comments
Expression.Error: We cannot apply operator & to types Date and Text.
Details:
Operator=&
Left=9/1/2021
Right=
Had the issue been with [Time], the error would have read:
Expression.Error: We cannot apply operator & to types Text and Time.
Details:
Operator=&
Left=
Right=1:15:00 PM
But the & operator will join a date type and a time type into a datetime type. No further conversion needed.
For example:
let
//data typed into a table
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkksSVXSUSrJzE1VitWJVrLUN9Q3MjAyBIoZGlsZmkIFjWCCZlYm5uhillamRkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
//data types set to date and time for the respective columns
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"date", type date}, {"time", type time}}),
//create a datetime column
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [date] & [time], type datetime)
in
#"Added Custom"

PowerQuery - Forecast from table

I am trying to create a forecast (single table) for departments to input their assumptions on spending in a single table. Instead of entering amounts for every single month, I would like the user to enter the amount, frequency, start date, and end date for each category. To illustrate, see below the table with some sample data.
This is the result in Power Query (or Power BI) I am trying to get, which is my understanding of how to be able to run date slicers and filters in a Power BI model when comparing against actuals.
If this can't be done with DAX and instead must be done in excel (through look up formulas), how would you structure the formula?
Here is a PQ example that creates what you show as your desired table given what you show as your input:
To use Power Query
Select some cell in your Data Table
Data => Get&Transform => from Table/Range
When the PQ Editor opens: Home => Advanced Editor
Make note of the Table Name in Line 2
Paste the M Code below in place of what you see
Change the Table name in line 2 back to what was generated originally.
Read the comments and explore the Applied Steps to better understand the algorithm
M Code
let
Source = Excel.CurrentWorkbook(){[Name="Table9"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"G/L", Int64.Type}, {"Dimension", type text}, {"Description", type text},
{"Amount", Int64.Type}, {"Repeat Every", type text}, {"Start Date", type date}, {"End Date", type date}}),
//Last possible date as Today + 5 years (to end of month)
lastDt = Date.EndOfMonth(Date.AddYears(Date.From(DateTime.FixedLocalNow()),5)),
//Generate list of all possible dates for a given row using List.Generate function
allDates = Table.AddColumn(#"Changed Type", "allDates", each let
lastDate = List.Min({lastDt,[End Date]}),
intvl = {1,3,6}{List.PositionOf({"Monthly","Quarterly","Semi Annual"},[Repeat Every])}
in
List.Generate(
()=> [Start Date],
each _ <= lastDate,
each Date.EndOfMonth(Date.AddMonths(_,intvl)))),
//Remove unneeded columns and expand the list of dates
#"Removed Columns" = Table.RemoveColumns(allDates,{"Repeat Every", "Start Date", "End Date"}),
#"Expanded allDates" = Table.ExpandListColumn(#"Removed Columns", "allDates"),
//Sort to get desired output
// Date column MUST be sorted to ensure correct order when pivoted
// Other columns sorted alphanumerically, but could change the sort to reflect original order if preferred.
#"Sorted Rows" = Table.Sort(#"Expanded allDates",{
{"allDates", Order.Ascending},
{"G/L", Order.Ascending},
{"Dimension", Order.Ascending}}),
//Pivot the date column with no aggregation
#"Pivoted Column" = Table.Pivot(
Table.TransformColumnTypes(#"Sorted Rows", {
{"allDates", type text}}, "en-US"),
List.Distinct(Table.TransformColumnTypes(#"Sorted Rows", {{"allDates", type text}}, "en-US")[allDates]),
"allDates", "Amount")
in
#"Pivoted Column"
Original Data
Results

Power Query - replacing text in a new column

I am new to Power Query in Excel and my question is:
I have a text column with date in this format "09-Feb-17 A". To remove the text " A" and populate the date info in a new column (custom column?), I have used this code:
= Table.AddColumn(#"Changed Type", "Start Date", each Replacer.ReplaceText([Start], " A",""))
Problem is some of the dates are in correct format i.e. without " A". For those dates I get an error:
Expression.Error: We cannot convert the value #date(2019, 1, 11) to
type Text. Details:
Value=11/01/2019
Type=Type
Is there any way to solve this issue within power query?
Thanks in advance.
You can use try otherwise to deal with both data types:
= Table.AddColumn(#"Changed Type", "Start Date", each try Date.FromText(Replacer.ReplaceText([Start], " A","")) otherwise DateTime.Date([Start]), type date)
Or this, which will extract the date before the first space, irrespective of which (or how many) characters follow:
= Table.AddColumn(#"Changed Type", "Start Date", each try Date.FromText(Text.BeforeDelimiter([Start], " ")) otherwise DateTime.Date([Start]), type date)
Perhaps
Change the column Data type to Text (dates --> date time)
Extract the portion of the string prior to the space
Optional Change column Data type to Date
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Dates", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.BeforeDelimiter([Dates]," ")),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", type date}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Dates"})
in
#"Removed Columns"

Resources