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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
apoje
Helper II
Helper II

Custom Column - that counts distinct occurrences of an ID

Hi,

 

I have a dataset where I would benefit greatly with a helper column to see if the order has more items than one in an order. One row means one item - and I have an order ID, if there are more items in an order there are multiple rows with identical order IDs.

 

Desired result:

Order.IDItem.idQuantityHelper.Column.Count
1182612311
1182712322
1182712512
1182812821
1182912413
1182912353
1182914133
1183012311

 

 

I would appreciate if anyone can help me how to compute this Helper column. 

 

Thanks!

Andraz

1 ACCEPTED SOLUTION
camargos88
Community Champion
Community Champion

Hi @apoje ,

 

You can use variable and list to count the ocurrence of each value, like:

let _orderId = [Order.ID] in
List.Count(List.Select(#"Changed Type"[Order.ID], each _ = _orderId))

 

Capture.PNG



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



View solution in original post

6 REPLIES 6
camargos88
Community Champion
Community Champion

Hi @apoje ,

 

You can use variable and list to count the ocurrence of each value, like:

let _orderId = [Order.ID] in
List.Count(List.Select(#"Changed Type"[Order.ID], each _ = _orderId))

 

Capture.PNG



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



@camargos88  Nice.  How does this perform for large tables?

Hi @lbendlin ,

 

I haven't tested it with a large database.

However I believe once it's a list, shouldn't have a heavy impact on it.

But it worths a test.



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



lbendlin
Super User
Super User

do you want that in Power Query or in DAX?

@lbendlin  PowerQuery if possible 🙂

This is one example how to do it in Power Query.  If you have a large table then you might want to throw in a Table.Buffer

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjS0MDJT0lEyNDIGkWAcqwMRN4eLG4ExqrgpVD1C3AIsbgFVjzDHEixuAlVvjCYOMt8UU9wEohZJ3NgA3Z2xAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Order.ID = _t, Item.id = _t, Quantity = _t, Helper.Column.Count = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order.ID", Int64.Type}, {"Item.id", Int64.Type}, {"Quantity", Int64.Type}, {"Helper.Column.Count", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Order.ID"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    Merged = Table.AddColumn(#"Changed Type","Join",(k)=> Table.SelectRows(#"Grouped Rows",each([Order.ID]=k[Order.ID]))),
    #"Expanded Join" = Table.ExpandTableColumn(Merged, "Join", {"Count"}, {"Count"})
in
    #"Expanded Join"

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors