Power BI/query text column is shown as a table - powerbi

I am importing data into Power BI desktop from an XML file. If the field value is empty, e.g.,
<Address></Address> then PBI assigns that column type "Table" and it is not possible to change the type into text. I am getting an error:
Expression.Error: We cannot convert a value of type Table to type Text.
Details:
Value=Table
Type=Type
It looks like if the first encountered value is empty, then all following rows of that column in the same "page" (data is being read in chunks a.k.a "pages" by the data connector) fetch are assigned type "Table", though not always.
For some columns, it even not possible to change the column data type (they have "table" icon)
Any ideas on how to change the column type to text?
Edit1: I have noticed that clicking "Table" actually shows there is a text value in that column. Maybe there is some invisible setting for Power BI to overcome this blank string value problem?
Edit2: it looks like I have the same problem as described here https://community.powerbi.com/t5/Desktop/XML-Import-Table-Type-in-Column/td-p/97512. There is no solution to this simple problem?
I found this https://community.powerbi.com/t5/Issues/Power-Query-should-not-wrap-simple-xml-elements-in-a-table/idi-p/125525. It looks it is "by design" and no solution exists. It's a pitty for wasting time...
I have found someone's solution:
let
ConvertTableField = (aTable as table, fieldName as text) =>
let
//alternative: https://community.powerbi.com/t5/Desktop/Expand-value-from-table/td-p/214838
//#"Expanded g_v" = Table.TransformColumns(#"Expanded ts_info", {{"g_v", each if _ is table then Table.FirstValue(_, "") else _}})
#"Lists from table" =
Table.TransformColumns(
aTable,
{
{
fieldName,
each
if _ is table then
Table.ToList(_)
else {_}
}
}
),
#"Expanded List" =
Table.ExpandListColumn(
#"Lists from table",
fieldName
)
in
#"Expanded List"
in
ConvertTableField
I haven't still figured out how to run this function for all the columns of the table. M language is the most difficult of the programming languages I know more or less...

I am not sure about the XML usage, but as far as table type to text conversion goes - this worked for me. Maybe it's an overkill.
let
Source = (Table1 as table) =>
let
Section = Record.ToTable(#sections[Section1]),
Compare =
Table.SelectRows(
Section,
each [Value] = Table1
){0},
GetName = Record.Field(Compare, "Name")
in
GetName
in
Source

Related

Use Global Parameters in Power Query

I have a lot of tables where in each of them there is an int filter. I want to simply change that value from editing the parameter here
and reference it from each query of each table like this
Is there any way I can do this?
I've tried searching into Power BI docs but I have not found anything yet
Yes. Either inject the parameter value into the query through string manipulation, like this or use the parameter in a filter in a subsequent step and let "query folding" do its job. I don't know if query folding works when using SQL against Kusto/ADX, so you may need to use KQL instead.
This query demonstrates both approaches, the ago condition is injected into the query text because it can't be folded around the original query, but the filter is added through folding, and will be evaluated by Kusto:
let
KQL = "
SomeTable
| where TIMESTAMP > ago(" & Text.From(WindowDays) & "d)
",
Data = AzureDataExplorer.Contents("yourcluster.kusto.windows.net", "yourdb", KQL, [CaseInsensitive=true]),
#"Filtered Rows" = Table.SelectRows(Data, each [SomeColumn] = "SomeValue")
in
#"Filtered Rows"
And if you don't further filter or select rows the query sent to Kusto will be
SomeTable
| where TIMESTAMP > ago(2d)
| where SomeColumn = "SomeValue"
But normally your report will add additional filters and select particular columns.

Change values from column in Power Query editor

I am working on a Power BI report and I am using a parquet file as the source. I have this column that holds info about the month:
As you see here, in MONTH_RUN, I have the data stored both as "202201" (yyyymm format) and "12/1/2021" (more like a date format).
I would need to change the values that appears like this: "12/1/2021" to "202112", so I can have all the values the same format.
I need to do this in a dynamic way, like searching for the rows that has values containing "/" character and based on that to change it into "yyyymm" format.
Is this possible?
I'm kinda new with power query and idk how should I implement this, but I would prefer to do it without creating any other additional columns.
Please, do not post sample data as images. It makes it difficult to answer your question.
One way to deal with this is to add a custom column, where the value will be either the same as MONTH_RUN (if it doesn't contain /), or it will parse the text to date, and then format it accordingly. For example:
if Text.Contains([MONTH_RUN], "/") then
Date.ToText(Date.FromText([MONTH_RUN], [Format="M/d/yyyy", Culture="en-US"]), [Format="yyyyMM", Culture="en-US"])
else
[MONTH_RUN]
Then use this custom column in your report.
I would just delete the existing MONTH_RUN column and create a new one based on the date in the DAY_RUN column.
eg:
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"MONTH_RUN"}),
#"Added Custom" = Table.AddColumn(#"Removed Columns", "MONTH_RUN", each Date.ToText([DAY_RUN],"yyyyMM"), type text)
Or, from the UI:

List Dates and customizing tables in Power Query

I'm trying to Add Custom Column in Power Query with the objective to return a Table from a List of dates.
The syntax used is as follows below:
= Table.AddColumn(TypeDate, "AddTable", each Table.FromList(
List.Dates([Date_begin],1,#duration(1,0,0,0)
)))
where:
TypeDate is the name of last step in Power Query
"AddTable" is the name of added custom column
[Date_begin] is a column with dates to be considered as the start of my list
Although the syntax seems correct, Power Query returns an error described as follows:
Expression.Error: We could not convert the value #date(2021, 1, 1) on to Text.
Details:
Value=01/01/2021
Type=[Type]
Does anyone know how to handle this problem?
I'll show an image where Power Query shows the error.
Select here to see Power Query interface
Your question is unclear
You want to add a column that has a table of dates for each row, using Date_Begin and Mes_Final?
#"Added Custom" = Table.AddColumn(TypeDate, "AddTable", each Table.TransformColumnTypes(Table.FromList({Number.From([Date_Begin])..Number.From([Mes_Final])}, Splitter.SplitByNothing(), {"date"}),{{"date", type date}}))

POWER BI - How to add manual columns/data to existing table instead of adding columns/data to the source csv file

The picture I have attached shows what my power query table looks like (exactly the same as source file) and then underneath what I would like the final end product to look like.
Correct me if I'm wrong but I thought the purpose of power query/power bi was to not manipulate the source file but do this in power query/power bi?
If that's the case, how can I enter new columns and data to the existing table below?
You can add custom columns without manipulating source file in power bi. Please refer to below link.
https://learn.microsoft.com/en-us/power-bi/desktop-add-custom-column
EDIT: Based on your comment editing my answer - Not sure if this helps.
Click on edit queries after loading source file to power bi.
Using 'Enter Data' button entered sample data you provided and created new table. Data can be copy pasted from excel. You can enter new rows manually. Using Tag number column to keep reference.
Merge Queries - Once the above table is created merged it with original table on tag number column.
Expand Table - In the original table expand the merged table. Uncheck tag number(as it is already present) and uncheck use original column name as prefix.
Now the table will look like the way you wanted it.
You can always change data(add new columns/rows) manually in new table by clicking on gear button next to source.
Here is the closest solution to what I found from "manual data entry" letting you as much freedom as you would like to add rows of data, if the columns that you want to create do not follow a specific pattern.
I used an example for the column "Mob". I have not exactly reproduced the content of your cells but I hope that this will not be an issue to understand the logic.
Here is the data I am starting with:
Here is the Power Query in which I "manually" add a row:
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Mob", each if [Tag Number] = "v" then null else null),
NewRows = Table.InsertRows(#"Added Conditional Column", 2, {[Mob="15-OHIO", Tag Number="4353654", Electronic ID=1.5, NLIS="", Date="31/05/2015", Live Weight="6", Draft="", Condition store="", Weighing Type="WEAN"]})
in
NewRows
1) I first created a column with only null values:
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Mob", each if [Tag Number] = "v" then null else null),
2) With the "Table.InsertRows" function:
I indicated the specific line: 2, (knowing that power Bi start counting at zero, at the "headers" so it will the third line in the file)
I indicated the column at which I wanted to insert the value, i.e "Mob"
I indicated the value that all other other rows should have:
NewRows = Table.InsertRows(#"Added Conditional Column", 2, {[Mob="15-OHIO", Tag Number="4353654", Electronic ID=1.5, NLIS="", Date="31/05/2015", Live Weight="6", Draft="", Condition store="", Weighing Type="WEAN"]})
Here is the result:
I hope this helps.
You can apply this logic for all the other rows.
I do not think that this is very scalable however, becaue you have to indicate each time the values of the rows in the other columns as well. There might be a better option.

Is it possible to ignore case sensitivity of column headings in Power Query?

In the M language is there a way to ignore case when using commands like Table.TransformColumnTypes? I had a column named "Task" that was renamed "TASK" and is now causing an Expression Error. I didn't expect column headings to be case sensitive since that's not normally an issue in SQL.
The step formula looks like this:
= Table.TransformColumnTypes(#"Removed Columns",{{"Task", type text}, {"Employee Name", type text}})
which returns this error:
Expression.Error: The column 'Task' of the table wasn't found.
Details:
Task
As I go through and adjust the case of each instance of "Task", I also get errors with these commands that are still referencing the "Task" field:
Table.Group
Table.RemoveColumns
Table.Sort
Table.TransformColumns
Table.TransformColumnTypes
Text.Combine
Note that I found an article about using Comparer.OrdinalIgnoreCase but that appears to apply to data in the table and not the column headings.
It can't be ignored,but you can rename all the column names by this formula:
= Table.RenameColumns(YourTableName,List.Transform(Table.ColumnNames(YourTableName),each {_,Text.Proper(_)}))
Comparer.OrdinalIgnoreCase only can be used in this condition:
= Text.Contains("abc","A", Comparer.OrdinalIgnoreCase) //returns true