pbi m query to concatenate a field - powerbi

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"

Related

Authentication error using Yahoo Finance in Power BI when updating data

When trying to autoupdate stock values using Yahoo Finance link from a csv file using this:
Source = Csv.Document(
Web.Contents(
"https://query1.finance.yahoo.com/v7/finance/download/"
& Comp
& "?period1=1022112000&period2="
& LastDate
& "&interval=1d&events=history"
),
[Delimiter = ",", Columns = 7, Encoding = 1252, QuoteStyle = QuoteStyle.None]
Extra info: Comp and lastdate are custom parameters, in which comp fetches all company stock data and lastdate records the most recent stock date.
I keep getting authentication errors everytime I try to access the data using either anonymous or user login. What can I do?
Thanks
Please check what value you provide to the URL string. Probably there is some error.
You can check this example with function:
let
Source = (STOCKNAME, StartDat, EndDat) => let
Source = Csv.Document(Web.Contents("https://query1.finance.yahoo.com/v7/finance/download/"&STOCKNAME&"?period1="&StartDat&"&period2="&EndDat&"&interval=1d&events=history&includeAdjustedClose=true"),[Delimiter=",", Columns=7, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Open", type text}, {"High", type text}, {"Low", type text}, {"Close", type text}, {"Adj Close", type text}, {"Volume", Int64.Type}})
in
#"Changed Type"
in
Source
Query:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCgkPCVLSUTI0M7E0MzY1MjCAcsyNDM2AnFidaCVPvxBnVFEIx8LAwgCkJBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [TICKER = _t, Start = _t, End = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"TICKER", type text}}),
#"Invoked Custom Function" = Table.AddColumn(#"Changed Type", "GetStock", each GetStock([TICKER], [Start], [End])),
#"Expanded GetStock" = Table.ExpandTableColumn(#"Invoked Custom Function", "GetStock", {"Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"}, {"GetStock.Date", "GetStock.Open", "GetStock.High", "GetStock.Low", "GetStock.Close", "GetStock.Adj Close", "GetStock.Volume"})
in
#"Expanded GetStock"

PowerBI Problems Subtracting Rows

Ok, so I'm trying to take two rows and subtract them to make another column with that difference. I know that there is a Question like this already but it need more help. here is the code that I think would work.
=Table.AddColumn(#"YourLastStep", "Diff",
each
(try DateTime.From(#"YourLastStep"[Time]{[ID]-2})
otherwise DateTime.From([Time])) - [Time]
)
But I don't understand "=Table.AddColumn(#"YourLastStep", "Diff",
each." what does "YourLastStep" mean please help.
Thank you,
If I take the following
let
Source = {1..10},
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", Int64.Type}})
in
#"Changed Type"
In #"Changed Type" step, YourLastStep is #"Converted to Table"
In #"Converted to Table" step, YourLastStep is Source

Concatenating dynamic column name to table - Power Query

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))"

Newly added column in cvs source data (combined folder) - not appearing when refreshing in power query section in Power BI

Still stumbling my way through Power BI.
I have added a new column in just a few of the files in the folder that I had already pulled in and combined but this column is not appearing once refreshed. Despite the endless research, I am at a loss as to how to fix this in advanced editor.
Your assistance would be greatly appreciated.
Code below:
Source = Folder.Files("C:\Users\Sarah\OneDrive\FEEDLOT\APS Files\Original Files"),
#"Filtered Hidden Files1" = Table.SelectRows(Source, each [Attributes]?[Hidden]? <> true),
#"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File", each #"Transform File"([Content])),
#"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
#"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File"}),
#"Removed Errors1" = Table.RemoveRowsWithErrors(#"Removed Other Columns1", {"Transform File"}),
#"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Errors1", "Transform File", Table.ColumnNames(#"Transform File"(#"Sample File"))),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Table Column1",{{"Source.Name", type text}, {"Tag Number", type text}, {"Electronic ID", type text}, {"NLIS", type any}, {"Date", type datetime}, {"Live Weight (kg)", type number}, {"Draft", type any}, {"Condition Score", type any}, {"Notes", type any}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Source.Name", "APS Source File Name"}, {"Tag Number", "Visual ID"}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Renamed Columns", "APS Source File Name", Splitter.SplitTextByEachDelimiter({"-"}, QuoteStyle.Csv, true), {"APS Source File Name.1", "APS Source File Name.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"APS Source File Name.1", type text}, {"APS Source File Name.2", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"APS Source File Name.2"}),
#"Renamed Columns2" = Table.RenameColumns(#"Removed Columns",{{"APS Source File Name.1", "APS Source File Name"}})
First of all, check twice you are reading the correct file and backup the current script. Then, I would try two approaches:
Delete the steps until you find the one that causes the problem. Then, fix the step causing the issue and check if at the end of the pipeline the columns appear.
If this does not solve the issue, I would go with the second approach:
Create a new PBIX, and one-by-one re-implement all the steps until you find the one causing the new columns to disappear.
Don't try to fix this in Advanced Editor alone. Instead, step through the code and inspect the result after each step. That will show you where the problem is.
Without seeing your data it is not possible to tell you where in your code you need to make changes, but you will be able to see that if you step through the actions one by one.

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