Concatenating dynamic column name to table - Power Query - powerbi

I am trying to create lists of data using dynamic columns, fetching the column name and appending it to the table.
Here is the problem as this step is throwing error:
Expression.Error: We cannot apply operator & to types Table and Text.
Details:
Operator=&
Left=[Table]
Right=Test1
Code:
let
Source = Excel.Workbook(File.Contents("C:\Users\Sivakumar A\Desktop\Power BI\test1.xlsx"), null, true),
Sheet2_Sheet = Source{[Item="Sheet2",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet2_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Test1", type text}, {"Test2", Int64.Type}, {"Test3", Int64.Type}, {"Test4", Int64.Type}}),
#"Col Names" = Table.ColumnNames(#"Changed Type"),
#"Generate" = List.Generate(() => [i=-1,x= Table.FromList(#"Col Names",Splitter.SplitByNothing())],
each [i]<List.Count(#"Col Names"),
each [
i=[i]+1,
x=#"Promoted Headers"&#"Col Names"{i}
],
each [x])
in
Generate
I need to extract the column names and append it to the talble and execute in loop
Any help is appreciated.

My requirement is to append the column names dynamically to the table using list.generate and generate the list of values out of it.
I was able to resolve the issue using Double Quotes 3 times.
"Table.Distinct(Table.FromList(#"&"""Sheet2 (2)"""&"["&#"Col Names"{i}&"], Splitter.SplitByNothing(), null, null, ExtraValues.Error))"

Related

pbi m query to concatenate a field

Not well versed in PBI formulas but kinda know what I'm looking for. I have a comment field that can be long so what I'm trying to do is
if text.length([comment] > 45) then text.range(text.combine([comment],"..."),45) else [comment]
Some of the comments field will be null as well. I've tried different variations of this and just can't see to get it right. Appreciate any help. Thanks.
Note, M is case sensitive, so text.length() Null and text.combine will not work. You have to use Text.Length() , Text.Combine() , null, etc.
Try this:
= try if Text.Length([comment])>45 then Text.Start([comment],45)&"..." else [comment] otherwise null
You can use add that as part of a new custom column:
let Source = #table({"comment"},{{"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"},{"Bus"},{null}, {"Car Log"}}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"comment", type text}}),
#"Add Custom Column" = Table.AddColumn( #"Changed Type", "Custom", each try if Text.Length([comment])>45 then Text.Start([comment],45)&"..." else [comment] otherwise null)
in #"Add Custom Column"
Or transform the existing column:
let Source = #table({"comment"},{{"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"},{"Bus"},{null}, {"Car Log"}}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"comment", type text}}),
#"Transform Column" = Table.TransformColumns(#"Changed Type",{{"comment", each try if Text.Length(_)>45 then Text.Start(_,45)&"..." else _ otherwise null, type text}})
in #"Transform Column"

Change field type and multiple exact value replacements in a single Power BI Step / M Statement

It's my first day starting out with Power BI and M Code. I've looked through a dozen replace threads, but couldn't find what I was looking for.
I'm currently using the below code (multiple steps) to change the field type to Text, then replace 3 exact values with another value. I'm struggling to do this in a more efficent/elegant way (in a single step).
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"EU Member State", type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Changed Type","0","Unknown",Replacer.ReplaceValue,{"EU Member State"}),
#"Replaced Value1" = Table.ReplaceValue(#"Changed Type","1","Yes",Replacer.ReplaceValue,{"EU Member State"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value","2","No",Replacer.ReplaceValue,{"EU Member State"})
Any help would be greatly appreciated!
Note: I don't want to use a lookup table.
Thank you!
Jay.
Table.replace can only perform one replace at a time, therefore you have to use alternative method to perform multiple replace within single m query, here is the solution and accept if help :)
#"Changed Type" = 'xxformula',
#"Replaced Value" =(
let
Source1 = Table.FromColumns({{"0","1","2"}}),
Substitutions = [
#"0" = "unknown",
#"1" = "yes",
2 = "no"],
Substituted = Table.TransformColumns(#"Changed Type",
{{"Name", each Record.FieldOrDefault(Substitutions, _, _)}})
in
Substituted
)
in
#"Replaced Value"
Before:
After:

How can I pull the top n rows of a list of tables with expanded columns?

I am using folder data source and would like to extract the first n rows from each file.
After implementing Excel.Workbook([Content]) to get the contents of each file and then expanding columns from each file, I would like to keep the contents of just the first n rows, rather than the entire columns.
Thanks in advance!
On the Power Query editor, use the Home > Keep Rows > Keep Top Rows option to filter the list down to just 1 result. You can apply this at any step of the query based on how you want it filtered.
I made some example data which looked like that
First I created a function with the name fxXlContent to extract the first two rows from one of these files which looks like that
(inpContent as any) =>
let
Source = Excel.Workbook(inpContent, null, true),
Tabelle1_Sheet = Source{[Item="Tabelle1",Kind="Sheet"]}[Data],
Header = Table.PromoteHeaders(Tabelle1_Sheet, [PromoteAllScalars=true]),
changeType = Table.TransformColumnTypes(Header,{{"ID", Int64.Type}, {"Info", type text}, {"Januar", Int64.Type}, {"Februar", Int64.Type}, {"März", Int64.Type}, {"April", Int64.Type}, {"Mai", Int64.Type}, {"Anmerkung", type text}}),
keepFirstRows = Table.FirstN(changeType,2)
in
keepFirstRows
Then I used this function when I extracted the folder to get the content of each file. You very likely need to adjust the steps below to your needs.
let
Source = Folder.Files("d:\tmp\01"),
filteredRows = Table.SelectRows(Source, each not Text.StartsWith([Extension], "~") and Text.EndsWith([Extension], "xlsx")),
addCustomColumn = Table.AddColumn(filteredRows, "xlContent", each fxXlContent([Content])),
removeOtherColumns = Table.SelectColumns(addCustomColumn,{"xlContent"}),
extendXlCOntent = Table.ExpandTableColumn(removeOtherColumns, "xlContent", {"ID", "Info", "Januar", "Februar", "März", "April", "Mai", "Anmerkung"}, {"ID", "Info", "Januar", "Februar", "März", "April", "Mai", "Anmerkung"})
in
extendXlCOntent
The single steps look like that

Model data source - Power BI

I'm trying to model a data source in power bi, but I'm not getting it.
Could you help me with alternatives so I can create a new column? The data source is in excel and brings the data with subtotal by types (XPTO, XPT, etc). I want to put these types as corresponding values ​​in the new column for your items. I tried via power query and dax, but I could not.
Original Source:
Modifications Needed
Source File
This assumes that you can identify the Subtotal Rows by checking the position of the first space in the first column:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added RowType" = Table.AddColumn(Source, "RowType", each Text.PositionOf([#"Centro financ./item orçamento"]," "), Int64.Type),
#"Added Type" = Table.AddColumn(#"Added RowType", "Type", each if [RowType] = 4 then Text.BetweenDelimiters([#"Centro financ./item orçamento"], " ", " ") else null, type text),
#"Filled Down" = Table.FillDown(#"Added Type",{"Type"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([RowType] = 8)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"RowType"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",List.Combine({{"Type"}, Table.ColumnNames(Source)}))
in
#"Reordered Columns"
Another solution is to - Include another data source in Power BI, which will look something like this.
Item Type
615x92120 Mat1 XPTA
615x92121 Mat2 XPTA
615x92122 Mat3 XPTU
615x92123 Mat4 XPTU
And then do a Join between your existing table and this table to bring out the Type in your existing table. Once you have done that, you should be able to filter out to blanks or null which will be your delete lines.
Note :- This only works, if you know all the items and corresponding types in advance.

Power Query select records by multiple strings OR by a string in a different field

Using the answer on this page How to search multiple strings in a string? I managed to write the following query -
let
Source = Csv.Document(File.Contents("P:\DMWORK\all_donations_for_last_10_years.txt"),[Delimiter=" ", Encoding=1252]),
#"Promoted Headers" = Table.PromoteHeaders(Source),
#"Defined Table" = Table.TransformColumnTypes(#"Promoted Headers",
{{"Donation Date", type date}, {"Amount", type number}, {"Company", type text}, {"Extra Codes", type text}}),
Text.ContainsAny = (string as text, list as list) as logical =>
List.AnyTrue(List.Transform(list, (substring) => Text.Contains(string, substring))),
FirstFilter = Table.SelectRows(#"Defined Table", each Text.ContainsAny([Company], {"Assoc","Band","Baseball"})),
SecondFilter = Table.SelectRows(#"Defined Table", each [Extra Codes] = "FUNDRAIS"),
FinalResult = FirstFilter or SecondFilter
in
FinalResult
So what I'm trying to do, is SelectRows that match either the FirstFilter OR the SecondFilter. Yet it's giving me a "Expression.Error: The value isn't a single-character string. Details: Value= " error.
If someone could put me in the right direction to resolve this, I would really appreciate it.
Thanks in advance.
I would simplify this using the secret "or" operator. I'm not being sarcastic - it actually is secret as in totally obscured in their documentation. Try searching for it...
Anyway I would replace the code from FirstFilter down with something like:
Filter = Table.SelectRows(#"Defined Table", each Text.ContainsAny([Company], {"Assoc","Band","Baseball"}) or [Extra Codes] = "FUNDRAIS" ),
in
Filter