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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
PBIfolks
Regular Visitor

Count number of occurrence of value in a Custom column = Power Query

Hi

Sample Item table with duplicate Item Codes. In the Output, I like to capture Item Code with date, if the there is no date, then also include like to get the Item Code. Incase of Duplicate Item code, capture Item which got E Date. As shown in the Output screenshot.

Sofar i managed to get the desired output as shown below.
   Count = List.Count(
   Table.SelectRows(
   Key,
   (C) => [Key] = C[Key]
    )[Key]
    ))

The Custom Column code is working, but when i filter the count this is taking ages to load the data. The data source is not very heavy it only got two excel tbl with 17K and 8K rows of data.

So i like to know is there a different technique or PQ code to achieve this in Power Query, pls?

Many ThanksPQ.jfif

 

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @PBIfolks 

 

You can try this code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRX0lFyBGIjAyMjfXN9CxjT0FDfyFApVidaydgUKOYEEzfRNwLxwTJmIJYzWMbQUt9Y38gIiW2GVYk5WG8sAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Code" = _t, #"Item Name" = _t, #"M Date" = _t, #"E Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item Code", Int64.Type}, {"Item Name", type text}, {"M Date", type date}, {"E Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Item Code"}, {{"All Data", each if Table.RowCount(_) > 1 then Table.SelectRows(_, each [E Date] <> null) else _, type table [Item Code=nullable number, Item Name=nullable text, M Date=nullable date, E Date=nullable date]}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"All Data"}),
    #"Expanded All Data" = Table.ExpandTableColumn(#"Removed Other Columns", "All Data", {"Item Code", "Item Name", "M Date", "E Date"}, {"Item Code", "Item Name", "M Date", "E Date"})
in
    #"Expanded All Data"

Steps are:

1. Group by Item Code and filter out null E Date rows for duplicated Item rows in the output table column.

2. Remove Item Code column. 

3. Expand the table column obtained from step 1. 

vjingzhang_0-1675221860221.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

 

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @PBIfolks 

 

You can try this code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRX0lFyBGIjAyMjfXN9CxjT0FDfyFApVidaydgUKOYEEzfRNwLxwTJmIJYzWMbQUt9Y38gIiW2GVYk5WG8sAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Code" = _t, #"Item Name" = _t, #"M Date" = _t, #"E Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item Code", Int64.Type}, {"Item Name", type text}, {"M Date", type date}, {"E Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Item Code"}, {{"All Data", each if Table.RowCount(_) > 1 then Table.SelectRows(_, each [E Date] <> null) else _, type table [Item Code=nullable number, Item Name=nullable text, M Date=nullable date, E Date=nullable date]}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"All Data"}),
    #"Expanded All Data" = Table.ExpandTableColumn(#"Removed Other Columns", "All Data", {"Item Code", "Item Name", "M Date", "E Date"}, {"Item Code", "Item Name", "M Date", "E Date"})
in
    #"Expanded All Data"

Steps are:

1. Group by Item Code and filter out null E Date rows for duplicated Item rows in the output table column.

2. Remove Item Code column. 

3. Expand the table column obtained from step 1. 

vjingzhang_0-1675221860221.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

 

Thanks @v-jingzhang @worked well. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.