Using Power Query Editor I am trying to remove the (Right,3) character if the LEN of the string is > 11.
This is what I am working with now ' = Table.RenameColumns(#"Merged Columns",{{"Merged", "oe_nosuf"}})'
example of current value : "65507129-02"
If the value is "65507129-002" then I want to remove the extra "0" from the right 3 spaces.
any help is appreciated.
Sample two ways to do this, a new column based on Column1 or replacing the current value of Column1
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each if Text.Length([Column1]) = 12 then Text.Start([Column1],9) & Text.End([Column1],2) else [Column1]),
#"Modify" = Table.TransformColumns(#"Added Custom",{{"Column1", each if Text.Length(_) = 12 then Text.Start(_,9) & Text.End(_,2) else _ , type text}})
in #"Modify"
Related
I need to add 12 empty columns in Power Query with custom names.
Now I am adding one column at the time and change the name.
Is it possible to do this faster/better?
I tried the first option but get an error.
When I add a column with code
= Table.FromColumns(
Table.ToColumns(#"Prev Step") & {{null}, {null}, {null}},
Table.ColumnNames(#"Prev Step") & {"Empty1", "Empty2", "Empty3"}
)
I get a lot of rows in the three columns.
What am I doing wrong?
How about this for adding three empty columns? Extend to more as needed.
= Table.FromColumns(
Table.ToColumns(#"Prev Step") & {{null}, {null}, {null}},
Table.ColumnNames(#"Prev Step") & {"Empty1", "Empty2", "Empty3"}
)
By generating lists of columns and associated names, you can specify just the number of columns to add, and let the list of nulls and names be generated automatically.
eg:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkksSVQwVIrVgTKNlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
//add Multiple Blank Columns
numCols = 12,
colNames = List.Generate(
()=>[colName = "Blank", idx=0],
each [idx] < numCols,
each [colName = "Blank" & Number.ToText([idx]+1), idx = [idx]+1],
each [colName]
),
addedCols = Table.FromColumns(
Table.ToColumns(#"Changed Type") & List.Repeat({{null}},numCols),
Table.ColumnNames(#"Changed Type") & colNames)
in
addedCols
New to PowerQuery and M-Code.
I have added a column with a calculation to get the max. Instead of using the hardcoded column name, I would like to use the position number of the column.
The current code is:
= Table.AddColumn(Source, "Maximum", each List.Max({[#"1-6-2021"], [#"1-5-2021"], [#"1-4-2021"]}), type number)
Instead of [#"1-6-2021"], I would like it to be column 3; for [#"1-5-2021"] column 4 etc.
How do I replace these columnnames with positions?
Many thanks for the help!
You can adjust the {x} part for the column # you want
0 is the first column, so this is max of columns 2/3/4
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
x= Table.AddColumn(Source, "Maximum", each List.Max({
Record.Field(_,Table.ColumnNames(Source){1}),
Record.Field(_,Table.ColumnNames(Source){2}),
Record.Field(_,Table.ColumnNames(Source){3})
}), type number)
in x
If you need to do a Max on a bunch of columns, below would, for example, do it for all columns except the first two, which are removed by the 2nd line
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
colToSum = List.RemoveFirstN(Table.ColumnNames(Source),2),
AddIndex = Table.AddIndexColumn(Source,"Index",0,1),
GetMax = Table.AddColumn(AddIndex, "Custom", each List.Max( Record.ToList( Table.SelectColumns(AddIndex,colToSum){[Index]}) ))
in GetMax
How can I turn this
Staff
Hours
Amy
5,10,20
Ben
6
Charles
10,1
into this using POWER QUERY?
Staff
Hours
Amy
35
Ben
6
Charles
11
Steps:
Convert the source table to an Excel Table
Get data from that table
Divide the column by delimiter (comma) into rows
Change the column type to number
Group rows and sum the Hours column
M code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
DivideByDelimiter = Table.ExpandListColumn(Table.TransformColumns(Table.TransformColumnTypes(Source, {{"Hours", type text}}, "es-CO"), {{"Hours", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Hours"),
ChangeType = Table.TransformColumnTypes(DivideByDelimiter,{{"Hours", type number}}),
GroupRows = Table.Group(ChangeType, {"Staff"}, {{"Sum", each List.Sum([Hours]), type nullable number}})
in
GroupRows
Let me know if it works
Another way to do this
right click the Hours column and replace values, replacing the comma with a +
right click Hours column and transform lowercase
In formula bar, replace the resulting formula so that it ends with
, each Expression.Evaluate(_), type number}})
instead of
, Text.Lower, type text}})
Full sample code
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Staff", type text}, {"Hours", type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Changed Type",",","+",Replacer.ReplaceText,{"Hours"}),
#"Lowercased Text" = Table.TransformColumns(#"Replaced Value",{{"Hours", each Expression.Evaluate(_), type number}})
in #"Lowercased Text"
Is there an an equivalent to EARLIER in M/Power Query?
Say, I have a table with lots of different dates in column DATE and a smaller number of letters in column LETTER. I now want the maximum date for each letter.
In DAX, I would use something like CALCULATE(MAX([Date]),FILTER(ALL(Table),[Letter]=EARLIER([Letter])).
How would I achieve the same in M?
Thanks
2 Solutions in the code below. Notice that each uses "PreviousStep" as basis, so these are separate solutions.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
PreviousStep = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Letter", type text}}),
// 1. Add a column to the original table with the MaxDate for each letter
// "earlier" is just the name of a function parameter; it could as well have been "x" or "MarcelBeug"
AddedMaxDate = Table.AddColumn(PreviousStep, "MaxDate", (earlier) => List.Max(Table.SelectRows(PreviousStep, each [Letter] = earlier[Letter])[Date])),
// 2. Group by letter and get the MaxDate for each letter
GroupedOnLetter = Table.Group(PreviousStep, {"Letter"}, {{"MaxDate", each List.Max([Date]), type date}})
in
GroupedOnLetter
In short, there is no exact match for this function. Still, you can use other ways that can produce same results.
To reproduce example offered by Microsoft in help for EARLIER function, you can use following code (table1 equals table given in the example before ranking):
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TVNNaxtBDP0rxuSoivn+uMYlLSUFE4f2YHIYd4d48Xq3rO1C/n01o1mc4+i9kZ6epP1+LcMa1o/94cvuOM3XCz0epHUgnccQ1m+wXytXGae8ekl/TpWhlACvBHrBDL8wdtc0dpWiLTgV0EVm1CrT9Trky4ooq016z5VnI2ij0OjKs402nVePM1XLrMgEcEaj8ZVU9czpxAmcAik1SlcxGSm2SX/5m4eoDVpToSJyc0z9WLEAwXgUrcX6a8hpzDNb4CAEhU5VuIjfzGk8XZoeGSVYpVBwd+X31zynfhjyjRM4A9FZ1NyWFhR7ymPX0hsJ0RuUbJ+s6DSzt96QtR4d96MK9m2Y/uVmfABtNVrWbSj2newc8iEtwjUoS401O2Rh5NQtyq0HZyNGFq4ZHs6Lz1aCjAopXmFV4I9uTtd+GlfbZfyR3IkafTOvJPlBneUPbj1GMCouMFkA6+f+/VhLcKjofp5aNmlBkKQ23JLs53QbrzSoVdkp3iYDWlgIzqBi6VJ9Jj7N6cxMA1ZSE16ga/XLTm3TOPZsPv8uora5SwNLMIIkK1Q8EF02bHs78xZJBS5alK1bCr1Mqbtro7+WfHPRoeZNk2Yh3XVpcNqBjgE9myuLrl3qaHg8GUUr5RYbVKlzP0kdLHhBJ9kOrsjfLQaWndCEWcZK8dfF7wcZIrkRUXNe7Ss6tzN8vR2WxTIQtMLQJl9Y023ux/d7o1JTHVOH0MyQ7hPv3isdh7F01gYFH5Aqvf7KF5akyLEYBYrmVpH0+5jz0C4nADEq+vYf", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ProductSubcategoryKey = _t, EnglishProductSubcategoryName = _t, TotalSubcategorySales = _t]),
table1 = Table.TransformColumnTypes(Source,{{"ProductSubcategoryKey", Int64.Type}, {"EnglishProductSubcategoryName", type text}, {"TotalSubcategorySales", Currency.Type}}, "en-US"),
AddCount = Table.AddColumn(
table1,
"SubcategoryRanking", //(a) is a parameter for function, which equals current record, and function should return value for new cell of "SubcategoryRanking"
(a)=> Table.RowCount(
Table.SelectRows(
table1, //(b) equals whole table1. This function returns table filtered by given criteria
(b) => b[TotalSubcategorySales] < a[TotalSubcategorySales])
) + 1,
Int64.Type)
in
AddCount
I think you can use the GroupBy function to group the data by Letter and find the Max of the date column. So your code should look like.
= Table.Group(#"Previous step", {"Letter"}, {{"Max Date", each List.Max([Date]), type date}})
I have a text file - in fact a report - that has several pages, each page having a header and a footer. The header has a string that indicates the topic covered in the body of the page. I would like to extract the body of the pages that relate to a specific topic. Headers and Footers have the same number of lines, and body has the same structure as shown in an example at the bottom of the note. How to extract the information about claims type BBB only ?
The number of rows to skip at the top of the report is unknown, as well as the number of rows to drop at the bottom of the report. Could somebody point me in the right direction ? Thank you.
Page 1
Claims type: AAA
Claim # Amount $
11111 10
11112 20
.....
End of Page 1
Page 2
Claims type : AAA
...etc.
End of Page 2
Page 3
Claims type : BBB
Claim # Amount $
21111 100
21112 200
.....
End of Page 3
Page 4
Claims type : CCC
You Can do it with UI only:
let
Source= Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
AddCustom = Table.AddColumn(Source, "Custom", each if Text.Start([Column1],6)="Claims" then Text.End([Column1],3) else if Text.Start([Column1],6)="End of" then "Trash" else null),
ReplErrs = Table.ReplaceErrorValues(AddCustom, {{"Custom", null}}),
FillDown = Table.FillDown(ReplErrs,{"Custom"}),
FilterBBB = Table.SelectRows(FillDown, each ([Custom] = "BBB")),
Rem1st = Table.Skip(FilterBBB,1),
Promoted = Table.PromoteHeaders(Rem1st)
in
Promoted
I don't think there's a way to do this purely through the UI. You'll want to use the Table.PositionOf and List.PositionOf methods.
Here is what I have:
let
Source = Table // however you get the table
#"Position of Claims" = Table.PositionOf(Source, [Column1 = "Claims type : BBB", Column2 = null]),
// Remove entries above the table belonging to Claims type BBB.
#"Remove Top Rows" = Table.Skip(Source, #"Position of Claims" + 2),
// Check which column has the "End of Page" tag
#"Added Custom" = Table.AddColumn(#"Remove Top Rows", "Custom", each if [Column1] is text and Text.StartsWith([Column1], "End of Page") then 1 else 0),
#"Position of End of Page" = List.PositionOf(#"Added Custom"[Custom], 1),
// Remove rows that don't belong to this page's table
#"Remove Bottom Rows" = Table.FirstN(#"Added Custom", #"Position of End of Page"),
// Remove the column that told us which row had End of Page on it
#"Removed Columns" = Table.RemoveColumns(#"Remove Bottom Rows",{"Custom"})
in
#"Removed Columns"`