Table.ExpandTableColumn first row only - powerbi

I want to use Power Query to extract a related table using Table.ExpandTableColumn. Instead of expanding the entire related table I would like to expand only the first row.
In the attempt below, I am extracting the entire "Name" column.
= Table.ExpandTableColumn(#"Changed Type", "ToRecipients", {"Name"}, {"ToRecipients.Name"})
Any ideas?
Paul

How would it work wit this?
Table.ExpandTableColumn(
// Retaining only first rows in the nested tables
Table.TransformColumns(#"Changed Type", {"Name", each Table.FirstN(_, 1)}),
"ToRecipients", {"Name"}, {"ToRecipients.Name"}
)
Alternatively,
Table.ExpandRecordColumn(
// Retaining only first rows in the nested tables as 'records'
Table.TransformColumns(#"Changed Type", {"Name", Table.First}),
"ToRecipients", {"Name"}, {"ToRecipients.Name"}
)
Both works just the same. Later one may be looking simpler a bit?

Guess it should have been like below;
= Table.ExpandTableColumn( Table.TransformColumns(
#"Changed Type", {"ToRecipients", each Table.FirstN(_, 1)}),
"ToRecipients", {"Name"}, {"ToRecipients.Name"})
...
Table.ExpandRecordColumn(
// Retaining only first rows in the nested tables as 'records'
Table.TransformColumns(#"Changed Type", {"ToRecipients", Table.First}),
"ToRecipients", {"Name"}, {"ToRecipients.Name"})

Related

How to unpivot specific columns of the table using DAX?

Hey, so a novice here. I dont want the data to be leaked so I have hidden it and marked it with tags though.
Basically what you are looking at is the accuracy data of a classification model. As you can see, for some classes like "A" it predicts all data points correctly, thus it is flagged as True, but we dont have a False column for A because there are no False values. For some reason when we created this table in DAX by grouping a couple of columns and performing the count operation on different table,DAX didnt show False for any values that were completely True.
On the other hand, for "D", 185 values are true and 2 values are false.
We need to showcase this as like a bar chart with maybe a slicer for the different categories. For that we first need the "False" attribute for the values that predicted completely true, then we need to compute the percentage of False and True predictions for each class and then we need to show that in a bar chart.
We are thinking we should pivot the "FLAG" and the "Predicted Values" column for this, and then we will be able to do all the things aforementioned. But because this table was created using DAX from another table, we are unable to find a way to pivot it. It wont evens how up in the Power Query editor. Any tips or help are welcomed. Sorry again, Im a novice so just learning as I work..
You don't need to pivot. You need a flag dimension table joined to your existing table on flag. The dimension contains just two values for true and false and the cardinality is one-to-many unidirectional. Then on your bar chart, make sure to check show items with no data in the field well.
Starting with : (Using enter data and copy paste from excel)
Adding the following code in the advanced editor under Transforme Data:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUQoJCnUFUkbmJsYWSrE60UpJCEFDAzOwUDJCyMTUFCyUgqTKAiHk5ugTDDYOLJKKpMjMBCyUhmy6BVwIps8QLJKOpMjUHCyUgSRkbAJXBdNnAnFoJpIQxFFZqI6KBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [category = _t, Flag = _t, Value = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"category", type text}, {"Flag", type logical}, {"Value", Int64.Type}}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Changed Type", {{"Flag", type text}}, "en-SE"), List.Distinct(Table.TransformColumnTypes(#"Changed Type", {{"Flag", type text}}, "en-SE")[Flag]), "Flag", "Value", List.Sum)
in
#"Pivoted Column"
Gives:
Which is what I assume you are after

ORDER A TEXT IN A COLUMN POWERQUERY

I would like to know if you can assist me in the following matter:
in PowerQuery I have a column like this:
input
and i would like to Sort the column "CANAL / BU" like this:
I tried to sort the function but sort the entire column not the text.
could you guys please so kind to assist me?
regards,
From your example, it appears you only want to sort the text in a single column.
You can do that by:
Split each text line by the "/" into a List.
Sort the List
Recombine the List using the "/" delimiter.
A line of M Code that will do that:
#"Sort Column" = Table.TransformColumns(#"Previous Step",
{"CANAL/BU", each Text.Combine(List.Sort(Text.Split(_,"/")),"/")})
Of course, you could also do this by adding a custom column and sorting there; but with the Table.TransformColumns method, you don't need the extra column.
Here is code that will reproduce the first few columns of your table, and show the sorting step in situ:
let
Source = Table.FromRecords(
{
[AREA="Channel Marketing",
#"CANAL/BU"="WT/NT/O",
GPID=70181170]
}
),
#"Change Type" = Table.TransformColumnTypes(Source,{{"AREA", type text}, {"CANAL/BU", type text}, {"GPID", Int64.Type}}),
#"Sort Column" = Table.TransformColumns(#"Change Type",
{"CANAL/BU", each Text.Combine(List.Sort(Text.Split(_,"/")),"/")})
in
#"Sort Column"
Output

Is there a way using PowerBI to use a if then within Table.SplitColumn function

This relates to PowerBI:
I have this code (mcode not DAX):
#"Split Column by Delimiter3" = Table.SplitColumn(#"Reordered Columns" , "Custom2", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), ListB),
Custom2 column
Which creates multiple columns from this list with data from the Custom2 column.
List
and this is the result
Result of Custom2 & List
Now I want to add if the result in each cell is blank or null then 0 (zero) else $200.
so the result word look like
Result Wanted
Given that the code creates multiple columns at once what mcode do i need to wrap around or put inside the existing code for #"Split Column by Delimiter3"?
I can do this manually but the whole point is to make it dynamic.
I have tried List.ReplaceValue(Text.Split([Custom2],","),"0","$200", Replacer.ReplaceText))as a text does work but i don't know how to make it replace any non blank with $200
Any help would be appreciated.
Thanks
Update: I found a solution myself:
1st step
use #"Added Custom13" =Table.AddColumn(#"Duplicated Column4", "CustomTest", each List.Transform(Text.Split([Custom3Prep],","),each "$200")),
2nd step
use #"Extracted Values1" = Table.TransformColumns(#"Added Custom13", {"CustomTest", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
All the items within the delimiter were replaced with $200.:)

How to turn 4.1M 1.22B 3K to numbers in power bi

my data number came in text form, for example: 4.1M 1.22B 3K
Is there a way to turn them back into numbers like 4100000 for 4.1M
Thanks in advance
If you are using card visualization you have feature to change count for thousands, millions etc. It depend on visualization.
You can follow the following steps:
Select the visual where you want to change this notation
Click on the paint roller in the Visualisations pane
You have already activated labels, expand this part
Here you can choose how to show values, select whatever you want
Keep in mind however that big numbers are hard to read and easy to read wrong. It is worth considering to keep them as 4.1M and 1.22B since it keeps them nice and compact. This is especially the case if you use visualisations that grow over time, where overlap might occur easily if you have written-out numbers.
Also, refer to the following documentation for explanation on the Microsoft website:
https://learn.microsoft.com/en-us/power-bi/visuals/power-bi-visualization-customize-title-background-and-legend
Here is an example of how to clean data in Power Query. It is a bit cumbersome, and personally I would contact whoever is responsible for my data source and ask if I could get data in a better format.
The idea is to first figure out what type of multiplier you want to apply to each row, then clean out the "foreign" symbols in the Sample Value column - this is generated using a nifty trick with Character.FromNumber, see link: https://www.excelguru.ca/blog/2015/11/19/keep-only-numbers-in-power-query/ )after that it is simply to convert the original column to a number (might have to remove #"Replaced Value" step depending on your locale) and create a final column that multiplies the number with the multiplier.
You can paste the below into a blank query to see for yourself:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtEz9FWK1YlWMtQzMnICs4y9IQLGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Sample Values" = _t]),
#"Added Conditional Column" = Table.AddColumn(Source, "Custom", each if Text.Contains([Sample Values], "B") then Number.Power(10,9) else if Text.Contains([Sample Values], "M") then Number.Power(10,6) else if Text.Contains([Sample Values], "K") then Number.Power(10,3) else 1),
CharsToRemove = List.Transform({33..45,47,58..126}, each Character.FromNumber(_)),
#"Added Custom" = Table.AddColumn(#"Added Conditional Column", "Result", each Text.Remove([Sample Values],CharsToRemove)),
#"Replaced Value" = Table.ReplaceValue(#"Added Custom",".",",",Replacer.ReplaceText,{"Result"}),
#"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Result", type number}}),
#"Added Custom1" = Table.AddColumn(#"Changed Type", "Result Value", each [Result]*[Custom], type number),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Result Value"})
in
#"Removed Other Columns"

M Split the string and copy to new column

Need some DAX help with the below data.
I have Columns Uniq ID and Controls.
I want to split the Controls column as shown in New Column.
The string will always start with "AON" and a total of 7 characters (i.e AON0913)
Thanks for help
Here is the Power Query solution. I added comments to understand what each step is doing.
Input Table:
Uniq ID
Controls
RIS123
{AON0913: test 1}, {AON0S18: Safety glasses complying to AS/NZS 1337.1:2010}, {AON0937: Wearing of protective gloves}, {AON0938: First Aid }
RIS345
{AON0913: test 1}, {AON0937: Wearing of protective gloves}, {AON0939: Some SCO's may already have up-to-date vaccinations. }, {AON0938: First Aid }
RIS3223
{AON0S18: Safety glasses complying to AS/NZS 1337.1:2010}, {AON0937: Wearing of protective gloves}, {AON0928: Safety and Compliance uniform Specifications 12-2017 }
RIS0456
{AON0912: test }, {AON0941: controlling test 4321234 }
Power Query M Code:
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
//Add an index so we know the original amount of rows
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
//Add a column to create a list of Split strings after ":", the trailing delimiter. This will create a list with multiple rows.
#"Add Column" = Table.AddColumn(#"Added Index", "Split Colon", each Text.Split([Controls],":")),
//Expand the new rows
#"Expanded Split Colon" = Table.ExpandListColumn(#"Add Column", "Split Colon"),
//Filter out any Row that does not contain "AON0"
#"Filtered Rows" = Table.SelectRows(#"Expanded Split Colon", each Text.Contains([Split Colon], "AON0")),
//Extract the text after "AON0", which will be the three digits. Prefix it with "AON0" to create the full string. This is in case it does not have a leading "{" delimiter.
#"Extracted Text After Delimiter" = Table.TransformColumns(#"Filtered Rows", {{"Split Colon", each "AON0" & Text.AfterDelimiter(_, "AON0"), type text}}),
//Group by the Index that we created earlier. We will group all existing columns together as well as create a new column with a comma delimited string for our Output
#"Grouped Rows" = Table.Group(#"Extracted Text After Delimiter", {"Index"}, {{"All Rows", each _, type table [Uniq ID=nullable text, Controls=nullable text, Index=number, Split Colon=text]}, {"Output", each Text.Combine([Split Colon], ", "), type text}}),
//Expand our groups
#"Expanded All Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All Rows", {"Uniq ID", "Controls"}, {"Uniq ID", "Controls"}),
//Remove any duplicated rows
#"Removed Duplicates" = Table.Distinct(#"Expanded All Rows")
in
#"Removed Duplicates"
Output Table:
You can remove the Index column after you remove the duplicates if it is not needed.
Power BI does not have any native functions in the Power query to perform any RegEx operations (which may be most useful here. Please vote ). What you can do is to use build-in option
Column From Examples; You can here type the desired result in a few rows (where you have the longest example - Max AON occurrence. ) and powerbi should indicate what you want (probably the should use multiple time splitbydelimiter + data.combine function + substring)