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
Anonymous
Not applicable

Selecting Sample based on volume

Hi Power Bi Community!

What I need to is select a sample (this could be a new table). I need to select 50% of any category having more than 10 records  any records  || if less than 10 and 5 or more records then to select 5 || if below 5 return all records.

 

For Example: the table returned (output) from the table below (input), should be 5 records with A, 5 records with B and 3 records with C. 

 

I've tried to use the sample formula but I can only apply 1 number to the sample size (e.g 0.5 to represent the 50%) but i cannot select sample based on the count of record. any help will be much appreciated, thank you in advance.

 

CategoryIDValue
A125
A236
A315
A412
A523
A612
A716
A812
A945
A1012
B1171
B1215
B1314
B1455
B1526
B1643
C1721
C1811
C1919

 

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Here is one way to do that in the query editor.  If you need both, you could reference your current query and do this.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.  This keeps the latest 5 rows (but you could remove the sort step to keep the first).

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VY67DcAgDER3cU0RzL9MMgZi/zXCYYkLhRFPB+fXu9zixM/RJMMZ6pyQNwa8YBqBujHhQ9iYz7QAWVXPtKGOzf5i/OAKr+LJSpPFyyySoZZ++XLLZMhFc33BsFNPrrZ0c7NjjA8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, ID = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"ID", Int64.Type}, {"Value", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"ID", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Category"}, {{"AllRows", each _, type table [Category=nullable text, ID=nullable number, Value=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "First5", each Table.FirstN([AllRows], 5)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AllRows"}),
    #"Expanded First5" = Table.ExpandTableColumn(#"Removed Columns", "First5", {"ID", "Value"}, {"ID", "Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded First5",{{"ID", Int64.Type}, {"Value", Int64.Type}})
in
    #"Changed Type1"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

4 REPLIES 4
Anonymous
Not applicable

@mahoneypat you are awesome! thank you so much

mahoneypat
Employee
Employee

Here is one way to do that in the query editor.  If you need both, you could reference your current query and do this.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.  This keeps the latest 5 rows (but you could remove the sort step to keep the first).

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VY67DcAgDER3cU0RzL9MMgZi/zXCYYkLhRFPB+fXu9zixM/RJMMZ6pyQNwa8YBqBujHhQ9iYz7QAWVXPtKGOzf5i/OAKr+LJSpPFyyySoZZ++XLLZMhFc33BsFNPrrZ0c7NjjA8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, ID = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"ID", Int64.Type}, {"Value", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"ID", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Category"}, {{"AllRows", each _, type table [Category=nullable text, ID=nullable number, Value=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "First5", each Table.FirstN([AllRows], 5)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AllRows"}),
    #"Expanded First5" = Table.ExpandTableColumn(#"Removed Columns", "First5", {"ID", "Value"}, {"ID", "Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded First5",{{"ID", Int64.Type}, {"Value", Int64.Type}})
in
    #"Changed Type1"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hi @mahoneypat ,Thank you for coming back with the code, it seems like it's pulling off the last five on each category. this is useful but I need somthing that takes a % sample as first parameter for high counts, then 5 for count >4  and <10 anything followed by all within that category. 

Here you go.  This version does it dynamically.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VY67DcAgDER3cU0RzL9MMgZi/zXCYYkLhRFPB+fXu9zixM/RJMMZ6pyQNwa8YBqBujHhQ9iYz7QAWVXPtKGOzf5i/OAKr+LJSpPFyyySoZZ++XLLZMhFc33BsFNPrrZ0c7NjjA8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, ID = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"ID", Int64.Type}, {"Value", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"ID", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Category"}, {{"AllRows", each _, type table [Category=nullable text, ID=nullable number, Value=nullable number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Rows To Keep", each let rowcount = Table.RowCount([AllRows]) in if rowcount > 10 then 0.5 * rowcount else 5),
    #"Added Custom" = Table.AddColumn(#"Added Custom1", "First5", each Table.FirstN([AllRows], [Rows To Keep])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AllRows"}),
    #"Expanded First5" = Table.ExpandTableColumn(#"Removed Columns", "First5", {"ID", "Value"}, {"ID", "Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded First5",{{"ID", Int64.Type}, {"Value", Int64.Type}})
in
    #"Changed Type1"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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.