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
DivyaJohn
Regular Visitor

Desktop

Hi,

Need help with a scenario as mentioned below:

I have data with Customers and their Products. I want to show the count of Customers based on the Product

example: there are two customers which have Web Only (C&E) , one customer has only Book Only (D), one customer has Book&Web&Insurance (A) and one Customer has Book&Insurance (B)

 

Im trying to show the result as in the 'Expected Result' column.  Thanks! for all your help

 

CustomerProductsExpected Result
ABookBook,Web,Insurance
AWeb 
AInsurance 
BBookBook,Insurance
BInsurance 
CWebWeb Only
DBookBook Only
EWebWeb Only
2 ACCEPTED SOLUTIONS
nsexton12
Resolver II
Resolver II

Does the attached screenshot work for you? Or do you need to see the blank rows with other data. I can modify the code to include that if needed. If this works, the code is below. 

 

 

Transformation Result.png

 

 

 

 

Please copy this code and paste it starting at the second line after opening your Advanced Editor. It should be directly after the Source line. You will need to replace every place in the code below where I have "Table1" with your query name. You can also just change your query name to Table1 and this should work.

 

#"Filtered Rows" = Table.SelectRows(Source, each ([Name] = "Table1")),
#"Expanded Data" = Table.ExpandTableColumn(#"Filtered Rows", "Data", {"Customer", "Products"}, {"Customer", "Products"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Data",{"Item", "Kind", "Hidden", "Name"}),
#"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Running Count", (r) =>
List.Count(
Table.SelectRows(#"Added Index", each [Customer] = r[Customer] and [Index] <= r[Index]) [Customer])),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Value", each "Value " & Number.ToText([Running Count])),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Index", "Running Count"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Value]), "Value", "Products"),
#"Added Custom2" = Table.AddColumn(#"Pivoted Column", "Expected Result", each if [Value 3] = null and [Value 2] <> null then [Value 1] & ", " & [Value 2] else if [Value 2] = null and [Value 3] = null then [Value 1] & " Only" else [Value 1] & ", " & [Value 2] & ", " & [Value 3]),
#"Removed Columns2" = Table.RemoveColumns(#"Added Custom2",{"Value 1", "Value 2", "Value 3"})
in
#"Removed Columns2"

 

If this works for you, please mark as a solution!

 

View solution in original post

v-huijiey-msft
Community Support
Community Support

Hi @DivyaJohn ,

 

Please follow these steps:

 

1. This is the original data I created:

vhuijieymsft_0-1708937399374.png

 

2. Under the "Data" tab, select "New Column".

Expected Result = 
VAR a = CALCULATETABLE(
    VALUES('Table'[Products]),
    ALLEXCEPT('Table', 'Table'[Customer])
)
VAR ProductCount = COUNTROWS(a)
VAR b = CONCATENATEX(a, 'Table'[Products], ", ")
RETURN
IF(
    ProductCount > 1, b,
    IF(ProductCount = 1, CONCATENATEX(a, 'Table'[Products], "") & " Only", "")
)

 

3. Drag fields to the report page for display.

vhuijieymsft_1-1708937399377.png

 

If you have any further questions please feel free to contact me.

 

pbix file is attached.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

View solution in original post

3 REPLIES 3
v-huijiey-msft
Community Support
Community Support

Hi @DivyaJohn ,

 

Please follow these steps:

 

1. This is the original data I created:

vhuijieymsft_0-1708937399374.png

 

2. Under the "Data" tab, select "New Column".

Expected Result = 
VAR a = CALCULATETABLE(
    VALUES('Table'[Products]),
    ALLEXCEPT('Table', 'Table'[Customer])
)
VAR ProductCount = COUNTROWS(a)
VAR b = CONCATENATEX(a, 'Table'[Products], ", ")
RETURN
IF(
    ProductCount > 1, b,
    IF(ProductCount = 1, CONCATENATEX(a, 'Table'[Products], "") & " Only", "")
)

 

3. Drag fields to the report page for display.

vhuijieymsft_1-1708937399377.png

 

If you have any further questions please feel free to contact me.

 

pbix file is attached.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

nsexton12
Resolver II
Resolver II

Does the attached screenshot work for you? Or do you need to see the blank rows with other data. I can modify the code to include that if needed. If this works, the code is below. 

 

 

Transformation Result.png

 

 

 

 

Please copy this code and paste it starting at the second line after opening your Advanced Editor. It should be directly after the Source line. You will need to replace every place in the code below where I have "Table1" with your query name. You can also just change your query name to Table1 and this should work.

 

#"Filtered Rows" = Table.SelectRows(Source, each ([Name] = "Table1")),
#"Expanded Data" = Table.ExpandTableColumn(#"Filtered Rows", "Data", {"Customer", "Products"}, {"Customer", "Products"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Data",{"Item", "Kind", "Hidden", "Name"}),
#"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Running Count", (r) =>
List.Count(
Table.SelectRows(#"Added Index", each [Customer] = r[Customer] and [Index] <= r[Index]) [Customer])),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Value", each "Value " & Number.ToText([Running Count])),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Index", "Running Count"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Value]), "Value", "Products"),
#"Added Custom2" = Table.AddColumn(#"Pivoted Column", "Expected Result", each if [Value 3] = null and [Value 2] <> null then [Value 1] & ", " & [Value 2] else if [Value 2] = null and [Value 3] = null then [Value 1] & " Only" else [Value 1] & ", " & [Value 2] & ", " & [Value 3]),
#"Removed Columns2" = Table.RemoveColumns(#"Added Custom2",{"Value 1", "Value 2", "Value 3"})
in
#"Removed Columns2"

 

If this works for you, please mark as a solution!

 

wini_R
Resolver IV
Resolver IV

Hi @DivyaJohn,

 

in power query you can try this code (could be simplified definitely):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXLKz89WitWBcMJTk+Bsz7zi0qLEvORUsIgTslInDGlnJM0uyEpdYRKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Products = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Customer"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"Grouping", each _, type table}} ),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "GroupingIndex", each Table.AddIndexColumn([Grouping], "indeks", 0, 1)),
    #"Expanded Grouping" = Table.ExpandTableColumn(#"Added Custom", "GroupingIndex", {"Products", "indeks"}, {"Products", "indeks"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Grouping", "result", each if [Count] > 1 and [indeks] = 0 then Text.Combine([Grouping][Products], ",") else if [Count] = 1 then [Grouping][Products]{0} & " only" else ""),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Customer", "Products", "result"})
in
    #"Removed Other Columns"

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!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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