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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
MagnusJ
Frequent Visitor

Concatenating text items into sequences based on previous rows

I have a problem that I have been attempting to solve through various approaches but it is slightly too complex for me. Not even sure if the best solution is through DAX or by using Power Query.
Power Query has at least helped me reduce the complexity of the data set but the last step is difficult. This is a problem that would be quite simple in excel but I can not solve it through DAX or in Power Query.

 

I have a table with customers with numbered identities and for each customer an ownership of a product (in the below example a car brand), represented by a text string. I need to create sequences of ownership, i.e. how the customer switches from one brand to another in order to later on calculate how many customers display a certain switching pattern (the image is from an excel sheet to simplify the data set). All duplicates have been removed, i.e. rows for subsequent periods with the same brand ownership for a customer have been removed through filtering.

 

One solution would be to create a new table but that might not be neecessary as long as the number of customers can be counted in a later step (e.g. through DISTINCTCOUNT). And since this problem requires combining/concatenating text strings it is as far as I know not easily solved by DAX functions through creating a new table.

 

Any ideas on how to solve this are much appreciated.

/Magnus

 

Customer numberBrandSequence New table?CustomerSequence
1Ford   1Ford->Nissan
1NissanFord->Nissan  5Nissan->Toyota->Mercedes
5Nissan   10BMW->Mercedes
5Toyota   25Nissan->Toyota->BMW->Mercedes
5MercedesNissan->Toyota->Mercedes  31Ford->Nissan
10BMW     
10MercedesBMW->Mercedes    
15Toyota     
25Nissan     
25Toyota     
25BMW     
25MercedesNissan->Toyota->BMW->Mercedes    
31Ford     
31NissanFord->Nissan    
1 ACCEPTED SOLUTION
Sean
Community Champion
Community Champion

@MagnusJ

I'm sure what @MarcelBeug suggests works! Smiley Happy

However you don't need to create another table to achieve this

You just need a single Measure

Sequence MEASURE =
IF (
    HASONEVALUE ( 'Table'[Customer number] ),
    CONCATENATEX ( 'Table', 'Table'[Brand], "-> " )
)

Here's the result

COCATENATEX Example.gif

 

 

If you do want to create another table on the Modeling tab click New Table and type...

Summary Table =
SUMMARIZECOLUMNS (
    'Table'[Customer number],
    "Sequence of Ownership", CONCATENATEX ( 'Table', 'Table'[Brand], "-> " )
)

Here's this result...

CONCATENATEX Example2.png

 

Hope this helps! Smiley Happy

 

 

 

View solution in original post

3 REPLIES 3
MagnusJ
Frequent Visitor

Excellent solutions! Thank you! I went with the summarizecolumns-solution to be able to use the concatenated strings as an axis in a chart or as a legend.  Power Query-solution would also be possible of course. All in all an interesting problem with what looks like simple solutions once you see it, but it was impossible for me to crack. Expertise was needed! Thanks!

MarcelBeug
Community Champion
Community Champion

In Power Query you can create base code with the "Group By" option on the Transform tab and adjust the generated code to combine the texts as ilustrated in this video. It is recorded with Excel, but can be used in Power BI as well.

 

Below the generated code. I just noticed after recording that you don't need single values.

These can be filtered out by adding additional aggregation "Count Rows" with "Group By" and filter out the 1's afterwards.

Or keep the recorded solution and add a step to filter out Sequences that don't contain " -> ".

 

let
    Source = Excel.CurrentWorkbook(){[Name="Input"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer number", Int64.Type}, {"Brand", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer number"}, {{"Sequence", each Text.Combine([Brand]," -> "), type text}})
in
    #"Grouped Rows"
Specializing in Power Query Formula Language (M)
Sean
Community Champion
Community Champion

@MagnusJ

I'm sure what @MarcelBeug suggests works! Smiley Happy

However you don't need to create another table to achieve this

You just need a single Measure

Sequence MEASURE =
IF (
    HASONEVALUE ( 'Table'[Customer number] ),
    CONCATENATEX ( 'Table', 'Table'[Brand], "-> " )
)

Here's the result

COCATENATEX Example.gif

 

 

If you do want to create another table on the Modeling tab click New Table and type...

Summary Table =
SUMMARIZECOLUMNS (
    'Table'[Customer number],
    "Sequence of Ownership", CONCATENATEX ( 'Table', 'Table'[Brand], "-> " )
)

Here's this result...

CONCATENATEX Example2.png

 

Hope this helps! Smiley Happy

 

 

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.