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
Anonymous
Not applicable

How to create more than 1 Column using field with different variables

Hi,

 

I uploaded a query and need to use a field that has different variables and create more than 1 column with those variables. Basically I need to segment the variables into different columns and have each column use the count. For example - if the field has colors red, white and blue for different customers, I need to create columns titled red, white and blue for each named customer on the left of my chart. The columns for each color will count the # of times each customer selected that color as a preference. I hope this helps. Thank you.

1 ACCEPTED SOLUTION
jbwtp
Memorable Member
Memorable Member

Hi @Anonymous,

 

Most simplisitically:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpSitVBZ7mDWU4oLCc0ljNchzNcnTNENhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Colour = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Colour", type text}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[Colour]), "Colour", "Colour", List.Count)
in
    #"Pivoted Column"

 

If you have more criteria, you can formalise it by gtrouping to the criteria first:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpSitVBZ7mDWU4oLCc0ljNchzNcnTNENhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Colour = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Colour", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer", "Colour"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Colour]), "Colour", "Count", List.Sum)
in
    #"Pivoted Column"

 

Cheers,

John

 

View solution in original post

1 REPLY 1
jbwtp
Memorable Member
Memorable Member

Hi @Anonymous,

 

Most simplisitically:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpSitVBZ7mDWU4oLCc0ljNchzNcnTNENhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Colour = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Colour", type text}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[Colour]), "Colour", "Colour", List.Count)
in
    #"Pivoted Column"

 

If you have more criteria, you can formalise it by gtrouping to the criteria first:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpSitVBZ7mDWU4oLCc0ljNchzNcnTNENhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Colour = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Colour", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer", "Colour"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Colour]), "Colour", "Count", List.Sum)
in
    #"Pivoted Column"

 

Cheers,

John

 

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.

Top Solution Authors
Top Kudoed Authors