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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
jonbox
Helper II
Helper II

Add a new column for a random sample

Hi,

 

I'm looking to randomly select 50% of rows in a table based on a filter, creating a new column that adds "Sampled" to the row being sampled"

 

I've tried the following code but it doesn't work as sample takes multiple columns into account when returning. Countblank is based on a column i created to separate low value & high value assets. 

 

 

 

Sampled = SAMPLE(COUNTBLANK('Asset'[Blank Value]) / 2 , 'Assets', 'Assets'[RFID Tag],ASC)

 

 

 

 

e.g. if i have the below table, i want to take 50% of all rows that have a value "Low".

IDValue
1Low
2Low
3High
4Low
5Low
7High
8Low
9Low
10High

 

Which would create this column:

only takes into consideration low value assets, 50% Of the 6 low value assets = 3, so only 3 sampled.

 

IDValueSample
1LowSampled
2Low 
3High 
4LowSampled
5Low 
7High 
8LowSampled
9Low 
10High 

 

Thank you!!

 

1 ACCEPTED SOLUTION
Ahmedx
Super User
Super User

I'm not sure if I understood you correctly, but try this anyway

Screenshot_3.png

View solution in original post

3 REPLIES 3
Ahmedx
Super User
Super User

you forgot the ) after ASC
VAR _SampleSize = SELECTCOLUMNS( SAMPLE(_randomlyselect,_tbl,[Asset Code],ASC),"@Asset Code",[Asset Code],"@High Value Asset 2",[High Value Asset 2])

Ahmedx
Super User
Super User

I'm not sure if I understood you correctly, but try this anyway

Screenshot_3.png

Hi Ahmed, Thanks for the response.

 

I've got as far as the return expression but as soon as i put that in it says unexpected expression return.

 

jonbox_0-1690902139660.png

Do you know why this might be?

 

Sampled = 
VAR _select = "Low Value"
VAR _tbl = FILTER(ALL('hardcat HardcatAssets1'),'hardcat HardcatAssets1'[High Value Asset 2]=_select)
VAR _randomlyselect = COUNTROWS(_tbl) * 0.1 -- 10%
VAR _SampleSize = SELECTCOLUMNS( SAMPLE(_randomlyselect,_tbl,[Asset Code],ASC,"@Asset Code",[Asset Code],"@High Value Asset 2",[High Value Asset 2])
RETURN
 IF(COUNTROWS(FILTER(_SampleSize,[@Asset Code]=[Asset Code]&&[@High Value Asset 2]=[High Value Asset 2])),"Sampled")

//SAMPLE(COUNTBLANK('hardcat HardcatAssets1'[Blank Value]) / 10 , 'hardcat HardcatAssets1', 'hardcat HardcatAssets1'[RFID Tag],ASC)

 

Thanks

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors