'PowerBI DAX Query to Return Keyword from String

I have a table that contains a column that holds PageURL in them. I want to pull out the value from the keyword in the URL.

Examples of URL: ?keyword=soccer&orderbydec=true

I want my table to have a new column called "Keyword" and display "soccer" in that column. However, if there is no Keyword, display NULL.

Example Table Output

PageURL Users Keyword
something.com/foo?keyword=soccer&orderbydec=true 45 soccer
something.com/foobarfoo?keyword=football 45 football
something.com/foobar 12 NULL


Solution 1:[1]

OK, I solved this and thought I would share in case someone else is trying to pull out keywords from Google Analytics Query String into a PowerBi report :-)

A HUGE thank you to Ruth Pozuelo Martinez from Curbal for their tutorial on using DAX Search!

https://www.youtube.com/watch?v=Tkqc5pOWAKY

GetKeyWord = 
// Get Start Postion AFTER the term 'keyword' in the URL querystring
var _StartPosition = SEARCH("keyword", 'TableName'[Field],,LEN('TableName'[Field])) + 7   

/*
Filter Results where the URL has a querystring with the term 'keyword'
Then get the string that follows to the RIGHT of that term.
*/
var _FindKeyWords = IF(
    CONTAINSSTRING('TableName'[Field],"keyword"),
        RIGHT('TableName'[Field], LEN('TableName'[Field]) - _StartPosition)
        ,
        "NULL"
    )
// Get Position if the term "&" inside the new string _FindKeyWords
var _EndPosition = SEARCH("&", _FindKeyWords,,LEN(_FindKeyWords))

RETURN

// Conditional to check for '&' inside new string and pull keyword to the LEFT of that position
IF(
    CONTAINSSTRING(_FindKeyWords,"&"),
        LEFT(_FindKeyWords,  _EndPosition - 1)
        ,
        LEFT(_FindKeyWords, _EndPosition)
)    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin