Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Add a value to random rows with specified quantity

Hi everyone! Is there a way to add a string of text to random rows with specified quantity? 

The "Sample" column below is what I want to achieve.

In this example, there are 4 rows with Red color, and the sampling % for red is 50%. Thus, for 2 random rows with red color (50% of 4) the text "for sampling" should be added.

 

Unit #ColorSampling %SampleOccurenceRequired Sampling
1Red50% 42
2Blue30%For sampling11
3Black25%For sampling11
4Red50%For sampling42
5Red50% 42
6Red50%For sampling42

 

So far I have added a column for occurrence count

Occurence = COUNTX(FILTER('Unit', EARLIER ('Unit'[Color] ) = 'Unit'[Color]),'Unit'[Color])

 

then multiplying it with the % to get the required number of sampling rounded up

Required Sampling = ROUNDUP('Unit'[Sampling %]*'Unit'[Occurence],0)

 

but I can't figure out how to implement the required # of sampling to random rows

or if there's a way to create non-repeating random numbers 

 

Thanks in advance!

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    I was able to find a workaround for now by adding an index column then creating a column with the following formula :

    Sampling Index = rankx(
        FILTER('Unit',EARLIER('Unit'[Color])='Unit'[Color]
             &&EARLIER('Unit'[Unit #])='Unit'[Unit #]),
            'Unit'[Index])
     
    and then the Sample column would be
    Sample = if('Unit'[Sampling Index]<='Unit'[Required Sampling],"For sampling",BLANK())
     
    not sure if this only gets the first x for sampling, but I checked and it doesn't necessarily get the data in the same order as the raw data so this may work.
     
    any other ideas are still welcome 🙂