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.
Hi,
I'm trying to calculate the occurence of products in a column .Below is the table:
Products |
a,b |
b |
c,a |
d |
Expected Result:
Products | Count_prod |
a | 2 |
b | 2 |
c | 1 |
d | 1 |
Can we achieve this in DAX (or) power query? Thanks in advance for the help!!
Solved! Go to Solution.
HI @Anonymous,
You can use the following formula to create a new calculated table based on the original table products field:
NEW Table =
VAR _path =
SUBSTITUTE (
CONCATENATEX ( ALL ( 'Table'[Products] ), [Products], "," ),
",",
"|"
)
VAR _length =
PATHLENGTH ( _path )
VAR _pathtable =
ADDCOLUMNS (
GENERATESERIES ( 1, _length, 1 ),
"Product", PATHITEM ( _path, [Value] )
)
RETURN
ADDCOLUMNS (
SUMMARIZE ( _pathtable, [Product] ),
"Count", COUNTROWS ( FILTER ( _pathtable, [Product] = EARLIER ( [Product] ) ) )
)
Reference link:
DAX how split a string by delimiter into a list or array?
Regards,
Xiaoxin Sheng
HI @Anonymous,
You can use the following formula to create a new calculated table based on the original table products field:
NEW Table =
VAR _path =
SUBSTITUTE (
CONCATENATEX ( ALL ( 'Table'[Products] ), [Products], "," ),
",",
"|"
)
VAR _length =
PATHLENGTH ( _path )
VAR _pathtable =
ADDCOLUMNS (
GENERATESERIES ( 1, _length, 1 ),
"Product", PATHITEM ( _path, [Value] )
)
RETURN
ADDCOLUMNS (
SUMMARIZE ( _pathtable, [Product] ),
"Count", COUNTROWS ( FILTER ( _pathtable, [Product] = EARLIER ( [Product] ) ) )
)
Reference link:
DAX how split a string by delimiter into a list or array?
Regards,
Xiaoxin Sheng
Name | Products |
Tom | a,b |
Harry | b |
Tom | c, a |
Potter | d |
Expected Result:
Products | Count_prod |
A | 2 |
Tom | 1 |
Tom | 1 |
B | 2 |
Harry | 1 |
Tom | 1 |
C | 1 |
Tom | 1 |
D | 1 |
Potter | 1 |
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKTylNLilWitWJVkrUSQLTEDJZJxFMpyjFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}}),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Products", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type1", "Products", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Products.1", "Products.2"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Products.1", type text}, {"Products.2", type text}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type2", {}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Value"}, {{"Count", each Table.RowCount(_), type number}})
in
#"Grouped Rows"
@Anonymous or you can try this, if you don't want to group in power query, remove last step and then with DAX you can group by product
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKTylNLilWitWJVkrUSQLTEDJZJxFMpyjFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}}),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Products", type text}}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type1", {{"Products", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Products"),
#"Grouped Rows" = Table.Group(#"Split Column by Delimiter", {"Products"}, {{"Count", each Table.RowCount(_), type number}})
in
#"Grouped Rows"
Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!
Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo
If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤
Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
111 | |
100 | |
80 | |
64 | |
58 |
User | Count |
---|---|
148 | |
111 | |
93 | |
84 | |
66 |