ORDER A TEXT IN A COLUMN POWERQUERY - powerbi

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

Related

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)

Power Query - trying to remove a dynamic column that's created/updated when I access external data source

I have a table that's generated when I pull data from an accounting software - the example columns are months/years in the format as follows (It pulls all the way to current day, and the last month will be partial month data):
Nov_2020
Dec_2020
Jan_2021
Feb_1_10_2021 (Current month, column to remove)
... So on and so forth.
My goal I have been trying to figure out is how to use the power query editor to remove the last column (The partial month) - I tried messing around with the text length to no avail (The goal being to remove anything with text length >8, so the full months data would show but the last month shouldn't). I can't just remove based on a text filter, because if someone were to pull the data 1 year from now it would have to account for 2021/2022.
Is this possible to do in PQ? Sorry, I'm new to it so if I need to elaborate more I can.. Thanks!
You can do this with Table.SelectColumns where you use List.Select on the Table.ColumnNames.
= Table.SelectColumns(
PrevStep,
List.Select(Table.ColumnNames(PrevStep), each Text.Length(_) <= 8)
)
Although both Alexis Olson's and Justyna MK's answers are valid, there is another approach. Since it appears that you're getting data for each month in a separate column, what you will surely want to do is unpivot your data, that is transform those columns into rows. It's the only sensible way to get a good material for analysis, therefore, I would suggest to unpivot the columns first, then simply filter out rows containing the last month.
To make it dynamic, I would use unpivot other columns option - you select columns and it will transform remaining columns into row in such a way that two columns will be created - one that will contain column names in rows and the other one will contain values.
To illustrate what I mean by unpivoting, when you have data like this:
You're automatically transforming that into this:
You can try to do it through Power Query's Advanced Editor. Assign the name of the last column to LastColumn variable and then use it in the last step (Removed Columns).
let
Source = Excel.Workbook(File.Contents(Excel file path), null, true),
tblPQ_Table = Source{[Item="tblPQ",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(tblPQ_Table,{{"Nov_2020", Int64.Type}, {"Dec_2020", Int64.Type}, {"Jan_2021", Int64.Type}, {"Feb_1_10_2021", Int64.Type}}),
LastColumn = List.Last(Table.ColumnNames(#"Changed Type")),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{LastColumn})
in
#"Removed Columns"

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.