Replace string value in PowerBI using a wildcard - powerbi

I have a list of Points below that are in a column and i am trying to find all and replace them with just MaTmp. I want to do like in excel with find and replace and use MaTmp and replace with MaTmp. Any ideas?
Points
MaTmp01
MaTmp02
MaTmp1
MaTmp-1
MaTmp2
MaTmp-2
MaTmp3
MaTmp-3

Here's one way using Power Query M Code:
Paste the code into a blank query and examine the Applied Steps to better understand what's happening
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8k0MyS0wMFSK1YGxjRBsJGFdJDaSCl0ktjGSMJAdCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Points = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Points", type text}}),
MaTmp = Table.TransformColumns(#"Changed Type", {"Points", each if Text.StartsWith(_,"MaTmp") then "MaTmp" else _})
in
MaTmp

Related

How to remove the part of text in one column that matches the value of another column?

I have a table with a column of email addresses and another column of unique IDs. For some reason, some of the values in the email address are prefixed with the unique IDs so I need to do some cleanup of the email address column by removing the unique ID prefix from the email address column. The length of the unique ID can vary, so it's not as straight forward as the only method I know of simply removing the first x amount of characters:
email
unique ID
george#hotmail.com
79fsdfv8v2657f842356j0
784ret3956w35497er834r62mary#hotmail.com
784ret3956w35497er834r62
34gfng6w5h6pauly#outlook.com
34gfng6w5h6
Resulting column:
george#hotmail.com
mary#hotmail.com
pauly#outlook.com
The solution has to be in m code, not DAX. Any ideas?
you can do it on the power query side...
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dYzNCgIhEIBfJea8RKgz6m3fQzxIqVtpE667y759EARBdP5+nIMcueU4TtxruJbjmesBBtA2zZe0mlUQ6mSUkEi3E/jBgTaqxS4t0iZRWR2bkaqRqKHtP5s/7nskVU6PTBtO9AxL2UdeemG+f+IvDt6/AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"email " = _t, #"unique ID" = _t]),
#"Duplicated Column" = Table.DuplicateColumn(Source, "email ", "email - Original"),
#"Replaced Value" = Table.ReplaceValue(#"Duplicated Column",each [unique ID],"",Replacer.ReplaceText,{"email "}),
#"Renamed Columns" = Table.RenameColumns(#"Replaced Value",{{"email ", "Result"}}),
#"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"unique ID", "email - Original", "Result"})
in
#"Reordered Columns"
sample file as solved above...
I found this answer and their solution solved my problem:
https://community.powerbi.com/t5/Power-Query/Dynamically-remove-string-from-one-column-if-string-is-contained/td-p/1809532
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
CLEAN = Table.ReplaceValue(Table.TransformColumns(Table.AddColumn(Source, "All ICD-10 Replaced",
each if Text.Contains([#"All ICD-10"], [#"ICD-10"])
then Text.Replace([#"All ICD-10"], [#"ICD-10"], "")
else [#"All ICD-10"]),
{{"All ICD-10 Replaced", Text.Trim, type text}}),
" "," ",Replacer.ReplaceText,{"All ICD-10 Replaced"})
in
CLEAN

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

Power Query - Find and replace in one step instead of several

I have a simple column here.
I am using Power Query to replace some values,
1 becomes 119, 2 becomes 201, 3 becomes 321
There are no particular logic to it, it's to correct the mistyped numbers by users.
I did a find a replace function.
Table.ReplaceValue(#"Changed Type",1,119,Replacer.ReplaceValue,{"Name"}) which worked great.
I want to combine the other find and replace functions into one single step for cleaner code.
I tried this but didn't work.
Table.ReplaceValue(#"Changed Type",{1,2},{119,201},Replacer.ReplaceValue,{"Name"})
I don't want to have 10 different find and replace steps in my PQ just want to have one step.
OPTION-1
Here below is the M code for all replace together-
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
#"AllReplace" = [
#"1" = "119",
#"2" = "201",
#"3" = "321"
],
#"Replaced Value" = Table.TransformColumns(Source,{{"Column1",each Record.FieldOrDefault(AllReplace,_,_)}})
in
#"Replaced Value"
Note: Need to number column as Text. You can convert the column to Number after the Replace Step.
Input
Output
OPTION-2:
You can also create a custom column using the below code-
if [Column1] = 1 then 119 else if [Column1] = 2 then 201 else 321
After creating the custom column, you can keep or remove the source coulmn.

Define report in PowerBI where columns change over time

I have a HTML file that is generated daily. Over the past few years we have added a couple of columns to the HTML table in the file. What I want to do is generate some reports that trend over time based on that HTML, so I want to define a single query for a report, but get a null/default value when the column isn't present in the source.
I have a list of report dates that are available and then I can add copies of the report data to a master report. The data source however fails to load if the column isn't present in the older reports. Essentially I read a date from one HTML file as an input, then modify the fetch URL per row for the source to get the historical data.
Is it possible to generate this report without retrospectively changing the old data and adding the column that is missing? I couldn't see how to do this easily.
I've never done anything like that, but maybe something like this will help.
It checks if a column exists and if it does not then create a new column with a default value.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUpUitWJVjICspLALGMgK1kpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [AA = _t, BB = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"AA", Int64.Type}, {"BB", type text}}),
#"Custom1" = if Table.HasColumns(#"Changed Type",{"MissingColumn1"}) = false
then Table.AddColumn(#"Changed Type","MissingColumn1", each "<n/d>")
else #"Changed Type"
,#"Custom2" = if Table.HasColumns(#"Custom1",{"MissingColumn2"}) = false
then Table.AddColumn(#"Custom1","MissingColumn2", each "<n/d>")
else #"Custom1"
,#"Custom3" = if Table.HasColumns(#"Custom2",{"MissingColumn3"}) = false
then Table.AddColumn(#"Custom2","MissingColumn3", each "<n/d>")
else #"Custom2"
in
#"Custom3"
If this is a suitable solution depends on how data are fetched from the source.

Power BI Table Transformation

I am trying to find a way in Power BI to transform a table into a specific format for a custom visualization. The transformation that I'd like to do is the following image:
Is this possible to do? I've thought of unpivoting or transposing but haven't seemed to figure out how to get past this point.
I do not really understand the use case of this transformation, but you can achieve what you want by copying the source table, remove all columns except 2, and then append all copies.
Let's say we have one table, named Table, like this:
Make 3 copies of the table, by clicking New Source -> Blank Query:
and enter =Table as query text, where Table is the name of our source table. If our queries are named Query1, Query2 and Query3, then click on each of them and remove the extra column and keep A and B in Query1, keep B and C in Query2, and C and D in Query3. Rename the remaining columns to be named the same way in all 3 queries and click Append Queries or Append Queries as New, then select the 3 copes:
This will give you the result you ask for:
Or you could do it in one go, without creating three different queries. Makes your Queries pane a bit nitter
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXICYmcgdlGKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [c1 = _t, c2 = _t, c3 = _t, c4 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"c1", type text}, {"c2", type text}, {"c3", type text}, {"c4", type text}}),
_t1 = Table.SelectColumns(#"Changed Type", {"c1", "c2"}),
_t1_r = Table.RenameColumns(_t1, {{"c1", "ca"}, {"c2", "cb"}}),
_t2 = Table.SelectColumns(#"Changed Type", {"c2", "c3"}),
_t2_r = Table.RenameColumns(_t2, {{"c2", "ca"}, {"c3", "cb"}}),
_t3 = Table.SelectColumns(#"Changed Type", {"c3", "c4"}),
_t3_r = Table.RenameColumns(_t3, {{"c3", "ca"}, {"c4", "cb"}}),
_res = Table.Combine({_t1_r, _t2_r, _t3_r})
in
_res
One use case I can think of this transformation will be useful is that, when we have a time series data like this, and we want to compare the values of current and last periods.
So here is a version for the case when the number of columns is variable.
let
Source = ...,
SingleRow = Table.SingleRow(Source),
Values = Record.FieldValues(SingleRow),
Pairs = List.Zip({List.RemoveLastN(Values, 1), List.RemoveFirstN(Values, 1)}),
Result = Table.FromRows(Pairs, {"Last", "Current"})
in
Result
I am implying the "rules" for this transformation are then you want a table where the first column and second column form each "pair" of items in the original set. Given the data loaded into Power Query like this:
I would transpose the data:
Then make two references to the query. In the first one remove the last row, in the second one remove the first row. Then add an index column to both.
(result of the first reference looks like this)
Then just do a merge between the two references and you get the two column outputs show in the example.