Forum Discussion
random sampling
Hi there. I'll give you an idea about how to do this. This is probably the only way to do it with the current version of DAX and Power BI.
First, let's say you have a table (call it 'Data') in the model with N rows which are numbered consecutively, say, from 0 to (N-1) (the column is called Index). Now, you'll construct a table ('Samples') with these properties:
- The first column will hold numbers from 1 to K (there'll be many rows for each value).
- The second column will hold the indexes of rows that will be selected for a particular k and SampleId.
- The third column will hold the SampleId.
Here's the structure:
| k | Index | SampleId |
| 1 | 2 | 1 |
| 1 | 4 |
2 |
| 1 | 9 | 3 |
| 2 | 1 | 1 |
| 2 | 6 | 1 |
| 2 | 8 | 2 |
| 2 | 3 | 2 |
| 3 | 4 | 1 |
| 3 | 2 | 1 |
| 3 | 8 | 1 |
...
Can you see what's going on here? The k column will be sitting on one slicer, the SampleId on another. When you select a value of k, the other slicer will adjust the SampleId from 1 to the number of samples for that particular k. Now you just have to select a sample from the range slicer. This is how you'll obtain a (pseudo-)random selection of rows.
For each k you have to create samples where each sample will be a unique combination of the indexes of the rows to select. For k = 1, you only need N samples to cover all possibilities. For k = 2, you need N*(N-1)/2 combinations, for k = 3, you need C(N, k) combinations... but this number of samples will be going up quickly as k increases. So, when k is relatively big you don't need to cover all combinations. Just pick enough of them. Once you have the above table, you can write a measure [Select Row] that will return 1 if the row should be selected and 0 if not. Here it is:
[Select Row] =
var __k = SELECTEDVALUE( 'Samples'[K] )
var __oneSampleSelected = HASONEVALUE( 'Samples'[SampleId] )
var __currentRowid = SELECTEDVALUE( 'Data'[Index] )
var __selectRow =
__currentRowid in VALUES( 'Samples'[Index] )
return
if( __oneSampleSelected, 1 * __selectRow )
The visual in which you've placed the rows of the Data table should have a filter on it so that a row will display when [Select Row] is 1.
Best
D
Thank you vey miuch ! Will try this and update how it goes