Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
KJoy
New Member

Search Contents of String, Group Similar Results, and Sum Results

Hello,

 

I am working with a large number of rows of URL page paths and their corresponding number of sessions. What I would like to be able to do is search for a specific part of the URL page path, group all of the results together, and calculate the sum of the sessions for those results.

 

I'd like to figure out how to do this with DAX and M.

 

Page URLSessions  Grouped Page URLSessions
/new-snowboard-23jk4j118  /new-snowboard235
/used-snowboard-3jfjk3y67  /used-snowboard148
/new-snowboard-54d5122  /videos54
/videos54  /support40
/used-snowboard-34d78c81    
/support40    
/new-snowboard-a351v2cc95    

 

On the left is an example of the type of data I have. On the right is what I'd like to accomplish.

 

Thanks!

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

If the pattern to create the grouped page URL is to skip the last element after the "-", you could use this M-code:

 

let

   Source = … YourSource …,

   #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sessions", type number}}),

   #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type","Page URL",Splitter.SplitTextByEachDelimiter({"-"}, QuoteStyle.Csv, true),{"Group", "Page URL.2"}),

   #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Page URL.2"}),

   #"Grouped Rows" = Table.Group(#"Removed Columns", {"Group"}, {{"Sessions", each List.Sum([Sessions]), type text}})

in

   #"Grouped Rows"

 

You have to replace ... YourSource .. by what it says 🙂

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

13 REPLIES 13
chuck_stones
New Member

Similarly to the OP, I am trying to use Power BI to pull SharePoint site usage information from Google Analytics. 

 

I have a number of top level sites, with sub-sites, and in some cases more sub-sites, and pages under those sub sites. The hierarchyis something like: 

 

Host URL: 

https://companyname.sharepoint.com

 

Sites: 

/sites/sitename

/sites/sitename/subsite1/subsite2

/teams/teamname

/search/pages

etc. 

 

What I'd like to do is to group the pages by common Site/sub-site and sum session counts for each site/subsite using PowerBI to see, overall which sites have the most sessions, hits, bounce rates etc. 

 

I'm sure there is a way of doing it, but I'm a PowerBI novice and I don't really know where to start. I saw some of the responses here and figured they may be useful in my scenario, but I don't know how to put them into practice. 

 

I figure if I'm going to be using a delimiter, it would need to be on the '/'.

 

Any suggestions? 

OK, so I achieved what I wanted to do by creating a custom column and using the following formula: 

=Text.BeforeDelimiter(Text.Lower([ColumnName]), "/", 3) 

 

This gave me a column by which I could group the sites & subsites. Bonza. However I still have a problem which is that for some reason, Google Analyics captures the site home page as two separate values, depending on how it is accessed, either; 

http://companyname.sharepoint.com/sites/sitename 

or 

http://companyname.sharepoint.com/sites/sitename/ 

 

This is creating two separate rows in my dataset.

 

Is there any way in which I can combine these two rows into one? I tried replace values, but that affects all the rows, thereby turning 

 

http://companyname.sharepoint.com/sites/sitename/subsite1

into 

http://companyname.sharepoint.com/sites/sitenamesubsite1

or 

http://companyname.sharepoint.com/sites/sitename/subsite1

into 

http://companyname.sharepoint.com/sites/sitename//subsite1

 

depending on which value I use as the source for the replace. Anyone got any genius ideas on how I can combine the two rows? 

chuck_stones
New Member

Similarly to the OP, I'm attempting to use Power BI to extract data from Google Analytics and look at grouping URLs by the begining of the URL. For example, our domain is: 

 

http://companyname.sharepoint.com and all the subsequent URLs follow a similar format to the following: 

/sites/sitename

/teams/teamname

/teams/teamname/department

/search/pages/results.aspx

etc. 

 

What I want to do is identify a way to sum the session counts for all the pages within each top level site and the sub sites. I figure this is possible, but being new to PowerBI I have no idea how this would be done. Any suggestions? Any use of delimiter would need to be on '/'. 

KJoy
New Member

@ImkeF that file helped a lot! Thanks!

 

I have a quick question that I'm having a problem with. To preface, I'm doing everything in Power BI Desktop. When I'm editing the queries, my pageviews shows the actual value for the page. However, when I close out of that and I am operating in the Data tab, my pageviews all become a value of 145. I have no idea why this is going on.

Clueless again 🙂

Check out my pbix

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

KJoy
New Member

Having issues with the reply function. In any case..

 

@ImkeF I've created a new Query and implemented the code that you created, however, what it did was duplicate my existing Query. Nothing grouped up as was expected. Any ideas on what I might have done wrong?

 

Thanks!

@KJoy

No ideas - have a look at the file.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

KJoy
New Member

Thank you for the quick and helpful responses.

 

I will try out the suggestions here and let you all know if I have any further questions.

ImkeF
Super User
Super User

If the pattern to create the grouped page URL is to skip the last element after the "-", you could use this M-code:

 

let

   Source = … YourSource …,

   #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sessions", type number}}),

   #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type","Page URL",Splitter.SplitTextByEachDelimiter({"-"}, QuoteStyle.Csv, true),{"Group", "Page URL.2"}),

   #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Page URL.2"}),

   #"Grouped Rows" = Table.Group(#"Removed Columns", {"Group"}, {{"Sessions", each List.Sum([Sessions]), type text}})

in

   #"Grouped Rows"

 

You have to replace ... YourSource .. by what it says 🙂

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

 
greggyb
Resident Rockstar
Resident Rockstar

Very strange error going on with my post above - it is un-editable. There's a strange rendering glitch when I go to edit it.

 

Nevertheless, it is probably best to use the Power Query solution @ImkeF or something similar to do the URL text-munging. This is just standard data cleanup.

 

Rather than persist a sum as a field in the table, though, I'd recommend writing a measure that counts the instances of the URL stem. It would be as simple as a COUNTA():

 

CountURL = 
COUNTA( TestURL[URL] )
Greg_Deckler
Super User
Super User

A little ugly, but you could create a number of columns equal to your groups and use IFERROR and SEARCH (or FIND) to return the starting position or -1 if not found. You could then have another column where you have a big IF statement and put the right "friendly" name into the column based upon which of your grouping columns are not -1. Brute force, probably a slicker way to do it, but should work.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.