Power Query to filter only numbers - powerquery

I've been searching everywhere to find a way to filter a column that contains both Text and Numbers, I want to filter out the numbers only from that column.
Thanks.

Add column, custom column, potentially with one of these
= Text.Select([Column1],{"0".."9"})
=try Number.From([Column1]) otherwise "Text"

Try this:
let
//Change next line to reflect Data source
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
//change next line to include all columns and their names
#"Changed Type" = Table.TransformColumnTypes(Source,{{"COLUMN", type any}}),
//Change next line to be testing the proper column
#"Numbers Only" = Table.SelectRows(#"Changed Type", each not (try Number.From([COLUMN]))[HasError]),
#"Changed Type1" = Table.TransformColumnTypes(#"Numbers Only",{{"COLUMN", type number}})
in
#"Changed Type1"

Related

Creating Dynamic Columns in Power Query with Expression Lookups

I have a Source file that has a set of columns, each of binary type (true, false). I then have a processing (transform) table which has one or more rows, each row specifying a new column to be added to the Source, along with the expression to use to populate that column based on existing columns. In Power Query, I want to be able to extend the Source table with these additional column definitions. There can be any number of transform rows, so I would need to do this dynamically based on row count. Click on link below for an illustration of what I'm shooting for.
I believe this can be achieved using List.Accumulate in Power Query, but I haven't figured out exactly how to do it. Any suggestions?
Here is a method using List.Generate:
let
Transform = Excel.CurrentWorkbook(){[Name="Transform"]}[Content],
#"Transform Table" = Table.TransformColumnTypes(Transform, {{"Col Name", type text},{"Col Formula", type text}}),
Source = Excel.CurrentWorkbook(){[Name="Source"]}[Content],
#"Source Table" = Table.TransformColumnTypes(Source,{{"A", type logical}, {"B", type logical}, {"C", type logical}}),
addCol = List.Last(
List.Generate(
()=>[c=Table.AddColumn(#"Source Table", #"Transform Table"[Col Name]{0},
Expression.Evaluate("each " & #"Transform Table"[Col Formula]{0}), type logical),
idx=0],
each [idx] < Table.RowCount(#"Transform Table"),
each [c=Table.AddColumn([c], #"Transform Table"[Col Name]{[idx]+1},
Expression.Evaluate("each " & #"Transform Table"[Col Formula]{[idx]+1}), type logical),
idx=[idx]+1],
each [c]))
in
addCol

Is there any way how to refer actual column name as variable in Power Query

I am new to Power Query and I would like to ask more experienced people about it.
I am trying to solve problem with Text.Combine where I would like to combine together value in specific column and column name of actual cell.
Do you have any idea?
My idea is formula similar to this:
=Text.Combine({[kod], Column.Name},";")
Thank you very much for answer.
Tomas
Edit 8.12.2021:
#horseyride
I actually try fill columns automatically with data in following format where first part will be value from actual row and actual column name:
For e.g.:
8M0183:F01A0101.B in first row, second column,
8M0182:F01A0102.A in second row, first column
Table example mentioned bellow.
Thank you very much for all answers.
see if this works for you. It combines the KOD column with the column name into each null cell, for every column that is not named KOD
It finds all column not named KOD. It converts those to text. It replaces all nulls with a placeholder, here "xxx". We unpivot to get everything to just three columns. We then combine the column title and the cell contents if the cell contents is equal to the placeholder. Pivot to get back to original format
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ColumnList=List.Difference(Table.ColumnNames(Source),{"KOD"}),
ConvertToText= Table.TransformColumnTypes (Source,List.Transform(ColumnList, each {_ , type text})),
ReplaceNullsWithPlaceholder = Table.ReplaceValue(ConvertToText,null,"xxx",Replacer.ReplaceValue,ColumnList),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(ReplaceNullsWithPlaceholder, {"KOD"}, "Attribute", "Value"),
CombineColumnAndRow = Table.AddColumn(#"Unpivoted Other Columns", "Value2", each if [Value]="xxx" then [Attribute]&":"&[KOD] else [Value]),
#"Removed Columns" = Table.RemoveColumns(CombineColumnAndRow,{"Value"}),
#"Pivoted Column1" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Value2")
in #"Pivoted Column1"

Power Query: How to insert a Column using the Left(Right Function based on A4

Is there a way to Add a column, in Power Query, by referencing data in a specific cell?
I want to take the text from "A4", use a Left(Right function, and add that to a new column.
My VBA macro is:
"Latest 4 Wks - Ending " & Left(Right(.Range("A4"), 24), 23)
I guess you want to do something like that. In a first step you define a named range for A4 which I named cellA4. I then did a load into Powerquery, added an extra column with the part of the text (I used Text.Middle other text function are possible, of course) from the cell and drilled down to the content of the cell. The M-code for that is
let
Source = Excel.CurrentWorkbook(){[Name="cellA4"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each Text.Middle([Column1],23)),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column1"}),
Custom = #"Removed Columns"{0}[Custom]
in
Custom
Result looks like
Them I just made a table with one column and imported that into Powerquery and added an extra column which just contains the text from cell A4. M-Code is
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each cellA4)
in
#"Added Custom"
Result is
Through further research, I found that by adding a Blank Query, I was able to add a column, in Power Query, by referencing data in a specific cell?
Insert BlankQuery
Advance Editor
(YourWorkSheet as table ) as text=>
let
SheetCellA4 =YourWorkSheet[Column1]{3},
SplitByFrom = Text.Split(SheetCellA4, "to "){1},
SplitByTime = Text.Split(SplitByFrom, "`"){0}
in SplitByTime
The bring in the worksheet data
After the Source line
#"Added Custom" = Table.AddColumn(Source, "Custom", each Query1(Source))
In
#"Added Custom"

In power query, how to turn a number into a duration in seconds?

I am trying, in power BI, to create change the type of one of my columns. Said column contain Numbers and I am trying to turn that number into a duration in seconds. But whenever I use the default type change, it turn the duration into days.
= Table.TransformColumnTypes(#"Changed Type",{{"Duration", type duration}})
is the default, I've tried puttin duration(seconds) or duration.seconds, but it didn't work.
I've looked around for a solution, but all I get are DAX solutions. I couldn't find much about power query in general.
Thanks for the help
I believe this does what you want.
If you start with a column called Seconds:
You can add a column with = Duration.From([Seconds]/86400) to get:
Alternatively, you could use:
= Table.ReplaceValue(Source, each [Seconds],each Duration.From([Seconds]/86400),Replacer.ReplaceValue,{"Seconds"})
to change...
directly to...
Here's the M code for the two different options:
Adding a column:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Duration", each Duration.From([Seconds]/86400))
in
#"Added Custom"
Directly changing:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Replaced Value" = Table.ReplaceValue(Source, each [Seconds],each Duration.From([Seconds]/86400),Replacer.ReplaceValue,{"Seconds"})
in
#"Replaced Value"

Inserting text manually in Powerquery

I'm merging multiple Excel files into one where the user can review and mark an additional Comment column as completed. Each day there are additional files and I need to refresh the query and pull the data in. Keeping the original Comment column values.
I've attempted to do this by referencing Marcel Beug's video but that uses an sql table and I cannot seem to get it to work with the Excel files as the source.
After the Merge Queries I attempt to modify the first file to my source "InputFile"
![Modify the Merge Formula1][2]
![Changed to last query step of InputFile][3]
![InputFile Query with Source2 and Merge][4]
![M Code of InputFile Query with Merge][5]
By setting the First field in the Merge Formula to the last step in the InputFile query I was able to get around the Cyclic error however I find that every Refresh creates duplicate rows. 4 become 8 that then becomes 16, etc.
let
Source = Excel.Workbook(File.Contents("S:\Fin_Aid\Operations Team\COD mpn - lec\InputFiles\8.22.18 to 8.23.18.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
Rename_RecID = Table.RenameColumns(#"Removed Columns",{{"Column3.1", "RecID"}}),
Source2 = Excel.CurrentWorkbook(){[Name="InputFile"]}[Content],
InputWithComment = Table.TransformColumnTypes(Source2,{{"RecID", Int64.Type}, {"Column1", type text}, {"Column2", type text}, {"Column4", type text}, {"Column5", type text}, {"Comment", type text}}),
#"Merged Queries" = Table.NestedJoin(Rename_RecID,{"RecID"},InputWithComment,{"RecID"},"InputWithComment",JoinKind.LeftOuter),
#"Expanded InputWithComment" = Table.ExpandTableColumn(#"Merged Queries", "InputWithComment", {"Comment"}, {"Comment"})
in
#"Expanded InputWithComment"
Regards,
Jim

Resources