I wonder if it is possible in Power BI M:
I have a list of acronyms: "AAA, BBB, XXXX, YYY..."
Source data table has a text field (description) and can mention one or more of those in a free text. Something like "Luctus a a quam AAA gravida cum a YYY elementum potenti a ultrices p".
I need to select all records that have one or more acronym and generate additional custom column that would list all occurrences in a particular record ("AAA, YYY...").
It is a few lines of code in Excel VBA but I need to do it in PowerQuery or Power BI.
You can try this
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("DYhBCsAgDMC+UnreJ3rfB0Q8lK4MQZ3Tdu9fySVJzni6mG/g4HXuQERwL/7qxSDeY6eUQJt2HRY9Hwup8b3ZqqIbJpYjo+k2LOUH", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Column1"}, {{"ad", each _, type table [Column1=nullable text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let x = [ad],
#"Added Custom" = Table.AddColumn(x, "Custom", each {"AAA", "BBB", "XXXX", "YYY"}),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
#"Added Custom1" = Table.AddColumn(#"Expanded Custom", "Custom.1", each Text.Contains([Column1],[Custom])),
#"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Custom.1] = true)),
#"Removed Duplicates" = Table.Distinct(#"Filtered Rows", {"Column1"}),
#"Removed Columns" = Table.RemoveColumns(#"Removed Duplicates",{"Custom", "Custom.1"}),
#"Added Custom2" = Table.AddColumn(#"Removed Columns", "Custom", each #"Filtered Rows"[Custom]),
#"Extracted Values" = Table.TransformColumns(#"Added Custom2", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
Custom = #"Extracted Values"[Custom]
in
Custom),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"ad"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each (List.IsEmpty([Custom])=false)),
#"Extracted Values" = Table.TransformColumns(#"Filtered Rows", {"Custom", each Text.Combine(List.Transform(_, Text.From)), type text})
in
#"Extracted Values"
If I understand what you want, it's only two lines of Power Query code:
Create a List of the relevant acronyms
Match the contents of the string to the relevant acronym list items, and combine them with a delimiter:
M Code
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("FYw5DsAgDAS/sqLOJ6DOBxCisIgVEQE5sPP+ONpmZopNya1aRCfIdit1eO+xP/TWjVC0W44xght3HmJ+nWJQrWuTpxaeuFxekqOxN0YIAYxDp+j4v1zOHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
acronyms = {"AAA","BBB","CCC","DDD","EEE","YYY"},
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom",
each Text.Combine(
List.Select(
Text.Split([Column1]," "),
each List.Contains(acronyms,_)),", "), Text.Type)
in
#"Added Custom"
Related
Can Power query do this?
So I have a group of parent IDs. If the parent Ids are the same but the values from the corresponding attributes are different, I want PQ to let me know they can be grouped together.
Here is the example.
So Parent IDs 12345 are the same, and the values are different, I want the output to say SDSKU..Yes Then if the Parent IDs 333 are the same and values are the same, then that will not be a grouping and I want it to say NO. See image link
If you mean by "values" the values of the column "Color", try the M code below :
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Parent ID", Int64.Type}, {"Kitchen Sink", Int64.Type}, {"Color", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Parent ID", "Kitchen Sink"}, {{"AllData", each _, type table [Parent ID=nullable number, Kitchen Sink=nullable number, Color=nullable text]}, {"OccuID", each Table.RowCount(_), Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "NumberOfColors", each List.Count(List.Distinct([AllData][Color]))),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "SDSKU", each if [OccuID] = [NumberOfColors] then "Yes" else "No"),
#"Expanded AllData" = Table.ExpandTableColumn(#"Added Custom1", "AllData", {"Kitchen Sink", "Color"}, {"Kitchen Sink.1", "Color"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded AllData",{"OccuID", "NumberOfColors"})
in
#"Removed Columns"
If "attributes" are the value of every column except the one named Parent ID, try the M code below :
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source , {"Parent ID"}, {
{"data", each _, type table },
{"check", each if Table.RowCount(_) = Table.RowCount(Table.Distinct(_, List.Difference(Table.ColumnNames(_),{"Parent ID"}))) then "YES" else "NO"}}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", List.Difference(Table.ColumnNames(Source),{"Parent ID"}), List.Difference(Table.ColumnNames(Source),{"Parent ID"}))
in #"Expanded data"
I have been searching for a week now and cannot find a resolution to my problem. I have a table which lists the "event" and individual is in during a certain week. I want to add a column - via PowerQuery - that will count the number of weeks a person has been in that event and then resets if the event changes in the following week. For example...
Pers1
Date
Event
Weeks in Event
Pers1
12/22/2022
Consideration
1
Pers1
12/26/2022
Consideration
2
Pers1
1/05/2022
Interview
1
Pers1
1/12/2022
Consideration
1
Pers1
1/19/2022
Consideration
2
Pers1
1/26/2022
Awaiting Hire
1
Pers2
1/19/2022
Awaiting Hire
1
Pers2
1/26/2022
Awaiting Hire
2
Note how the count resets back to starting at 1 when Pers1 has their second stint of Consideration during weeks 1/12 and 1/19. Additionally, I need the solution to be smart enough to distinguish between two different individuals and uniquely count their time in an event.
This community has always come through for me. Please help!
EDIT 1: I incorporated the code provided by Ron and am receiving the following error: Expression.Error: 5 arguments were passed to function which expects between 2 and 4.
Details:
Pattern=
Arguments=List
PQ Advanced Editor Code is below:
let
Source = Excel.Workbook(File.Contents("C:\Location"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Pers1", type text}, {"Date", type date}, {"Event", type text}}),
//add an offset column for Pers and Event to do easy comparison with previous row
offsetPersonEvent=Table.FromColumns(
Table.ToColumns(#"Changed Type")
& {List.RemoveFirstN(#"Changed Type"[Pers1]) & {null}}
& {List.RemoveFirstN(#"Changed Type"[Event]) & {null}},
type table[Pers=text, Date=date,Event=text,offsetPers=text, offsetEvent=text]
),
//create "grouper" column by checking where both Pers and Event change
#"Added Index" = Table.AddIndexColumn(offsetPersonEvent, "Index", 0, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "groups",
each if [Pers]=[offsetPers] and [Event]=[offsetEvent] then null else [Index]),
//remove unneeded columns, fillUp the grouper, and group by "grouper"
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"offsetPers", "offsetEvent", "Index"}),
#"Filled Up" = Table.FillUp(#"Removed Columns",{"groups"}),
//Add Index column to each subtable
#"Grouped Rows" = Table.Group(#"Filled Up", {"groups"}, {
{"addedIndex", each Table.AddIndexColumn(_,"Weeks in Event",1,1,Int64.Type)
, type table}}),
//Remove unneccessary columns
// Expand the grouped tables
// reset the data types
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"groups"}),
#"Expanded addedIndex" = Table.ExpandTableColumn(#"Removed Columns1", "addedIndex", {"Pers", "Date", "Event", "Weeks in Event"}, {"Pers", "Date", "Event", "Weeks in Event"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded addedIndex",{{"Pers", type text}, {"Date", type date}, {"Event", type text}, {"Weeks in Event", Int64.Type}})
in
#"Changed Type1"
Assuming your data is sorted by Person and then by Date, as you show, you can use the following M-Code.
(If your data is not so sorted, then you could merely add steps initially to sort it appropriately, and then continue with the code shown)
Please read the code comments and examine the Applied steps to understand the algorithm
Open the Advanced Editor and paste in the code below
let
//change table name in next line to your actual table name
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Pers1", type text}, {"Date", type date}, {"Event", type text}}),
//add an offset column for Pers and Event to do easy comparison with previous row
offsetPersonEvent=Table.FromColumns(
Table.ToColumns(#"Changed Type")
& {List.RemoveFirstN(#"Changed Type"[Pers1]) & {null}}
& {List.RemoveFirstN(#"Changed Type"[Event]) & {null}},
type table[Pers=text, Date=date,Event=text,offsetPers=text, offsetEvent=text]
),
//create "grouper" column by checking where both Pers and Event change
#"Added Index" = Table.AddIndexColumn(offsetPersonEvent, "Index", 0, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "groups",
each if [Pers]=[offsetPers] and [Event]=[offsetEvent] then null else [Index]),
//remove unneeded columns, fillUp the grouper, and group by "grouper"
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"offsetPers", "offsetEvent", "Index"}),
#"Filled Up" = Table.FillUp(#"Removed Columns",{"groups"}),
//Add Index column to each subtable
#"Grouped Rows" = Table.Group(#"Filled Up", {"groups"}, {
{"addedIndex", each Table.AddIndexColumn(_,"Weeks in Event",1,1,Int64.Type)
, type table}}),
//Remove unneccessary columns
// Expand the grouped tables
// reset the data types
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"groups"}),
#"Expanded addedIndex" = Table.ExpandTableColumn(#"Removed Columns1", "addedIndex", {"Pers", "Date", "Event", "Weeks in Event"}, {"Pers", "Date", "Event", "Weeks in Event"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded addedIndex",{{"Pers", type text}, {"Date", type date}, {"Event", type text}, {"Weeks in Event", Int64.Type}})
in
#"Changed Type1"
Ron's code worked with one minor tweak. For me, the following section of code was causing an error
//Add Index column to each subtable
#"Grouped Rows" = Table.Group(#"Filled Up", {"groups"}, {
{"addedIndex", each Table.AddIndexColumn(_,"Weeks in Event",1,1,Int64.Type)
, type table}}),
I removed the Int64.Type parameter from the Table.AddIndexColumn and everything functioned. I've included the updated code snip-it below:
let
Source = Excel.Workbook(File.Contents("C:\Location"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Pers1", type text}, {"Date", type date}, {"Event", type text}}),
//add an offset column for Pers and Event to do easy comparison with previous row
offsetPersonEvent=Table.FromColumns(
Table.ToColumns(#"Changed Type")
& {List.RemoveFirstN(#"Changed Type"[Pers1]) & {null}}
& {List.RemoveFirstN(#"Changed Type"[Event]) & {null}},
type table[Pers=text, Date=date,Event=text,offsetPers=text, offsetEvent=text]
),
//create "grouper" column by checking where both Pers and Event change
#"Added Index" = Table.AddIndexColumn(offsetPersonEvent, "Index", 0, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "groups",
each if [Pers]=[offsetPers] and [Event]=[offsetEvent] then null else [Index]),
//remove unneeded columns, fillUp the grouper, and group by "grouper"
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"offsetPers", "offsetEvent", "Index"}),
#"Filled Up" = Table.FillUp(#"Removed Columns",{"groups"}),
//Add Index column to each subtable
#"Grouped Rows" = Table.Group(#"Filled Up", "groups", {
{"addedIndex", each Table.AddIndexColumn(_,"Weeks in Event",1,1)
, type table}}),
//Remove unneccessary columns
// Expand the grouped tables
// reset the data types
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"groups"}),
#"Expanded addedIndex" = Table.ExpandTableColumn(#"Removed Columns1", "addedIndex", {"Pers", "Date", "Event", "Weeks in Event"}, {"Pers", "Date", "Event", "Weeks in Event"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded addedIndex",{{"Pers", type text}, {"Date", type date}, {"Event", type text}, {"Weeks in Event", Int64.Type}})
in
#"Changed Type1"
I frequently have large datasets in powerquery where I need to remove/filter out the same row, as well as the following 13 whenever a certain value, in this case "Page" occurs. This occurs multiple times throughout the column.
I've tried referring to the next/previous rows by adding an index column and {[Index]+1} shenanigans but that either didn't work or took 15+ minutes to load.
I've tried setting up something with Table.RemoveFirstN(Text.Contains([Column], "Page"), 13) but that just errored out.
Would anyone know how I could filter the row where a value occurs, as well as the next n rows (index?) in Powerquery?
Kind regards,
This seems to work ok
We add an index. Test for "Page". In a new column, if Page is present, copy over the index. Fill down then group on that. Add 2nd index to the grouping. Expand all columns. Filter out anything where 2nd index is <14. Remove extra columns
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Merged Price Country", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each try if Text.Contains([Merged Price Country],"Page") then [Index] else null otherwise null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
mGroup = Table.Group(#"Filled Down", {"Custom"}, {{"Data", each Table.AddIndexColumn(_, "Index2", 1, 1), type table}}),
#"Removed Columns" = Table.RemoveColumns(mGroup,{"Custom"}),
// expand all columns
List = List.Union(List.Transform(#"Removed Columns"[Data], each Table.ColumnNames(_))),
#"Expanded Data" = Table.ExpandTableColumn(#"Removed Columns", "Data", List,List),
#"Filtered Rows" = Table.SelectRows(#"Expanded Data", each [Custom]=null or [Index2] > 14),
#"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"Index", "Custom", "Index2"})
in #"Removed Columns1"
I skipped out on using Table.RemoveFirstN() on the groupings in code above case there are leading rows you want to keep, but you could use that instead of adding the 2nd index and filtering like below
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Merged Price Country", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each try if Text.Contains([Merged Price Country],"Page") then [Index] else null otherwise null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
mGroup = Table.Group(#"Filled Down", {"Custom"}, {{"Data", each Table.RemoveFirstN(_, 13), type table}}),
#"Removed Columns" = Table.RemoveColumns(mGroup,{"Custom"}),
// expand all columns
List = List.Union(List.Transform(#"Removed Columns"[Data], each Table.ColumnNames(_))),
#"Expanded Data" = Table.ExpandTableColumn(#"Removed Columns", "Data", List,List),
#"Removed Columns1" = Table.RemoveColumns(#"Expanded Data",{"Index", "Custom"})
in #"Removed Columns1"
Different approach. Wonder which might be faster:
Create a list of rows to be removed (by row number)
Select the rows not in that list
let
Source = Excel.CurrentWorkbook(){[Name="Table12"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Text", type text}, {"Data", Int64.Type}}),
//Add index column
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
//create list rows to be removed
textCol = List.Transform(#"Added Index"[Text], each
if _ = null then null
else if Text.Contains(_,"Page",Comparer.OrdinalIgnoreCase) then "RemoveMe"
else _),
//create list of positions to be removed
removePos = List.Combine(List.Transform(List.PositionOf(textCol,"RemoveMe",Occurrence.All), each {_..List.Min({_+13, List.Count(textCol)})})),
//Filter the table using the "RemoveMe" list
filter = Table.SelectRows(#"Added Index", each not List.Contains(removePos,[Index])),
#"Removed Columns" = Table.RemoveColumns(filter,{"Index"})
in
#"Removed Columns"
I have a question about my M code. I am currently working in Visual Studio and 'Editing Expressions' via VSDT. The problem that I have is that I am trying to create a relationship between one dimension table and another fact table using a primary key. The primary key in the Dim table has unique values, or so it seems. I have removed nulls and blanks in the primary key column. I started off creating the PK by concatenating three columns shared between the two tables. Further, I have removed duplicates and run this command in the PQ editor. I am puzzled as to why, when I join the two tables, I get an error message saying that my Dim table does not have unique values. More frustrating is when I sort the data ascending and descending and I find no blanks, where there once was a blank. I am puzzled about how to fix this problem. I've tried 16 different ways to Sunday-- even moving some of the query steps around. To no avail...
The error thrown:
Error: Failed to save modifications to the server. Column Style_List_PK in Table dStyles contains blank values and this is not
allowed for columns on the one-side of a one-to-many relationship or
for columns that are used as a primary key of a table.
The code:
let
Source = #"SQL/sbsqld59;PJ",
dbo_Style_List = Source{[Schema="dbo",Item="Style_List"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(dbo_Style_List,{{"Brand", type text}, {"Style", type text}, {"Style Description", type text}, {"Sizes", type any}, {"Style Name", type text}, {"General Category", type text}, {"Style Category", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Sizes", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Sizes.1", "Sizes.2", "Sizes.3", "Sizes.4", "Sizes.5", "Sizes.6", "Sizes.7", "Sizes.8", "Sizes.9"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Sizes.1", type text}, {"Sizes.2", type text}, {"Sizes.3", type text}, {"Sizes.4", type text}, {"Sizes.5", type text}, {"Sizes.6", type text}, {"Sizes.7", type text}, {"Sizes.8", type text}, {"Sizes.9", type text}}),
#"Replaced Errors" = Table.ReplaceErrorValues(#"Changed Type1", {{"Sizes.1", "NULL"}, {"Sizes.2", "NULL"}, {"Sizes.3", "NULL"}, {"Sizes.4", "NULL"}, {"Sizes.5", "NULL"}, {"Sizes.6", "NULL"}, {"Sizes.7", "NULL"}, {"Sizes.8", "NULL"}, {"Sizes.9", "NULL"}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Replaced Errors", {"Brand", "Style", "Style Description", "Style Name", "General Category", "Style Category"}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value", "Size"}}),
#"Replaced Value" = Table.ReplaceValue(#"Renamed Columns","","NULL",Replacer.ReplaceValue,{"Brand", "Style", "Style Description", "Style Name", "General Category", "Style Category", "Size"}),
#"Uppercased Text" = Table.TransformColumns(#"Replaced Value",{{"Style", Text.Upper, type text}, {"Brand", Text.Upper, type text}, {"Size", Text.Upper, type text}}),
#"Added Custom" = Table.AddColumn(#"Uppercased Text", "Style_List_PK", each Text.Combine({[Brand],[Style],[Size]})),
#"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"Style_List_PK", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type2",{{"Style_List_PK", Text.Trim, type text}}),
#"Removed Duplicates" = Table.Distinct(#"Trimmed Text", {"Style_List_PK"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Duplicates", each [Style_List_PK] <> null and [Style_List_PK] <> "" and [Style_List_PK] <> " " and [Style_List_PK] <> " ")
in
#"Filtered Rows"
I'm trying to figure out a simple way (using PowerQuery) to convert this:
into this:
I've spent days trying to figure out a simple way to do it.
Everything I've tried has failed.
You can use Group By on the Transform tab, group by project and define a aggregation for each of the segment columns (e.g. Sum).
Then adjust the created code from List.Sum to List.RemoveNulls.
Then add a column with nested tables from the segment columns, using Table.FromColumns.
Remove the original segment columns and expand the nested tables.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project", type text}, {"Segment1", type text}, {"Segment2", type text}, {"Segment3", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Project"}, {{"Segment1", each List.RemoveNulls([Segment1]), type text}, {"Segment2", each List.RemoveNulls([Segment2]), type text}, {"Segment3", each List.RemoveNulls([Segment3]), type text}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Tabled", each Table.FromColumns({[Segment1],[Segment2],[Segment3]},{"Segment1","Segment2","Segment3"})),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Segment1", "Segment2", "Segment3"}),
#"Expanded Tabled" = Table.ExpandTableColumn(#"Removed Columns", "Tabled", {"Segment1", "Segment2", "Segment3"}, {"Segment1", "Segment2", "Segment3"})
in
#"Expanded Tabled"