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
ryan_b_fiting
Post Patron
Post Patron

Count number of times value appears based on another column

Hello PQ Community - 

I am looking for some help.  I have a data table that contains a wireless number, a billing date and a partial/full flag.  What I am looking to do is count the number of times that the partial flag = "Partial" for a given wireless number.  Here is a basic sample of the data:

 

Wireless NumberBilling DatePartial/Full
555-555-55551/1/2023Partial
555-555-55552/1/2023Full
555-555-55553/1/2023Full
555-555-55554/1/2023Full
555-555-55555/1/2023Full
555-555-55556/1/2023Full
999-999-99992/1/2023Full
999-999-99993/1/2023Full
999-999-99994/1/2023Full
999-999-99995/1/2023Full
999-999-99996/1/2023Full
111-111-11111/1/2023Partial
111-111-11112/1/2023Full
111-111-11113/1/2023Full
111-111-11114/1/2023Full
111-111-11115/1/2023Full
111-111-11116/1/2023Partial

 

I need the output to look like this at the end in Power Query:

Wireless NumberBilling DatePartial/FullPartial Count
555-555-55551/1/2023Partial1
555-555-55552/1/2023Full1
555-555-55553/1/2023Full1
555-555-55554/1/2023Full1
555-555-55555/1/2023Full1
555-555-55556/1/2023Full1
999-999-99992/1/2023Full0
999-999-99993/1/2023Full0
999-999-99994/1/2023Full0
999-999-99995/1/2023Full0
999-999-99996/1/2023Full0
111-111-11111/1/2023Partial2
111-111-11112/1/2023Full2
111-111-11113/1/2023Full2
111-111-11114/1/2023Full2
111-111-11115/1/2023Full2
111-111-11116/1/2023Partial2

 

I am having a hard time figuring out how to handle this in Power Query, so it will count the Partial flag by wireless number and apply that count to all rows for the number.

 

Any quick help would be greatly appreciated!

Thanks
Ryan F.

1 ACCEPTED SOLUTION
adudani
Super User
Super User

hi @ryan_b_fiting 

 

Create a blank query , copy and paste the below code into the advanced editor:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdGxCoAgFIXhd3FO7Go38AWa28XBMXCKev8ahOB0xOGCwzf8B1MyqmrbqZmMOHF+9uF97uW8jlJNnn7Kf2q7KyVhTJYx0TFZCYkx2naxkwuE5QJhuUBYLhCWKyK2nfT/ABQbBYSNAsJGAWGjgKykOD8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Wireless Number" = _t, #"Billing Date" = _t, #"Partial/Full" = _t]),
    #"Partial Count" = Table.AddColumn(Source, "Partial Count", each if [#"Partial/Full"] = "Partial" then 1 else 0),
    #"Changed Type" = Table.TransformColumnTypes(#"Partial Count",{{"Wireless Number", type text}, {"Billing Date", type date}, {"Partial/Full", type text}, {"Partial Count", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Wireless Number"}, {{"Detail", each _, type table [Wireless Number=nullable text, Billing Date=nullable date, #"Partial/Full"=nullable text, Partial Count=nullable number]}, {"PC", each List.Sum([Partial Count]), type nullable number}}),
    #"Expanded Detail" = Table.ExpandTableColumn(#"Grouped Rows", "Detail", {"Wireless Number", "Billing Date", "Partial/Full", "Partial Count"}, {"Wireless Number.1", "Billing Date", "Partial/Full", "Partial Count"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Detail",{"Partial Count", "Wireless Number"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Wireless Number.1", "Wireless Number"}, {"PC", "Partial Count"}})
in
    #"Renamed Columns"

Output:

adudani_0-1705024726114.png

 

Steps:

1. created a partial count column, if partial then 1 else 0

2. grouped by wireless number, sum the partial count column, another aggregation with operation: all rows

3. expand the table.

 

the code could be made more dynamic/ shorter.

 

let me know if this works for you.

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

View solution in original post

2 REPLIES 2
adudani
Super User
Super User

hi @ryan_b_fiting 

 

Create a blank query , copy and paste the below code into the advanced editor:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdGxCoAgFIXhd3FO7Go38AWa28XBMXCKev8ahOB0xOGCwzf8B1MyqmrbqZmMOHF+9uF97uW8jlJNnn7Kf2q7KyVhTJYx0TFZCYkx2naxkwuE5QJhuUBYLhCWKyK2nfT/ABQbBYSNAsJGAWGjgKykOD8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Wireless Number" = _t, #"Billing Date" = _t, #"Partial/Full" = _t]),
    #"Partial Count" = Table.AddColumn(Source, "Partial Count", each if [#"Partial/Full"] = "Partial" then 1 else 0),
    #"Changed Type" = Table.TransformColumnTypes(#"Partial Count",{{"Wireless Number", type text}, {"Billing Date", type date}, {"Partial/Full", type text}, {"Partial Count", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Wireless Number"}, {{"Detail", each _, type table [Wireless Number=nullable text, Billing Date=nullable date, #"Partial/Full"=nullable text, Partial Count=nullable number]}, {"PC", each List.Sum([Partial Count]), type nullable number}}),
    #"Expanded Detail" = Table.ExpandTableColumn(#"Grouped Rows", "Detail", {"Wireless Number", "Billing Date", "Partial/Full", "Partial Count"}, {"Wireless Number.1", "Billing Date", "Partial/Full", "Partial Count"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Detail",{"Partial Count", "Wireless Number"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Wireless Number.1", "Wireless Number"}, {"PC", "Partial Count"}})
in
    #"Renamed Columns"

Output:

adudani_0-1705024726114.png

 

Steps:

1. created a partial count column, if partial then 1 else 0

2. grouped by wireless number, sum the partial count column, another aggregation with operation: all rows

3. expand the table.

 

the code could be made more dynamic/ shorter.

 

let me know if this works for you.

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

@adudani thank you.  This worked perfectly for my need.  I was able to implement the Table.Group into my full data set and add the the additional columns that I needed in the detail and expand.

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