The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
Hello,
I need help with the following step I need to take in the Query Editor for which I think I need the right M code:
I have two tables:
Table 1:
URL | Description |
https://www.mywebpage.com/my-page-keyword1-home/start | My start page |
https://www.otherpage.com/Keyword2/start/home.html | My home page |
etc |
Table 2
Code | Keyword |
10001 | Keyword1 |
10002 | Keyword2 |
etc |
Desired version of table 1
URL | Description | Code |
https://www.mywebpage.com/my-page-keyword1-home/start | My start page | 10001 |
https://www.otherpage.com/Keyword2/start/home.html | My home page | 10002 |
etc |
So based on the keyword being found in the URL string, I would like to lookup the code of the keyword. There is always only one keyword in the URL string.
Who can help me with finding the M code to do this in the Query Editor?
Thanks for the help!!!!
Solved! Go to Solution.
Hello @Anonymous
for sure you have to connect to your real data source. but as I don't know where your data is coming from and I probably have also no access to it, I had to simulate your data with some manual code. You have to change my code in order that its connecting to your data source
let
Data = YourQueryToYourDataSourceComesHere,
KeyWords = YourQueryToYouKeywordTableComesHere,
....
in case your data sources have other column names you have to change them in my code too. My starting point was always your dummy-table.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Hello @Anonymous
you can add a new column to your data-table with this formula. KeyWords has to reference your data where your stored your keywords. If you have big data then there might some enhances needed
let
CheckKeyWordColumn =Text.Combine( List.Transform(KeyWords[Keyword], (item)=> if Text.Contains(Text.Lower([URL]), Text.Lower(item)) then item else null),", "),
GetCode = try Table.SelectRows(KeyWords, each [Keyword]= CheckKeyWordColumn)[Code]{0} otherwise null
in
GetCode
Here a complete example
let
Data =
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WyigpKSi20tcvLy/Xy60sT00qSExP1UvOz9XPrdQFsXWzUyvL84tSDHUz8nNT9YtLEotKlHSUfCsVwEwFkBqlWB1Uk/JLMlKL4CZ5Q0wwgmjWB5mjl1GSmwMxBsRFmJJakgwUVoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [URL = _t, Description = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"URL", type text}, {"Description", type text}})
in
#"Changed Type",
KeyWords =
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwMDBU0lHyTq0szy9KMVSK1YEIGiEEjZRiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Code = _t, Keyword = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", type text}, {"Keyword", type text}})
in
#"Changed Type",
AddCode = Table.AddColumn(Data, "Code", each let
CheckKeyWordColumn =Text.Combine( List.Transform(KeyWords[Keyword], (item)=> if Text.Contains(Text.Lower([URL]), Text.Lower(item)) then item else null),", "),
GetCode = try Table.SelectRows(KeyWords, each [Keyword]= CheckKeyWordColumn)[Code]{0} otherwise null
in
GetCode)
in
AddCode
Outcome is this
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Hi Jimmy,
This seems to be working if it is a table inserted in Power Query itself (with create table), but what if I loaded the URL from Google Analytics and the keyword table from a database table?
I think I need to tweak 'the green parts' of your code, but not sure exactly what to change it to.
Can you help?
Thanks!
Hello @Anonymous
for sure you have to connect to your real data source. but as I don't know where your data is coming from and I probably have also no access to it, I had to simulate your data with some manual code. You have to change my code in order that its connecting to your data source
let
Data = YourQueryToYourDataSourceComesHere,
KeyWords = YourQueryToYouKeywordTableComesHere,
....
in case your data sources have other column names you have to change them in my code too. My starting point was always your dummy-table.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Hi @Anonymous
It depends on how you extract your Keyword from URL (how the URL constructs), this code is based on your sample, paste in Advanced Editor:
let
Source = Table.FromRows(
Json.Document(
Binary.Decompress(
Binary.FromText(
"VcxBCoAgEAXQq4TrdKhlV4hOEC6shoQaFBsYvH2YEbT7H/5/86w8c7wGABExlAWX6HY0ayCgrEvWB2YJaeu0D4RwsUusWjXl5olN2Sjb/qXAHtMnjVXo6xmKYzzTWZlSX8Xe",
BinaryEncoding.Base64
),
Compression.Deflate
)
),
let
_t = ((type nullable text) meta [Serialized.Text = true])
in
type table [URL = _t, Description = _t]
),
LookupTable = Table.FromRows(
Json.Document(
Binary.Decompress(
Binary.FromText("i45WMjQwMDBU0lHyTq0szy9KMVSK1YEIGiEEjZRiYwE=", BinaryEncoding.Base64),
Compression.Deflate
)
),
let
_t = ((type nullable text) meta [Serialized.Text = true])
in
type table [Code = _t, Keyword = _t]
),
#"Changed Type" = Table.TransformColumnTypes(
Source,
{{"URL", type text}, {"Description", type text}}
),
#"Added Custom" = Table.AddColumn(
#"Changed Type",
"Keyword",
each Text.Proper(
if Text.Contains([URL], "mywebpage") then
Text.BetweenDelimiters([URL], "my-page-", "-")
else
Text.BetweenDelimiters([URL], ".com/", "/")
)
),
#"Added Custom1" = Table.AddColumn(
#"Added Custom",
"Code",
each LookupTable[Code]{List.PositionOf(LookupTable[Keyword], [Keyword])}
)
in
#"Added Custom1"
Hey @Vera_33 Thanks, the code works.
But it looks like you create the keyword table yourself (instead of referring to the table loaded)? Or do I just not understand enough what is happening in the code?
In reality I have lots more URLs (85K of them) and lots more Keywords (about 2K of them).
Can you help explain how to do this with tables loaded and/or explain your code a little more, please?
Hi @Anonymous
Yes, I copied your data and put it in the same query so you can see the result. You can delete it, and refer to another query (your keyword table).
The code from @Jimmy801 is better for different ways of construction in the URL.